{
"tool_response": {
"filePath": "\/var\/www\/dev.campus.systemische-tools.de\/src\/Infrastructure\/Formatter\/ApiResponseFormatter.php",
"oldString": "<?php\n\nnamespace Infrastructure\\Formatter;\n\n\/**\n * Formats API responses with a consistent structure.\n *\n * Success response:\n * {\n * \"success\": true,\n * \"data\": {...},\n * \"message\": \"optional message\",\n * \"meta\": {\"timestamp\": \"...\", \"count\": N}\n * }\n *\n * Error response:\n * {\n * \"success\": false,\n * \"error\": \"error message\",\n * \"code\": \"ERROR_CODE\",\n * \"details\": {...}\n * }\n *\/\nfinal class ApiResponseFormatter",
"newString": "<?php\n\ndeclare(strict_types=1);\n\nnamespace Infrastructure\\Formatter;\n\n\/\/ @responsibility: Formatiert API-Responses mit einheitlicher Struktur\n\nfinal class ApiResponseFormatter",
"originalFile": "<?php\n\nnamespace Infrastructure\\Formatter;\n\n\/**\n * Formats API responses with a consistent structure.\n *\n * Success response:\n * {\n * \"success\": true,\n * \"data\": {...},\n * \"message\": \"optional message\",\n * \"meta\": {\"timestamp\": \"...\", \"count\": N}\n * }\n *\n * Error response:\n * {\n * \"success\": false,\n * \"error\": \"error message\",\n * \"code\": \"ERROR_CODE\",\n * \"details\": {...}\n * }\n *\/\nfinal class ApiResponseFormatter\n{\n \/**\n * Format a success response.\n *\n * @param mixed $data The response data\n * @param string|null $message Optional success message\n * @param array<string, mixed> $meta Optional metadata (pagination, counts, etc.)\n * @return array<string, mixed>\n *\/\n public function success(mixed $data, ?string $message = null, array $meta = []): array\n {\n $response = [\n 'success' => true,\n 'data' => $data,\n ];\n\n if ($message !== null) {\n $response['message'] = $message;\n }\n\n if ($meta !== []) {\n $response['meta'] = $meta;\n }\n\n return $response;\n }\n\n \/**\n * Format a list response with pagination metadata.\n *\n * @param array<mixed> $items The list items\n * @param int $total Total count (for pagination)\n * @param int $page Current page\n * @param int $limit Items per page\n * @return array<string, mixed>\n *\/\n public function list(array $items, int $total, int $page = 1, int $limit = 10): array\n {\n return $this->success($items, null, [\n 'total' => $total,\n 'page' => $page,\n 'limit' => $limit,\n 'pages' => (int) ceil($total \/ max(1, $limit)),\n ]);\n }\n\n \/**\n * Format an error response.\n *\n * @param string $message Error message\n * @param string|null $code Machine-readable error code\n * @param array<string, mixed>|null $details Additional error details\n * @return array<string, mixed>\n *\/\n public function error(string $message, ?string $code = null, ?array $details = null): array\n {\n $response = [\n 'success' => false,\n 'error' => $message,\n ];\n\n if ($code !== null) {\n $response['code'] = $code;\n }\n\n if ($details !== null) {\n $response['details'] = $details;\n }\n\n return $response;\n }\n\n \/**\n * Format a not found error.\n *\n * @param string $resource The resource type that was not found\n * @return array<string, mixed>\n *\/\n public function notFound(string $resource = 'Resource'): array\n {\n return $this->error(\"{$resource} nicht gefunden\", 'NOT_FOUND');\n }\n\n \/**\n * Format a validation error.\n *\n * @param string $message Error message\n * @param array<string, string> $fieldErrors Field-specific errors\n * @return array<string, mixed>\n *\/\n public function validationError(string $message, array $fieldErrors = []): array\n {\n return $this->error($message, 'VALIDATION_ERROR', $fieldErrors !== [] ? ['fields' => $fieldErrors] : null);\n }\n\n \/**\n * Format a created response with the new resource ID.\n *\n * @param int|string $id The created resource ID\n * @param string|null $message Optional success message\n * @return array<string, mixed>\n *\/\n public function created(int|string $id, ?string $message = null): array\n {\n return $this->success(['id' => $id], $message ?? 'Erfolgreich erstellt');\n }\n\n \/**\n * Format a deleted\/updated confirmation.\n *\n * @param string|null $message Optional message\n * @return array<string, mixed>\n *\/\n public function ok(?string $message = null): array\n {\n return $this->success(null, $message);\n }\n}\n",
"structuredPatch": [
{
"oldStart": 1,
"oldLines": 26,
"newStart": 1,
"newLines": 11,
"lines": [
" <?php",
" ",
"+declare(strict_types=1);",
"+",
" namespace Infrastructure\\Formatter;",
" ",
"-\/**",
"- * Formats API responses with a consistent structure.",
"- *",
"- * Success response:",
"- * {",
"- * \"success\": true,",
"- * \"data\": {...},",
"- * \"message\": \"optional message\",",
"- * \"meta\": {\"timestamp\": \"...\", \"count\": N}",
"- * }",
"- *",
"- * Error response:",
"- * {",
"- * \"success\": false,",
"- * \"error\": \"error message\",",
"- * \"code\": \"ERROR_CODE\",",
"- * \"details\": {...}",
"- * }",
"- *\/",
"+\/\/ @responsibility: Formatiert API-Responses mit einheitlicher Struktur",
"+",
" final class ApiResponseFormatter",
" {",
" \/**"
]
}
],
"userModified": false,
"replaceAll": false
}
}