12
12
*
13
13
* @template TKey of array-key
14
14
* @template T
15
- * @implements DomainCollection <TKey,T>
15
+ * @implements PaginatedDomainCollection <TKey,T>
16
16
*/
17
- final class GenericDomainCollection implements DomainCollection
17
+ final class GenericDomainCollection implements PaginatedDomainCollection
18
18
{
19
19
/** @var iterable<TKey, T> */
20
20
private $ elements ;
21
+ /** @var float */
22
+ private $ offset ;
23
+ /** @var float */
24
+ private $ limit ;
25
+ /** @var null|float */
26
+ private $ count ;
27
+ /** @var null|float */
28
+ private $ totalCount ;
21
29
22
30
/**
23
- * @param iterable<TKey, T> $elements
31
+ * @param null| iterable<TKey, T> $elements
24
32
*/
25
- public function __construct (iterable $ elements )
33
+ public function __construct (? iterable $ elements = null , float $ offset = .0 , float $ limit = .0 , ? float $ count = null , ? float $ totalCount = null )
26
34
{
27
- $ this ->elements = $ elements ;
28
- }
29
-
30
- public static function fromValue (?iterable $ value ): DomainCollection
31
- {
32
- return new self ($ value ?? []);
35
+ $ this ->elements = $ elements ?? [];
36
+ $ this ->offset = $ offset ;
37
+ $ this ->limit = $ limit ;
38
+ $ this ->count = $ count ;
39
+ $ this ->totalCount = $ totalCount ;
33
40
}
34
41
35
42
public function getIterator (): \Traversable
@@ -51,10 +58,6 @@ public function getIterator(): \Traversable
51
58
52
59
public function isEmpty (): bool
53
60
{
54
- if ($ this ->elements instanceof DomainCollection) {
55
- return $ this ->elements ->isEmpty ();
56
- }
57
-
58
61
if ($ this ->elements instanceof \Traversable) {
59
62
foreach ($ this ->elements as $ element ) {
60
63
return false ;
@@ -68,10 +71,6 @@ public function isEmpty(): bool
68
71
69
72
public function contains ($ element ): bool
70
73
{
71
- if ($ this ->elements instanceof DomainCollection) {
72
- return $ this ->elements ->contains ($ element );
73
- }
74
-
75
74
if ($ this ->elements instanceof \Traversable) {
76
75
foreach ($ this ->elements as $ key => $ knownElement ) {
77
76
if ($ element === $ knownElement ) {
@@ -87,10 +86,6 @@ public function contains($element): bool
87
86
88
87
public function containsKey ($ key ): bool
89
88
{
90
- if ($ this ->elements instanceof DomainCollection) {
91
- return $ this ->elements ->containsKey ($ key );
92
- }
93
-
94
89
if ($ this ->elements instanceof \Traversable) {
95
90
/** @psalm-suppress InvalidCast */
96
91
$ key = \is_int ($ key ) ? (string ) $ key : $ key ;
@@ -109,10 +104,6 @@ public function containsKey($key): bool
109
104
110
105
public function first ()
111
106
{
112
- if ($ this ->elements instanceof DomainCollection) {
113
- return $ this ->elements ->first ();
114
- }
115
-
116
107
if ($ this ->elements instanceof \Traversable) {
117
108
foreach ($ this ->elements as $ element ) {
118
109
return $ element ;
@@ -130,10 +121,6 @@ public function first()
130
121
131
122
public function last ()
132
123
{
133
- if ($ this ->elements instanceof DomainCollection) {
134
- return $ this ->elements ->last ();
135
- }
136
-
137
124
if ($ this ->elements instanceof \Traversable) {
138
125
$ empty = true ;
139
126
$ element = null ;
@@ -157,10 +144,6 @@ public function last()
157
144
158
145
public function get ($ key )
159
146
{
160
- if ($ this ->elements instanceof DomainCollection) {
161
- return $ this ->elements ->get ($ key );
162
- }
163
-
164
147
if ($ this ->elements instanceof \Traversable) {
165
148
/** @psalm-suppress InvalidCast */
166
149
$ key = \is_int ($ key ) ? (string ) $ key : $ key ;
@@ -183,11 +166,6 @@ public function get($key)
183
166
184
167
public function filter (callable $ filter ): DomainCollection
185
168
{
186
- if ($ this ->elements instanceof DomainCollection) {
187
- /** @var DomainCollection<TKey, T> */
188
- return $ this ->elements ->filter ($ filter );
189
- }
190
-
191
169
if ($ this ->elements instanceof \Traversable) {
192
170
return new self ((/** @return \Generator<TKey, T> */ function () use ($ filter ): iterable {
193
171
foreach ($ this ->elements as $ key => $ element ) {
@@ -203,11 +181,6 @@ public function filter(callable $filter): DomainCollection
203
181
204
182
public function slice (int $ offset , int $ limit = 0 ): DomainCollection
205
183
{
206
- if ($ this ->elements instanceof DomainCollection) {
207
- /** @var DomainCollection<TKey, T> */
208
- return $ this ->elements ->slice ($ offset , $ limit );
209
- }
210
-
211
184
if ($ this ->elements instanceof \Traversable) {
212
185
return new self ((/** @return \Generator<TKey, T> */ function () use ($ offset , $ limit ): iterable {
213
186
$ i = -1 ;
@@ -233,11 +206,6 @@ public function slice(int $offset, int $limit = 0): DomainCollection
233
206
*/
234
207
public function map (callable $ mapper ): DomainCollection
235
208
{
236
- if ($ this ->elements instanceof DomainCollection) {
237
- /** @var DomainCollection<TKey, T2> */
238
- return $ this ->elements ->map ($ mapper );
239
- }
240
-
241
209
if ($ this ->elements instanceof \Traversable) {
242
210
return new self ((/** @return \Generator<TKey, T2> */ function () use ($ mapper ): iterable {
243
211
foreach ($ this ->elements as $ key => $ element ) {
@@ -251,10 +219,47 @@ public function map(callable $mapper): DomainCollection
251
219
252
220
public function count (): int
253
221
{
222
+ if (null !== $ this ->count ) {
223
+ return (int ) $ this ->count ;
224
+ }
225
+
254
226
if ($ this ->elements instanceof \Countable) {
255
227
return $ this ->elements ->count ();
256
228
}
257
229
258
230
return $ this ->elements instanceof \Traversable ? iterator_count ($ this ->elements ) : \count ($ this ->elements );
259
231
}
232
+
233
+ public function getOffset (): flo
E868
at
234
+ {
235
+ return $ this ->offset ;
236
+ }
237
+
238
+ public function getLimit (): float
239
+ {
240
+ return $ this ->limit ;
241
+ }
242
+
243
+ public function getCurrentPage (): float
244
+ {
245
+ if (0 >= $ this ->limit ) {
246
+ return 1. ;
247
+ }
248
+
249
+ return floor ($ this ->offset / $ this ->limit ) + 1. ;
250
+ }
251
+
252
+ public function getLastPage (): float
253
+ {
254
+ if (0 >= $ this ->limit ) {
255
+ return 1. ;
256
+ }
257
+
258
+ return ceil ($ this ->getTotalCount () / $ this ->limit ) ?: 1. ;
259
+ }
260
+
261
+ public function getTotalCount (): float
262
+ {
263
+ return $ this ->totalCount ?? (float ) \count ($ this );
264
+ }
260
265
}
0 commit comments