Lines
100.00%
26 / 26
Methods
100.00%
3 / 3
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| success | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 2 | ||
| error | 100.00% | 10 / 10 | 100.00% | 1 / 1 | 2 | ||
| buildMetadata | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 7 | ||
| 32 | class ApiResponse | |
| 33 | { | |
| 34 | /** | |
| 35 | * Creates a successful API response with data and optional metadata | |
| 36 | * | |
| 37 | * @param array|object $data The response data | |
| 38 | * @param PaginationMetadata|null $pagination Optional pagination metadata | |
| 39 | * @param SortRequest|null $sort Optional sorting information | |
| 40 | * @param FilterRequest|null $filters Optional filtering information | |
| 41 | * @return array Standardized response array | |
| 42 | */ | |
| 43 | public static function success( | |
| 44 | array|object $data, | |
| 45 | ?PaginationMetadata $pagination = null, | |
| 46 | ?SortRequest $sort = null, | |
| 47 | ?FilterRequest $filters = null, | |
| 48 | ): array { | |
| 49 | $response = [ | |
| 50 | 'success' => true, | |
| 51 | 'data' => $data, | |
| 52 | ]; | |
| 53 | ||
| 54 | // Build metadata if any is present | |
| 55 | $meta = self::buildMetadata($pagination, $sort, $filters); | |
| 56 | if ($meta !== null) { | |
| 57 | $response['meta'] = $meta; | |
| 58 | } | |
| 59 | ||
| 60 | return $response; | |
| 61 | } | |
| 62 | ||
| 63 | /** | |
| 64 | * Creates an error API response | |
| 65 | * | |
| 66 | * @param string $message Error message | |
| 67 | * @param string $code Error code | |
| 68 | * @param array|null $details Optional error details | |
| 69 | * @return array Standardized error response array | |
| 70 | */ | |
| 71 | public static function error(string $message, string $code = 'ERROR', ?array $details = null): array | |
| 72 | { | |
| 73 | $response = [ | |
| 74 | 'success' => false, | |
| 75 | 'error' => [ | |
| 76 | 'code' => $code, | |
| 77 | 'message' => $message, | |
| 78 | ], | |
| 79 | ]; | |
| 80 | ||
| 81 | if ($details !== null) { | |
| 82 | $response['error']['details'] = $details; | |
| 83 | } | |
| 84 | ||
| 85 | return $response; | |
| 86 | } | |
| 87 | ||
| 88 | /** | |
| 89 | * Builds the metadata section of the response | |
| 90 | * | |
| 91 | * @param PaginationMetadata|null $pagination Pagination metadata | |
| 92 | * @param SortRequest|null $sort Sort information | |
| 93 | * @param FilterRequest|null $filters Filter information | |
| 94 | * @return array|null Metadata array or null if no metadata | |
| 95 | */ | |
| 96 | private static function buildMetadata( | |
| 97 | ?PaginationMetadata $pagination, | |
| 98 | ?SortRequest $sort, | |
| 99 | ?FilterRequest $filters, | |
| 100 | ): ?array { | |
| 101 | $meta = []; | |
| 102 | ||
| 103 | if ($pagination !== null) { | |
| 104 | $meta['pagination'] = $pagination->toArray(); | |
| 105 | } | |
| 106 | ||
| 107 | if ($sort !== null && $sort->hasSort()) { | |
| 108 | $meta['sorting'] = $sort->toArray(); | |
| 109 | } | |
| 110 | ||
| 111 | if ($filters !== null && $filters->hasFilters()) { | |
| 112 | $meta['filters'] = $filters->toArray(); | |
| 113 | } | |
| 114 | ||
| 115 | return $meta === [] ? null : $meta; | |
| 116 | } | |
| 117 | } |