1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine Collection |
5
|
|
|
* |
6
|
|
|
* Platine Collection provides a flexible and simple PHP collection implementation. |
7
|
|
|
* |
8
|
|
|
* This content is released under the MIT License (MIT) |
9
|
|
|
* |
10
|
|
|
* Copyright (c) 2020 Platine Collection |
11
|
|
|
* |
12
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
13
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
14
|
|
|
* in the Software without restriction, including without limitation the rights |
15
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
16
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
17
|
|
|
* furnished to do so, subject to the following conditions: |
18
|
|
|
* |
19
|
|
|
* The above copyright notice and this permission notice shall be included in all |
20
|
|
|
* copies or substantial portions of the Software. |
21
|
|
|
* |
22
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
23
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
24
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
25
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
26
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
27
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
28
|
|
|
* SOFTWARE. |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @file BaseCollection.php |
33
|
|
|
* |
34
|
|
|
* The BaseCollection class |
35
|
|
|
* |
36
|
|
|
* @package Platine\Collection |
37
|
|
|
* @author Platine Developers Team |
38
|
|
|
* @copyright Copyright (c) 2020 |
39
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
40
|
|
|
* @link http://www.iacademy.cf |
41
|
|
|
* @version 1.0.0 |
42
|
|
|
* @filesource |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
declare(strict_types=1); |
46
|
|
|
|
47
|
|
|
namespace Platine\Collection; |
48
|
|
|
|
49
|
|
|
use Countable; |
50
|
|
|
use JsonSerializable; |
51
|
|
|
use OutOfRangeException; |
52
|
|
|
use Platine\Collection\Exception\InvalidOperationException; |
53
|
|
|
use ArrayIterator; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Class BaseCollection |
57
|
|
|
* @package Platine\Collection |
58
|
|
|
* @template T |
59
|
|
|
*/ |
60
|
|
|
abstract class BaseCollection implements Countable, JsonSerializable |
61
|
|
|
{ |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* The data container instance |
65
|
|
|
* @var DataContainer<T> |
66
|
|
|
*/ |
67
|
|
|
protected DataContainer $data; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Create new instance |
71
|
|
|
* @param array<mixed, T> $initials |
72
|
|
|
*/ |
73
|
|
|
public function __construct(array $initials = []) |
74
|
|
|
{ |
75
|
|
|
$this->data = new DataContainer($initials); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Clear the collection data |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
|
|
public function clear(): void |
83
|
|
|
{ |
84
|
|
|
$this->data = new DataContainer([]); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Check whether the collection has the given element |
89
|
|
|
* @param T $needle |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
|
|
public function contains($needle): bool |
93
|
|
|
{ |
94
|
|
|
return in_array($needle, $this->all()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* |
99
|
|
|
* @param mixed $offset |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
|
|
public function exists($offset): bool |
103
|
|
|
{ |
104
|
|
|
return $this->data->offsetExists($offset); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Return the first element of collection |
109
|
|
|
* @return T |
110
|
|
|
*/ |
111
|
|
|
public function first() |
112
|
|
|
{ |
113
|
|
|
if ($this->isEmpty()) { |
114
|
|
|
throw new OutOfRangeException('The collection is empty'); |
115
|
|
|
} |
116
|
|
|
$values = $this->all(); |
117
|
|
|
|
118
|
|
|
return $values[0]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Return the last element of collection |
123
|
|
|
* @return T |
124
|
|
|
*/ |
125
|
|
|
public function last() |
126
|
|
|
{ |
127
|
|
|
if ($this->isEmpty()) { |
128
|
|
|
throw new OutOfRangeException('The collection is empty'); |
129
|
|
|
} |
130
|
|
|
$values = $this->all(); |
131
|
|
|
|
132
|
|
|
return $values[$this->count() - 1]; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* |
137
|
|
|
* @return bool |
138
|
|
|
*/ |
139
|
|
|
public function isEmpty(): bool |
140
|
|
|
{ |
141
|
|
|
return $this->count() === 0; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Return the sum of the collection element |
146
|
|
|
* @param callable $callback |
147
|
|
|
* @return float |
148
|
|
|
*/ |
149
|
|
|
public function sum(callable $callback): float |
150
|
|
|
{ |
151
|
|
|
$sum = 0; |
152
|
|
|
foreach ($this->data as $value) { |
153
|
|
|
$val = call_user_func($callback, $value); |
154
|
|
|
if (!is_numeric($val)) { |
155
|
|
|
throw new InvalidOperationException( |
156
|
|
|
'You cannot sum non-numeric values' |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$sum += $val; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $sum; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Return the array representation of the collection |
168
|
|
|
* @return array<mixed, T> |
169
|
|
|
*/ |
170
|
|
|
public function all(): array |
171
|
|
|
{ |
172
|
|
|
return $this->data->getData(); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* {@inheritedoc} |
177
|
|
|
*/ |
178
|
|
|
public function count(): int |
179
|
|
|
{ |
180
|
|
|
return count($this->all()); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* {@inheritedoc} |
185
|
|
|
* @return array<mixed, T> |
186
|
|
|
*/ |
187
|
|
|
public function jsonSerialize(): array |
188
|
|
|
{ |
189
|
|
|
return $this->all(); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Return the different with the given collection |
194
|
|
|
* @param BaseCollection<T> $collection |
195
|
|
|
* @return $this<T> |
196
|
|
|
*/ |
197
|
|
|
abstract public function diff(BaseCollection $collection): self; |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Whether two collections are equal |
201
|
|
|
* @param BaseCollection<T> $collection |
202
|
|
|
* @return bool |
203
|
|
|
*/ |
204
|
|
|
abstract public function equals(BaseCollection $collection): bool; |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Returns a portion of the collection. |
208
|
|
|
* @param int $offset |
209
|
|
|
* @param int|null $length |
210
|
|
|
* @return null|$this |
211
|
|
|
*/ |
212
|
|
|
abstract public function slice(int $offset, ?int $length = null): ?self; |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Fill the collection |
216
|
|
|
* @param array<mixed, T> $data |
217
|
|
|
* @return void |
218
|
|
|
*/ |
219
|
|
|
abstract public function fill(array $data): void; |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* |
223
|
|
|
* @return void |
224
|
|
|
*/ |
225
|
|
|
protected function repopulate(): void |
226
|
|
|
{ |
227
|
|
|
$oldData = array_values($this->all()); |
228
|
|
|
$this->data->setData($oldData); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|