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
|
7 |
|
public function item($resourceKey, array $data) |
42
|
|
|
{ |
43
|
7 |
|
return $data; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Serialize null resource. |
48
|
|
|
* |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
1 |
|
public function null() |
52
|
|
|
{ |
53
|
1 |
|
return []; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Serialize the included data. |
58
|
|
|
* |
59
|
|
|
* @param ResourceInterface $resource |
60
|
|
|
* @param array $data |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
public function includedData(ResourceInterface $resource, array $data) |
65
|
|
|
{ |
66
|
|
|
return $data; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Serialize the meta. |
71
|
|
|
* |
72
|
|
|
* @param array $meta |
73
|
|
|
* |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
22 |
|
public function meta(array $meta) |
77
|
|
|
{ |
78
|
22 |
|
if (empty($meta)) { |
79
|
20 |
|
return []; |
80
|
|
|
} |
81
|
|
|
|
82
|
9 |
|
return ['meta' => $meta]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Serialize the paginator. |
87
|
|
|
* |
88
|
|
|
* @param PaginatorInterface $paginator |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
1 |
|
public function paginator(PaginatorInterface $paginator) |
93
|
|
|
{ |
94
|
1 |
|
$currentPage = (int) $paginator->getCurrentPage(); |
95
|
1 |
|
$lastPage = (int) $paginator->getLastPage(); |
96
|
|
|
|
97
|
|
|
$pagination = [ |
98
|
1 |
|
'total' => (int) $paginator->getTotal(), |
99
|
1 |
|
'count' => (int) $paginator->getCount(), |
100
|
1 |
|
'per_page' => (int) $paginator->getPerPage(), |
101
|
1 |
|
'current_page' => $currentPage, |
102
|
1 |
|
'total_pages' => $lastPage, |
103
|
1 |
|
]; |
104
|
|
|
|
105
|
1 |
|
$pagination['links'] = []; |
106
|
|
|
|
107
|
1 |
|
if ($currentPage > 1) { |
108
|
1 |
|
$pagination['links']['previous'] = $paginator->getUrl($currentPage - 1); |
109
|
1 |
|
} |
110
|
|
|
|
111
|
1 |
|
if ($currentPage < $lastPage) { |
112
|
1 |
|
$pagination['links']['next'] = $paginator->getUrl($currentPage + 1); |
113
|
1 |
|
} |
114
|
|
|
|
115
|
1 |
|
if (empty($pagination['links'])) { |
116
|
|
|
$pagination['links'] = (object) []; |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
return ['pagination' => $pagination]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Serialize the cursor. |
124
|
|
|
* |
125
|
|
|
* @param CursorInterface $cursor |
126
|
|
|
* |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
1 |
|
public function cursor(CursorInterface $cursor) |
130
|
|
|
{ |
131
|
|
|
$cursor = [ |
132
|
1 |
|
'current' => $cursor->getCurrent(), |
133
|
1 |
|
'prev' => $cursor->getPrev(), |
134
|
1 |
|
'next' => $cursor->getNext(), |
135
|
1 |
|
'count' => (int) $cursor->getCount(), |
136
|
1 |
|
]; |
137
|
|
|
|
138
|
1 |
|
return ['cursor' => $cursor]; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|