|
8 | 8 |
|
9 | 9 | namespace PhpBootstrap\Services;
|
10 | 10 |
|
| 11 | +use League\Fractal\Manager; |
| 12 | +use League\Fractal\Pagination\Cursor; |
| 13 | +use League\Fractal\Resource\Collection; |
| 14 | +use League\Fractal\Resource\Item; |
| 15 | +use League\Fractal\TransformerAbstract; |
11 | 16 | use Zend\Diactoros\MessageTrait;
|
12 | 17 | use InvalidArgumentException;
|
13 | 18 |
|
@@ -147,10 +152,79 @@ public function withStatus($code, $reasonPhrase = '')
|
147 | 152 | return $new;
|
148 | 153 | }
|
149 | 154 |
|
| 155 | + /** |
| 156 | + * @param $data |
| 157 | + * @param $code |
| 158 | + * @param array $headers |
| 159 | + * @return Response|static |
| 160 | + */ |
| 161 | + public function withArray(array $data, $code = 200, array $headers = []) |
| 162 | + { |
| 163 | + $new = clone $this; |
| 164 | + $new->setStatusCode($code); |
| 165 | + $new->getBody()->write(json_encode([$data])); |
| 166 | + $new = $new->withHeader('Content-Type', 'application/json'); |
| 167 | + $new->headers = array_merge($new->headers, $headers); |
| 168 | + return $new; |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * @param $data |
| 173 | + * @param TransformerAbstract|callable $transformer |
| 174 | + * @param int $code |
| 175 | + * @param null $resourceKey |
| 176 | + * @param array $meta |
| 177 | + * @param array $headers |
| 178 | + * @return Response |
| 179 | + */ |
| 180 | + public function withItem($data, $transformer, $code = 200, $resourceKey = null, $meta = [], array $headers = []) |
| 181 | + { |
| 182 | + $resource = new Item($data, $transformer, $resourceKey); |
| 183 | + |
| 184 | + foreach ($meta as $metaKey => $metaValue) { |
| 185 | + $resource->setMetaValue($metaKey, $metaValue); |
| 186 | + } |
| 187 | + |
| 188 | + $manager = new Manager(); |
| 189 | + |
| 190 | + $rootScope = $manager->createData($resource); |
| 191 | + |
| 192 | + return $this->withArray($rootScope->toArray(), $code, $headers); |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * @param $data |
| 197 | + * @param TransformerAbstract|callable $transformer |
| 198 | + * @param int $code |
| 199 | + * @param null $resourceKey |
| 200 | + * @param Cursor|null $cursor |
| 201 | + * @param array $meta |
| 202 | + * @param array $headers |
| 203 | + * @return Response |
| 204 | + */ |
| 205 | + public function withCollection($data, $transformer, $code = 200, $resourceKey = null, Cursor $cursor = null, $meta = [], array $headers = []) |
| 206 | + { |
| 207 | + $resource = new Collection($data, $transformer, $resourceKey); |
| 208 | + |
| 209 | + foreach ($meta as $metaKey => $metaValue) { |
| 210 | + $resource->setMetaValue($metaKey, $metaValue); |
| 211 | + } |
| 212 | + |
| 213 | + if (!is_null($cursor)) { |
| 214 | + $resource->setCursor($cursor); |
| 215 | + } |
| 216 | + |
| 217 | + $manager = new Manager(); |
| 218 | + |
| 219 | + $rootScope = $manager->createData($resource); |
| 220 | + |
| 221 | + return $this->withArray($rootScope->toArray(), $code, $headers); |
| 222 | + } |
| 223 | + |
150 | 224 | /**
|
151 | 225 | * Response for errors
|
152 | 226 | *
|
153 |
| - * @param string $message |
| 227 | + * @param string|array $message |
154 | 228 | * @param string $code
|
155 | 229 | * @param array $headers
|
156 | 230 | * @return mixed
|
@@ -211,6 +285,78 @@ public function errorNotFound($message = '', array $headers = [])
|
211 | 285 | return $this->withError($message, 404, $headers);
|
212 | 286 | }
|
213 | 287 |
|
| 288 | + /** |
| 289 | + * Generates a response with a 401 HTTP header and a given message. |
| 290 | + * |
| 291 | + * @param string $message |
| 292 | + * @param array $headers |
| 293 | + * @return mixed |
| 294 | + */ |
| 295 | + public function errorUnauthorized($message = '', array $headers = []) |
| 296 | + { |
| 297 | + return $this->withError($message, 401, $headers); |
| 298 | + } |
| 299 | + |
| 300 | + /** |
| 301 | + * Generates a response with a 400 HTTP header and a given message. |
| 302 | + * |
| 303 | + * @param array $message |
| 304 | + * @param array $headers |
| 305 | + * @return mixed |
| 306 | + */ |
| 307 | + public function errorWrongArgs(array $message, array $headers = []) |
| 308 | + { |
| 309 | + return $this->withError($message, 400, $headers); |
| 310 | + } |
| 311 | + |
| 312 | + /** |
| 313 | + * Generates a response with a 410 HTTP header and a given message. |
| 314 | + * |
| 315 | + * @param string $message |
| 316 | + * @param array $headers |
| 317 | + * @return mixed |
| 318 | + */ |
| 319 | + public function errorGone($message = '', array $headers = []) |
| 320 | + { |
| 321 | + return $this->withError($message, 410, $headers); |
| 322 | + } |
| 323 | + |
| 324 | + /** |
| 325 | + * Generates a response with a 405 HTTP header and a given message. |
| 326 | + * |
| 327 | + * @param string $message |
| 328 | + * @param array $headers |
| 329 | + * @return mixed |
| 330 | + */ |
| 331 | + public function errorMethodNotAllowed($message = '', array $headers = []) |
| 332 | + { |
| 333 | + return $this->withError($message, 405, $headers); |
| 334 | + } |
| 335 | + |
| 336 | + /** |
| 337 | + * Generates a Response with a 431 HTTP header and a given message. |
| 338 | + * |
| 339 | + * @param string $message |
| 340 | + * @param array $headers |
| 341 | + * @return mixed |
| 342 | + */ |
| 343 | + public function errorUnwillingToProcess($message = '', array $headers = []) |
| 344 | + { |
| 345 | + return $this->withError($message, 431, $headers); |
| 346 | + } |
| 347 | + |
| 348 | + /** |
| 349 | + * Generates a Response with a 422 HTTP header and a given message. |
| 350 | + * |
| 351 | + * @param string $message |
| 352 | + * @param array $headers |
| 353 | + * @return mixed |
| 354 | + */ |
| 355 | + public function errorUnprocessable($message = '', array $headers = []) |
| 356 | + { |
| 357 | + return $this->withError($message, 422, $headers); |
| 358 | + } |
| 359 | + |
214 | 360 | /**
|
215 | 361 | * Set a valid status code.
|
216 | 362 | *
|
|
0 commit comments