|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EllipseSynergie\ApiResponse\Laravel; |
|
4
|
|
|
|
|
5
|
|
|
use EllipseSynergie\ApiResponse\AbstractResponse; |
|
6
|
|
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter; |
|
7
|
|
|
use Illuminate\Contracts\Routing\ResponseFactory; |
|
8
|
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator; |
|
9
|
|
|
use Illuminate\Contracts\Validation\Validator; |
|
10
|
|
|
use League\Fractal\Resource\Collection; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class Response |
|
14
|
|
|
* |
|
15
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
16
|
|
|
* file that was distributed with this source code. |
|
17
|
|
|
* |
|
18
|
|
|
* @package EllipseSynergie\ApiResponse\Laravel |
|
19
|
|
|
* @author Maxime Beaudoin <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class Response extends AbstractResponse |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @param array $array |
|
25
|
|
|
* @param array $headers |
|
26
|
|
|
* @return ResponseFactory |
|
27
|
|
|
*/ |
|
28
|
|
|
public function withArray(array $array, array $headers = []) |
|
29
|
|
|
{ |
|
30
|
|
|
return response()->json($array, $this->statusCode, $headers); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Respond with a paginator, and a transformer. |
|
35
|
|
|
* |
|
36
|
|
|
* @param LengthAwarePaginator $paginator |
|
37
|
|
|
* @param callable|\League\Fractal\TransformerAbstract $transformer |
|
38
|
|
|
* @param string $resourceKey |
|
39
|
|
|
* @param array $meta |
|
40
|
|
|
* @return ResponseFactory |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public function withPaginator(LengthAwarePaginator $paginator, $transformer, $resourceKey = null, $meta = []) |
|
|
|
|
|
|
43
|
|
|
{ |
|
44
|
1 |
|
$queryParams = array_diff_key($_GET, array_flip(['page'])); |
|
45
|
1 |
|
$paginator->appends($queryParams); |
|
46
|
|
|
|
|
47
|
|
|
$resource = new Collection($paginator->items(), $transformer, $resourceKey); |
|
48
|
|
|
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); |
|
49
|
|
|
|
|
50
|
|
|
foreach ($meta as $metaKey => $metaValue) { |
|
51
|
|
|
$resource->setMetaValue($metaKey, $metaValue); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$rootScope = $this->manager->createData($resource); |
|
55
|
|
|
|
|
56
|
|
|
return $this->withArray($rootScope->toArray()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Generates a Response with a 400 HTTP header and a given message from validator |
|
61
|
|
|
* |
|
62
|
|
|
* @param Validator $validator |
|
63
|
|
|
* @return ResponseFactory |
|
64
|
|
|
*/ |
|
65
|
1 |
|
public function errorWrongArgsValidator(Validator $validator) |
|
66
|
|
|
{ |
|
67
|
1 |
|
return $this->errorWrongArgs($validator->getMessageBag()->toArray()); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: