1 | <?php |
||
26 | class Responder implements ResponderContract |
||
27 | { |
||
28 | /** |
||
29 | * Generate a successful JSON response. |
||
30 | * |
||
31 | * @param mixed $data |
||
32 | * @param int $statusCode |
||
33 | * @return JsonResponse |
||
34 | */ |
||
35 | public function success( $data = null, int $statusCode = 200 ):JsonResponse |
||
47 | |||
48 | /** |
||
49 | * Generate an unsuccessful JSON response. |
||
50 | * |
||
51 | * @param string $errorCode |
||
52 | * @param int $statusCode |
||
53 | * @param mixed $message |
||
54 | * @return JsonResponse |
||
55 | */ |
||
56 | public function error( string $errorCode, int $statusCode = 500, $message = null ):JsonResponse |
||
69 | |||
70 | /** |
||
71 | * Transforms the data. |
||
72 | * |
||
73 | * @param mixed $data |
||
74 | * @param Transformer|null $transformer |
||
75 | * @return ResourceInterface |
||
76 | */ |
||
77 | public function transform( $data = null, Transformer $transformer = null ):ResourceInterface |
||
91 | |||
92 | /** |
||
93 | * Serializes the data. |
||
94 | * |
||
95 | * @param ResourceInterface $resource |
||
96 | * @return array |
||
97 | */ |
||
98 | public function serialize( ResourceInterface $resource ):array |
||
113 | |||
114 | /** |
||
115 | * Transform a transformable Eloquent model. |
||
116 | * |
||
117 | * @param Transformable $model |
||
118 | * @param Transformer|null $transformer |
||
119 | * @return FractalItem |
||
120 | */ |
||
121 | protected function transformModel( Transformable $model, Transformer $transformer = null ):FractalItem |
||
127 | |||
128 | /** |
||
129 | * Transform a collection of Eloquent models. |
||
130 | * |
||
131 | * @param Collection $collection |
||
132 | * @param Transformer|null $transformer |
||
133 | * @return FractalCollection |
||
134 | */ |
||
135 | protected function transformCollection( Collection $collection, Transformer $transformer = null ):FractalCollection |
||
142 | |||
143 | /** |
||
144 | * Transform paginated data using Laravel's paginator. |
||
145 | * |
||
146 | * @param LengthAwarePaginator $paginator |
||
147 | * @param Transformer|null $transformer |
||
148 | * @return FractalCollection |
||
149 | */ |
||
150 | protected function transformPaginator( LengthAwarePaginator $paginator, Transformer $transformer = null ):FractalCollection |
||
157 | |||
158 | /** |
||
159 | * Transform the data using the given transformer. |
||
160 | * |
||
161 | * @param Transformable|Collection $data |
||
162 | * @param Transformer|null $transformer |
||
163 | * @param string $resourceKey |
||
164 | * @return ResourceInterface |
||
165 | */ |
||
166 | protected function transformData( $data, Transformer $transformer, string $resourceKey ):ResourceInterface |
||
174 | |||
175 | /** |
||
176 | * Resolves model class path from a collection of models. |
||
177 | * |
||
178 | * @param Collection $collection |
||
179 | * @return Transformable |
||
180 | * @throws InvalidArgumentException |
||
181 | */ |
||
182 | protected function resolveModel( Collection $collection ):Transformable |
||
198 | |||
199 | /** |
||
200 | * Here we prepend a status code to the response data, if status code is enabled in |
||
201 | * the configuration file. |
||
202 | * |
||
203 | * @param int $statusCode |
||
204 | * @param array $data |
||
205 | * @return array |
||
206 | */ |
||
207 | protected function includeStatusCode( int $statusCode, array $data ):array |
||
217 | |||
218 | /** |
||
219 | * Get the skeleton for an error response. |
||
220 | * |
||
221 | * @param string $errorCode |
||
222 | * @param int $statusCode |
||
223 | * @return array |
||
224 | */ |
||
225 | protected function getErrorResponse( string $errorCode, int $statusCode ):array |
||
236 | |||
237 | /** |
||
238 | * Get any error messages for the response. If no message can be found it will |
||
239 | * try to resolve a set message from the translator. |
||
240 | * |
||
241 | * @param mixed $message |
||
242 | * @param string $errorCode |
||
243 | * @return array |
||
244 | */ |
||
245 | protected function getErrorMessages( $message, string $errorCode ):array |
||
260 | } |