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