1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tequilarapido\ApiResponse; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use League\Fractal\Manager; |
8
|
|
|
use League\Fractal\Resource\Collection; |
9
|
|
|
use League\Fractal\Resource\Item; |
10
|
|
|
use League\Fractal\Serializer\SerializerAbstract; |
11
|
|
|
|
12
|
|
|
class FractalAdapter |
13
|
|
|
{ |
14
|
|
|
/** @var \League\Fractal\Manager */ |
15
|
|
|
protected $manager; |
16
|
|
|
|
17
|
|
|
/** @var Request */ |
18
|
|
|
private $request; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* FractalAdapter constructor. |
22
|
|
|
* |
23
|
|
|
* @param SerializerAbstract $serializer |
24
|
|
|
* @param Request $request |
25
|
|
|
*/ |
26
|
|
|
public function __construct(SerializerAbstract $serializer, Request $request) |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
$this->manager = new Manager(); |
29
|
|
|
$this->manager->setSerializer($serializer); |
30
|
|
|
|
31
|
|
|
$this->request = $request; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Return an item. |
36
|
|
|
* |
37
|
|
|
* @param $data |
38
|
|
|
* @param null $transformer |
39
|
|
|
* @param null $resourceKey |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
public function item($data, $transformer = null, $resourceKey = null) |
43
|
|
|
{ |
44
|
|
|
$resource = new Item($data, $this->getTransformer($transformer), $resourceKey); |
45
|
|
|
|
46
|
|
|
return $this->manager->createData($resource)->toArray(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Return a collection of items. |
51
|
|
|
* |
52
|
|
|
* @param $data |
53
|
|
|
* @param null $transformer |
54
|
|
|
* @param null $resourceKey |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
public function collection($data, $transformer = null, $resourceKey = null) |
58
|
|
|
{ |
59
|
|
|
$resource = new Collection($data, $this->getTransformer($transformer), $resourceKey); |
60
|
|
|
|
61
|
|
|
return $this->manager->createData($resource)->toArray(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param LengthAwarePaginator $paginator |
66
|
|
|
* @param null $transformer |
67
|
|
|
* @param null $resourceKey |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
public function paginatedCollection(LengthAwarePaginator $paginator, $transformer = null, $resourceKey = null) |
71
|
|
|
{ |
72
|
|
|
$paginator->appends($this->request->query()); |
73
|
|
|
|
74
|
|
|
$resource = new Collection($paginator->items(), $this->getTransformer($transformer), $resourceKey); |
75
|
|
|
|
76
|
|
|
$resource->setPaginator(new PaginatorAdapter($paginator)); |
77
|
|
|
|
78
|
|
|
return $this->manager->createData($resource)->toArray(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Return transformer. |
83
|
|
|
* |
84
|
|
|
* @param $transformer |
85
|
|
|
* @return \Closure |
86
|
|
|
*/ |
87
|
|
|
protected function getTransformer($transformer) |
88
|
|
|
{ |
89
|
|
|
return $transformer ?: function ($data) { |
90
|
|
|
|
91
|
|
|
if ($data instanceof Arrayable) { |
|
|
|
|
92
|
|
|
return $data->toArray(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return (array)$data; |
96
|
|
|
}; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|