1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spiral Framework. |
4
|
|
|
* |
5
|
|
|
* @license MIT |
6
|
|
|
* @author Anton Titov (Wolfy-J) |
7
|
|
|
*/ |
8
|
|
|
namespace Spiral\Listing; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
use Spiral\Listing\Exceptions\InvalidSelectorException; |
12
|
|
|
use Spiral\Listing\Traits\SelectorValidationTrait; |
13
|
|
|
use Spiral\ODM\Entities\DocumentSelector; |
14
|
|
|
use Spiral\ORM\Entities\RecordSelector; |
15
|
|
|
use Spiral\Pagination\Paginator; |
16
|
|
|
use Spiral\Pagination\PaginatorAwareInterface; |
17
|
|
|
use Spiral\Pagination\PaginatorInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Packs listing state into array form. |
21
|
|
|
*/ |
22
|
|
|
class ListingSerializer implements \JsonSerializable |
23
|
|
|
{ |
24
|
|
|
use SelectorValidationTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Listing |
28
|
|
|
*/ |
29
|
|
|
private $listing = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var PaginatorAwareInterface|RecordSelector|DocumentSelector |
33
|
|
|
*/ |
34
|
|
|
private $selector = null; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Listing $listing |
38
|
|
|
* @param PaginatorAwareInterface|RecordSelector|DocumentSelector $selector |
39
|
|
|
* |
40
|
|
|
* @throws InvalidSelectorException |
41
|
|
|
*/ |
42
|
|
|
public function __construct(Listing $listing, PaginatorAwareInterface $selector) |
43
|
|
|
{ |
44
|
|
|
$this->validateSelector($selector); |
45
|
|
|
|
46
|
|
|
$this->listing = $listing; |
47
|
|
|
$this->selector = $selector; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Unique id associated with listing. |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function getID() |
56
|
|
|
{ |
57
|
|
|
return md5(spl_object_hash($this->listing)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
|
public function toArray() |
64
|
|
|
{ |
65
|
|
|
return [ |
66
|
|
|
'namespace' => $this->listing->getNamespace(), |
67
|
|
|
'sorting' => $this->packSorting(), |
68
|
|
|
'filters' => $this->packFilters(), |
69
|
|
|
'pagination' => $this->packPagination() |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
public function jsonSerialize() |
77
|
|
|
{ |
78
|
|
|
return $this->toArray(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param Listing $listing |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public static function listingID(Listing $listing) |
86
|
|
|
{ |
87
|
|
|
return md5(spl_object_hash($listing)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
protected function packSorting() |
94
|
|
|
{ |
95
|
|
|
$packed = [ |
96
|
|
|
'sorter' => $this->listing->getSorter(), |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
$sorter = $this->listing->activeSorter(); |
100
|
|
|
if ($sorter instanceof DirectionalSorterInterface) { |
101
|
|
|
$packed['direction'] = $sorter->getDirection() == SorterInterface::ASC ? 'asc' : 'desc'; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $packed; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
protected function packFilters() |
111
|
|
|
{ |
112
|
|
|
$packed = []; |
113
|
|
|
foreach ($this->listing->activeFilters() as $name => $filter) { |
114
|
|
|
$packed[$name] = $filter->getValue(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $packed; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
protected function packPagination() |
124
|
|
|
{ |
125
|
|
|
/** |
126
|
|
|
* @var PaginatorInterface $paginator |
127
|
|
|
*/ |
128
|
|
|
$paginator = $this->selector->getPaginator(); |
129
|
|
|
|
130
|
|
|
if ($paginator instanceof Paginator) { |
131
|
|
|
return [ |
132
|
|
|
'page' => $paginator->getPage(), |
133
|
|
|
|
134
|
|
|
//Total items |
135
|
|
|
'total' => $paginator->count(), |
136
|
|
|
|
137
|
|
|
//Pages around |
138
|
|
|
'countPages' => $paginator->countPages(), |
139
|
|
|
'nextPage' => $paginator->nextPage(), |
140
|
|
|
'previousPage' => $paginator->previousPage(), |
141
|
|
|
|
142
|
|
|
//Limitations |
143
|
|
|
'limit' => $paginator->getLimit(), |
144
|
|
|
'limits' => $this->listing->getLimits() |
145
|
|
|
]; |
146
|
|
|
} else { |
147
|
|
|
return [ |
148
|
|
|
'page' => floor($paginator->getOffset() / $paginator->getLimit()) ?: 1, |
149
|
|
|
'limit' => $paginator->getLimit(), |
150
|
|
|
'limits' => $this->listing->getLimits() |
151
|
|
|
]; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
} |