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
|
|
|
* @param int $json_options @link http://php.net/manual/en/function.json-encode.php |
27
|
|
|
* @return ResponseFactory |
28
|
|
|
*/ |
29
|
|
|
public function withArray(array $array, array $headers = [], $json_options = 0) |
30
|
|
|
{ |
31
|
|
|
return response()->json($array, $this->statusCode, $headers, $json_options); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Respond with a paginator, and a transformer. |
36
|
|
|
* |
37
|
|
|
* @param LengthAwarePaginator $paginator |
38
|
|
|
* @param callable|\League\Fractal\TransformerAbstract $transformer |
39
|
|
|
* @param string $resourceKey |
40
|
|
|
* @param array $meta |
41
|
|
|
* @return ResponseFactory |
42
|
|
|
*/ |
43
|
1 |
|
public function withPaginator(LengthAwarePaginator $paginator, $transformer, $resourceKey = null, $meta = []) |
44
|
|
|
{ |
45
|
1 |
|
$queryParams = array_diff_key($_GET, array_flip(['page'])); |
46
|
1 |
|
$paginator->appends($queryParams); |
47
|
|
|
|
48
|
1 |
|
$resource = new Collection($paginator->items(), $transformer, $resourceKey); |
49
|
1 |
|
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); |
50
|
|
|
|
51
|
1 |
|
foreach ($meta as $metaKey => $metaValue) { |
52
|
1 |
|
$resource->setMetaValue($metaKey, $metaValue); |
53
|
1 |
|
} |
54
|
|
|
|
55
|
1 |
|
$rootScope = $this->manager->createData($resource); |
56
|
|
|
|
57
|
1 |
|
return $this->withArray($rootScope->toArray()); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Generates a Response with a 400 HTTP header and a given message from validator |
62
|
|
|
* |
63
|
|
|
* @param Validator $validator |
64
|
|
|
* @return ResponseFactory |
65
|
|
|
*/ |
66
|
1 |
|
public function errorWrongArgsValidator(Validator $validator) |
67
|
|
|
{ |
68
|
1 |
|
return $this->errorWrongArgs($validator->getMessageBag()->toArray()); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.