ApiResponseFormatter.php
- Pfad:
src/Infrastructure/Formatter/ApiResponseFormatter.php - Namespace: Infrastructure\Formatter
- Zeilen: 128 | Größe: 3,541 Bytes
- Geändert: 2025-12-23 08:06:27 | Gescannt: 2025-12-31 10:22:15
Code Hygiene Score: 100
- Dependencies: 100 (25%)
- LOC: 100 (20%)
- Methods: 100 (20%)
- Secrets: 100 (15%)
- Classes: 100 (10%)
- Magic Numbers: 100 (10%)
Keine Issues gefunden.
Klassen 1
-
ApiResponseFormatterclass Zeile 9
Funktionen 6
-
success()public Zeile 19 -
error()public Zeile 64 -
notFound()public Zeile 88 -
validationError()Zeile 100 -
created()Zeile 112 -
ok()Zeile 123
Verwendet von 9
- EntityController.php constructor
- EntityController.php use
- InfrastructureServiceProvider.php use
- OntologyController.php constructor
- OntologyController.php use
- RelationController.php constructor
- RelationController.php use
- TaxonomyController.php constructor
- TaxonomyController.php use
Versionen 1
-
v1
2025-12-23 08:06 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation
Code
<?php
declare(strict_types=1);
namespace Infrastructure\Formatter;
// @responsibility: Formatiert API-Responses mit einheitlicher Struktur
final class ApiResponseFormatter
{
/**
* Format a success response.
*
* @param mixed $data The response data
* @param string|null $message Optional success message
* @param array<string, mixed> $meta Optional metadata (pagination, counts, etc.)
* @return array<string, mixed>
*/
public function success(mixed $data, ?string $message = null, array $meta = []): array
{
$response = [
'success' => true,
'data' => $data,
];
if ($message !== null) {
$response['message'] = $message;
}
if ($meta !== []) {
$response['meta'] = $meta;
}
return $response;
}
/**
* Format a list response with pagination metadata.
*
* @param array<mixed> $items The list items
* @param int $total Total count (for pagination)
* @param int $page Current page
* @param int $limit Items per page
* @return array<string, mixed>
*/
public function list(array $items, int $total, int $page = 1, int $limit = 10): array
{
return $this->success($items, null, [
'total' => $total,
'page' => $page,
'limit' => $limit,
'pages' => (int) ceil($total / max(1, $limit)),
]);
}
/**
* Format an error response.
*
* @param string $message Error message
* @param string|null $code Machine-readable error code
* @param array<string, mixed>|null $details Additional error details
* @return array<string, mixed>
*/
public function error(string $message, ?string $code = null, ?array $details = null): array
{
$response = [
'success' => false,
'error' => $message,
];
if ($code !== null) {
$response['code'] = $code;
}
if ($details !== null) {
$response['details'] = $details;
}
return $response;
}
/**
* Format a not found error.
*
* @param string $resource The resource type that was not found
* @return array<string, mixed>
*/
public function notFound(string $resource = 'Resource'): array
{
return $this->error("{$resource} nicht gefunden", 'NOT_FOUND');
}
/**
* Format a validation error.
*
* @param string $message Error message
* @param array<string, string> $fieldErrors Field-specific errors
* @return array<string, mixed>
*/
public function validationError(string $message, array $fieldErrors = []): array
{
return $this->error($message, 'VALIDATION_ERROR', $fieldErrors !== [] ? ['fields' => $fieldErrors] : null);
}
/**
* Format a created response with the new resource ID.
*
* @param int|string $id The created resource ID
* @param string|null $message Optional success message
* @return array<string, mixed>
*/
public function created(int|string $id, ?string $message = null): array
{
return $this->success(['id' => $id], $message ?? 'Erfolgreich erstellt');
}
/**
* Format a deleted/updated confirmation.
*
* @param string|null $message Optional message
* @return array<string, mixed>
*/
public function ok(?string $message = null): array
{
return $this->success(null, $message);
}
}