1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the League\Fractal package. |
5
|
|
|
* |
6
|
|
|
* (c) Phil Sturgeon <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace League\Fractal\Serializer; |
13
|
|
|
|
14
|
|
|
use League\Fractal\Pagination\CursorInterface; |
15
|
|
|
use League\Fractal\Pagination\PaginatorInterface; |
16
|
|
|
use League\Fractal\Resource\ResourceInterface; |
17
|
|
|
|
18
|
|
|
class ArraySerializer extends SerializerAbstract |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Serialize a collection. |
22
|
|
|
* |
23
|
|
|
* @param string $resourceKey |
24
|
|
|
* @param array $data |
25
|
|
|
* |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
2 |
|
public function collection($resourceKey, array $data) |
29
|
|
|
{ |
30
|
2 |
|
return [$resourceKey ?: 'data' => $data]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Serialize an item. |
35
|
|
|
* |
36
|
|
|
* @param string $resourceKey |
37
|
|
|
* @param array $data |
38
|
|
|
* |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
4 |
|
public function item($resourceKey, array $data) |
42
|
|
|
{ |
43
|
4 |
|
return $data; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Serialize null resource. |
48
|
|
|
* |
49
|
|
|
* @param string $resourceKey |
50
|
|
|
* |
51
|
|
|
* @return array|null |
52
|
|
|
*/ |
53
|
1 |
|
public function null($resourceKey) |
54
|
|
|
{ |
55
|
1 |
|
return []; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Serialize the included data. |
60
|
|
|
* |
61
|
|
|
* @param ResourceInterface $resource |
62
|
|
|
* @param array $data |
63
|
|
|
* |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
public function includedData(ResourceInterface $resource, array $data) |
67
|
|
|
{ |
68
|
|
|
return $data; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Serialize the meta. |
73
|
|
|
* |
74
|
|
|
* @param array $meta |
75
|
|
|
* |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
17 |
|
public function meta(array $meta) |
79
|
|
|
{ |
80
|
17 |
|
if (empty($meta)) { |
81
|
15 |
|
return []; |
82
|
|
|
} |
83
|
|
|
|
84
|
10 |
|
return ['meta' => $meta]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Serialize the paginator. |
89
|
|
|
* |
90
|
|
|
* @param PaginatorInterface $paginator |
91
|
|
|
* |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
1 |
|
public function paginator(PaginatorInterface $paginator) |
95
|
|
|
{ |
96
|
1 |
|
$currentPage = (int) $paginator->getCurrentPage(); |
97
|
1 |
|
$lastPage = (int) $paginator->getLastPage(); |
98
|
|
|
|
99
|
|
|
$pagination = [ |
100
|
1 |
|
'total' => (int) $paginator->getTotal(), |
101
|
1 |
|
'count' => (int) $paginator->getCount(), |
102
|
1 |
|
'per_page' => (int) $paginator->getPerPage(), |
103
|
1 |
|
'current_page' => $currentPage, |
104
|
1 |
|
'total_pages' => $lastPage, |
105
|
1 |
|
]; |
106
|
|
|
|
107
|
1 |
|
$pagination['links'] = []; |
108
|
|
|
|
109
|
1 |
|
if ($currentPage > 1) { |
110
|
1 |
|
$pagination['links']['previous'] = $paginator->getUrl($currentPage - 1); |
111
|
1 |
|
} |
112
|
|
|
|
113
|
1 |
|
if ($currentPage < $lastPage) { |
114
|
1 |
|
$pagination['links']['next'] = $paginator->getUrl($currentPage + 1); |
115
|
1 |
|
} |
116
|
|
|
|
117
|
1 |
|
return ['pagination' => $pagination]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Serialize the cursor. |
122
|
|
|
* |
123
|
|
|
* @param CursorInterface $cursor |
124
|
|
|
* |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
1 |
|
public function cursor(CursorInterface $cursor) |
128
|
|
|
{ |
129
|
|
|
$cursor = [ |
130
|
1 |
|
'current' => $cursor->getCurrent(), |
131
|
1 |
|
'prev' => $cursor->getPrev(), |
132
|
1 |
|
'next' => $cursor->getNext(), |
133
|
1 |
|
'count' => (int) $cursor->getCount(), |
134
|
1 |
|
]; |
135
|
|
|
|
136
|
1 |
|
return ['cursor' => $cursor]; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|