1 | <?php |
||
25 | class Responder implements ResponderContract |
||
26 | { |
||
27 | /** |
||
28 | * Generate a successful JSON response. |
||
29 | * |
||
30 | * @param mixed $data |
||
31 | * @param int $statusCode |
||
32 | * @return JsonResponse |
||
33 | */ |
||
34 | public function success( $data = null, int $statusCode = 200 ):JsonResponse |
||
51 | |||
52 | /** |
||
53 | * Generate an error JSON response. |
||
54 | * |
||
55 | * @param string $errorCode |
||
56 | * @param int $statusCode |
||
57 | * @param mixed $message |
||
58 | * @return JsonResponse |
||
59 | */ |
||
60 | public function error( string $errorCode, int $statusCode = 500, $message = null ):JsonResponse |
||
73 | |||
74 | /** |
||
75 | * Transforms the data using Fractal. |
||
76 | * |
||
77 | * @param mixed $data |
||
78 | * @param Transformer|null $transformer |
||
79 | * @return ResourceInterface |
||
80 | */ |
||
81 | public function transform( $data = null, Transformer $transformer = null ):ResourceInterface |
||
95 | |||
96 | /** |
||
97 | * Transform a transformable Eloquent model. |
||
98 | * |
||
99 | * @param Transformable $model |
||
100 | * @param Transformer|null $transformer |
||
101 | * @return FractalItem |
||
102 | */ |
||
103 | protected function transformModel( Transformable $model, Transformer $transformer = null ):FractalItem |
||
109 | |||
110 | /** |
||
111 | * Transform a collection of Eloquent models. |
||
112 | * |
||
113 | * @param Collection $collection |
||
114 | * @param Transformer|null $transformer |
||
115 | * @return FractalCollection |
||
116 | */ |
||
117 | protected function transformCollection( Collection $collection, Transformer $transformer = null ):FractalCollection |
||
124 | |||
125 | /** |
||
126 | * Transform paginated data using Laravel's paginator. |
||
127 | * |
||
128 | * @param LengthAwarePaginator $paginator |
||
129 | * @param Transformer|null $transformer |
||
130 | * @return FractalCollection |
||
131 | */ |
||
132 | protected function transformPaginator( LengthAwarePaginator $paginator, Transformer $transformer = null ):FractalCollection |
||
139 | |||
140 | /** |
||
141 | * Transform the data using the given transformer. |
||
142 | * |
||
143 | * @param Transformable|Collection $data |
||
144 | * @param Transformer|null $transformer |
||
145 | * @param string $resourceKey |
||
146 | * @return ResourceInterface |
||
147 | */ |
||
148 | protected function transformData( $data, Transformer $transformer, string $resourceKey ):ResourceInterface |
||
156 | |||
157 | /** |
||
158 | * Resolves model class path from a collection of models. |
||
159 | * |
||
160 | * @param Collection $collection |
||
161 | * @return Transformable |
||
162 | * @throws InvalidArgumentException |
||
163 | */ |
||
164 | protected function resolveModel( Collection $collection ):Transformable |
||
180 | |||
181 | public function serialize( ResourceInterface $resource ):array |
||
196 | |||
197 | /** |
||
198 | * Get the skeleton for an error response. |
||
199 | * |
||
200 | * @param string $errorCode |
||
201 | * @param int $statusCode |
||
202 | * @return array |
||
203 | */ |
||
204 | protected function getErrorResponse( string $errorCode, int $statusCode ):array |
||
214 | |||
215 | /** |
||
216 | * Get any error messages for the response. If no message can be found it will |
||
217 | * try to resolve a set message from the translator. |
||
218 | * |
||
219 | * @param mixed $message |
||
220 | * @param string $errorCode |
||
221 | * @return array |
||
222 | */ |
||
223 | protected function getErrorMessages( $message, string $errorCode ):array |
||
238 | } |