|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rexlabs\Smokescreen\Serializer; |
|
4
|
|
|
|
|
5
|
|
|
use Rexlabs\Smokescreen\Pagination\CursorInterface; |
|
6
|
|
|
use Rexlabs\Smokescreen\Pagination\PaginatorInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* The default serializer: |
|
10
|
|
|
* - Returns collections nested under a "data" key |
|
11
|
|
|
* - Returns items without any nesting. |
|
12
|
|
|
*/ |
|
13
|
|
|
class DefaultSerializer implements SerializerInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Serialize a collection. |
|
17
|
|
|
* The data will be nested under a "data" key. |
|
18
|
|
|
* |
|
19
|
|
|
* @param string $resourceKey |
|
20
|
|
|
* @param array $data |
|
21
|
|
|
* |
|
22
|
|
|
* @return array |
|
23
|
|
|
*/ |
|
24
|
3 |
|
public function collection($resourceKey, array $data): array |
|
25
|
|
|
{ |
|
26
|
3 |
|
return ['data' => $data]; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Serialize an item. |
|
31
|
|
|
* The item data will be returned as-is. (not nested). |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $resourceKey |
|
34
|
|
|
* @param array $data |
|
35
|
|
|
* |
|
36
|
|
|
* @return array |
|
37
|
|
|
*/ |
|
38
|
11 |
|
public function item($resourceKey, array $data): array |
|
39
|
|
|
{ |
|
40
|
11 |
|
return $data; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Serialize a null collection. |
|
45
|
|
|
* |
|
46
|
|
|
* @return mixed |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function nullCollection() |
|
49
|
|
|
{ |
|
50
|
1 |
|
return []; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Serialize a null item. |
|
55
|
|
|
* |
|
56
|
|
|
* @return mixed |
|
57
|
|
|
*/ |
|
58
|
2 |
|
public function nullItem() |
|
59
|
|
|
{ |
|
60
|
2 |
|
return null; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Serialize the paginator. |
|
65
|
|
|
* |
|
66
|
|
|
* @param PaginatorInterface $paginator |
|
67
|
|
|
* |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function paginator(PaginatorInterface $paginator) |
|
71
|
|
|
{ |
|
72
|
1 |
|
$currentPage = $paginator->getCurrentPage(); |
|
73
|
1 |
|
$lastPage = $paginator->getLastPage(); |
|
74
|
|
|
|
|
75
|
|
|
$pagination = [ |
|
76
|
1 |
|
'total' => $paginator->getTotal(), |
|
77
|
1 |
|
'count' => $paginator->getCount(), |
|
78
|
1 |
|
'per_page' => $paginator->getPerPage(), |
|
79
|
1 |
|
'current_page' => $currentPage, |
|
80
|
1 |
|
'total_pages' => $lastPage, |
|
81
|
|
|
]; |
|
82
|
|
|
|
|
83
|
1 |
|
$pagination['links'] = []; |
|
84
|
|
|
|
|
85
|
1 |
|
if ($currentPage > 1) { |
|
86
|
1 |
|
$pagination['links']['previous'] = $paginator->getUrl($currentPage - 1); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
if ($currentPage < $lastPage) { |
|
90
|
1 |
|
$pagination['links']['next'] = $paginator->getUrl($currentPage + 1); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
return ['pagination' => $pagination]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Serialize the cursor. |
|
98
|
|
|
* |
|
99
|
|
|
* @param CursorInterface $cursor |
|
100
|
|
|
* |
|
101
|
|
|
* @return array |
|
102
|
|
|
*/ |
|
103
|
1 |
|
public function cursor(CursorInterface $cursor): array |
|
104
|
|
|
{ |
|
105
|
|
|
return [ |
|
106
|
|
|
'cursor' => [ |
|
107
|
1 |
|
'current' => $cursor->getCurrent(), |
|
108
|
1 |
|
'prev' => $cursor->getPrev(), |
|
109
|
1 |
|
'next' => $cursor->getNext(), |
|
110
|
1 |
|
'count' => (int) $cursor->getCount(), |
|
111
|
|
|
], |
|
112
|
|
|
]; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|