-
Notifications
You must be signed in to change notification settings - Fork 349
/
ArraySerializer.php
111 lines (93 loc) · 2.46 KB
/
ArraySerializer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/*
* This file is part of the League\Fractal package.
*
* (c) Phil Sturgeon <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\Fractal\Serializer;
use League\Fractal\Pagination\CursorInterface;
use League\Fractal\Pagination\PaginatorInterface;
use League\Fractal\Resource\ResourceInterface;
class ArraySerializer extends SerializerAbstract
{
/**
* {@inheritDoc}
*/
public function collection(?string $resourceKey, array $data): array
{
return [$resourceKey ?: 'data' => $data];
}
/**
* {@inheritDoc}
*/
public function item(?string $resourceKey, array $data): array
{
return $data;
}
/**
* {@inheritDoc}
*/
public function null(): ?array
{
return [];
}
/**
* {@inheritDoc}
*/
public function includedData(ResourceInterface $resource, array $data): array
{
return $data;
}
/**
* {@inheritDoc}
*/
public function meta(array $meta): array
{
if (empty($meta)) {
return [];
}
return ['meta' => $meta];
}
/**
* {@inheritDoc}
*/
public function paginator(PaginatorInterface $paginator): array
{
$currentPage = $paginator->getCurrentPage();
$lastPage = $paginator->getLastPage();
$pagination = [
'total' => $paginator->getTotal(),
'count' => $paginator->getCount(),
'per_page' => $paginator->getPerPage(),
'current_page' => $currentPage,
'total_pages' => $lastPage,
];
$pagination['links'] = [];
if ($currentPage > 1) {
$pagination['links']['previous'] = $paginator->getUrl($currentPage - 1);
}
if ($currentPage < $lastPage) {
$pagination['links']['next'] = $paginator->getUrl($currentPage + 1);
}
if (empty($pagination['links'])) {
$pagination['links'] = (object) [];
}
return ['pagination' => $pagination];
}
/**
* {@inheritDoc}
*/
public function cursor(CursorInterface $cursor): array
{
$cursor = [
'current' => $cursor->getCurrent(),
'prev' => $cursor->getPrev(),
'next' => $cursor->getNext(),
'count' => $cursor->getCount(),
];
return ['cursor' => $cursor];
}
}