1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
5
|
|
|
* Date: 6/15/15 |
6
|
|
|
* Time: 11:31 PM. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace NilPortugues\Foundation\Domain\Model\Repository; |
12
|
|
|
|
13
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Collections\ImmutableTypedCollection; |
14
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Fields as FieldsInterface; |
15
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Filter as FilterInterface; |
16
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Page as PageInterface; |
17
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Pageable as PageableInterface; |
18
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Sort as SortInterface; |
19
|
|
|
|
20
|
|
|
class Page implements PageInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var ImmutableTypedCollection |
24
|
|
|
*/ |
25
|
|
|
protected $elements; |
26
|
|
|
/** |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
protected $totalPages; |
30
|
|
|
/** |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
protected $totalElements; |
34
|
|
|
/** |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
protected $pageNumber; |
38
|
|
|
/** |
39
|
|
|
* @var SortInterface |
40
|
|
|
*/ |
41
|
|
|
protected $sort; |
42
|
|
|
/** |
43
|
|
|
* @var FilterInterface |
44
|
|
|
*/ |
45
|
|
|
protected $filter; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var Fields |
49
|
|
|
*/ |
50
|
|
|
protected $fields; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Page constructor. |
54
|
|
|
* |
55
|
|
|
* @param array $elements |
56
|
|
|
* @param $totalElements |
57
|
|
|
* @param $pageNumber |
58
|
|
|
* @param $totalPages |
59
|
|
|
* @param SortInterface $sort |
60
|
|
|
* @param FilterInterface $filter |
61
|
|
|
* @param FieldsInterface $fields |
62
|
|
|
*/ |
63
|
|
|
public function __construct( |
64
|
|
|
array $elements, |
65
|
|
|
$totalElements, |
66
|
|
|
$pageNumber, |
67
|
|
|
$totalPages, |
68
|
|
|
SortInterface $sort = null, |
69
|
|
|
FilterInterface $filter = null, |
70
|
|
|
FieldsInterface $fields = null |
71
|
|
|
) { |
72
|
|
|
$this->elements = ImmutableTypedCollection::fromArray($elements); |
73
|
|
|
$this->totalElements = (int) $totalElements; |
74
|
|
|
$this->pageNumber = (int) $pageNumber; |
75
|
|
|
$this->totalPages = (int) $totalPages; |
76
|
|
|
$this->sort = ($sort) ? $sort : new Sort(); |
77
|
|
|
$this->filter = ($filter) ? $filter : new Filter(); |
78
|
|
|
$this->fields = ($fields) ? $fields : new Fields(); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns the page content as an array. |
83
|
|
|
* |
84
|
|
|
* @return ImmutableTypedCollection |
85
|
|
|
*/ |
86
|
|
|
public function content() |
87
|
|
|
{ |
88
|
|
|
return $this->elements; |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Returns if there is a previous Page. |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
|
public function hasPrevious() |
97
|
|
|
{ |
98
|
|
|
return $this->pageNumber > 1; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns whether the current Page is the first one. |
103
|
|
|
* |
104
|
|
|
* @return bool |
105
|
|
|
*/ |
106
|
|
|
public function isFirst() |
107
|
|
|
{ |
108
|
|
|
return 1 === $this->pageNumber; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Returns whether the current Page is the last one. |
113
|
|
|
* |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
public function isLast() |
117
|
|
|
{ |
118
|
|
|
return false === $this->hasNext(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Returns if there is a next Page. |
123
|
|
|
* |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
public function hasNext() |
127
|
|
|
{ |
128
|
|
|
return $this->pageSize() * $this->pageNumber() < $this->totalPages(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Returns the size of the Page. |
133
|
|
|
* |
134
|
|
|
* @return int |
135
|
|
|
*/ |
136
|
|
|
public function pageSize() |
137
|
|
|
{ |
138
|
|
|
return count($this->elements); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Returns the number of the current Page. |
143
|
|
|
* |
144
|
|
|
* @return int |
145
|
|
|
*/ |
146
|
|
|
public function pageNumber() |
147
|
|
|
{ |
148
|
|
|
return $this->pageNumber; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Returns the number of total pages. |
153
|
|
|
* |
154
|
|
|
* @return int |
155
|
|
|
*/ |
156
|
|
|
public function totalPages() |
157
|
|
|
{ |
158
|
|
|
return $this->totalPages; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Returns the Pageable to request the next Page. |
163
|
|
|
* |
164
|
|
|
* @return PageableInterface |
165
|
|
|
*/ |
166
|
|
|
public function nextPageable() |
167
|
|
|
{ |
168
|
|
|
return new Pageable( |
169
|
|
|
$this->pageNumber() + 1, |
170
|
|
|
$this->pageSize(), |
171
|
|
|
$this->sortings(), |
172
|
|
|
$this->filters(), |
173
|
|
|
$this->fields() |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Returns the sorting parameters for the Page. |
179
|
|
|
* |
180
|
|
|
* @return SortInterface |
181
|
|
|
*/ |
182
|
|
|
public function sortings() |
183
|
|
|
{ |
184
|
|
|
return $this->sort; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return FilterInterface |
189
|
|
|
*/ |
190
|
|
|
public function filters() |
191
|
|
|
{ |
192
|
|
|
return $this->filter; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return FieldsInterface |
197
|
|
|
*/ |
198
|
|
|
public function fields() |
199
|
|
|
{ |
200
|
|
|
return $this->fields; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Returns the Pageable to request the previous Page. |
205
|
|
|
* |
206
|
|
|
* @return PageableInterface |
207
|
|
|
*/ |
208
|
|
|
public function previousPageable() |
209
|
|
|
{ |
210
|
|
|
$pageable = new Pageable( |
211
|
|
|
$this->pageNumber(), |
212
|
|
|
$this->pageSize(), |
213
|
|
|
$this->sortings(), |
214
|
|
|
$this->filters(), |
215
|
|
|
$this->fields() |
216
|
|
|
); |
217
|
|
|
|
218
|
|
|
return $pageable->previousOrFirst(); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Returns the total amount of elements. |
223
|
|
|
* |
224
|
|
|
* @return int |
225
|
|
|
*/ |
226
|
|
|
public function totalElements() |
227
|
|
|
{ |
228
|
|
|
return $this->totalElements; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Returns a new Page with the content of the current one mapped by the $converter callable. |
233
|
|
|
* |
234
|
|
|
* @param callable $converter |
235
|
|
|
* |
236
|
|
|
* @return PageInterface |
237
|
|
|
*/ |
238
|
|
|
public function map(callable $converter) |
239
|
|
|
{ |
240
|
|
|
$collection = []; |
241
|
|
|
foreach ($this->elements as $key => $element) { |
242
|
|
|
$collection[$key] = $converter($element); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
return new self( |
246
|
|
|
$collection, |
247
|
|
|
$this->totalElements, |
248
|
|
|
$this->pageNumber, |
249
|
|
|
$this->totalPages, |
250
|
|
|
$this->sort, |
251
|
|
|
$this->filter, |
252
|
|
|
$this->fields |
253
|
|
|
); |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.