|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Minerva\Collections\Basis\Abstractions; |
|
4
|
|
|
|
|
5
|
|
|
use Minerva\Collections\Basis\Exceptions\InvalidCapacityException; |
|
6
|
|
|
use Minerva\Collections\Basis\Exceptions\InvalidOffsetException; |
|
7
|
|
|
use Minerva\Collections\Basis\Exceptions\InvalidOffsetTypeException; |
|
8
|
|
|
use Minerva\Collections\Basis\Exceptions\MaxCapacityReachedException; |
|
9
|
|
|
use Minerva\Collections\Basis\Interfaces\StorageInterface; |
|
10
|
|
|
use Minerva\Collections\Basis\Exceptions\ReadOnlyStorageException; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Implementação abstrata do storage |
|
14
|
|
|
* |
|
15
|
|
|
* @author Lucas A. de Araújo <[email protected]> |
|
16
|
|
|
* @package Minerva\Collections\Basis\Abstractions |
|
17
|
|
|
*/ |
|
18
|
|
|
abstract class AbstractStorage implements StorageInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Array onde os dados serão armazenados |
|
22
|
|
|
* |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $storage = array(); |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Elemento atual em operação |
|
29
|
|
|
* |
|
30
|
|
|
* @var int |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $current = 0; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Define se o storage é apenas para leitura |
|
36
|
|
|
* |
|
37
|
|
|
* @var bool |
|
38
|
|
|
*/ |
|
39
|
|
|
private $readOnly = false; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Define a capacidade máxima do storage |
|
43
|
|
|
* |
|
44
|
|
|
* @var int|null |
|
45
|
|
|
*/ |
|
46
|
|
|
private $capacity = null; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return boolean |
|
50
|
|
|
*/ |
|
51
|
9 |
|
public function isReadOnly() |
|
52
|
|
|
{ |
|
53
|
9 |
|
return $this->readOnly; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Fecha a coleção para apenas leitura |
|
58
|
|
|
*/ |
|
59
|
2 |
|
public function lock() |
|
60
|
|
|
{ |
|
61
|
2 |
|
$this->readOnly = true; |
|
62
|
2 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return int|null |
|
66
|
|
|
*/ |
|
67
|
9 |
|
public function getCapacity() |
|
68
|
|
|
{ |
|
69
|
9 |
|
return $this->capacity; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param int|null $capacity |
|
74
|
|
|
* @throws InvalidCapacityException |
|
75
|
|
|
*/ |
|
76
|
1 |
|
public function setCapacity($capacity) |
|
77
|
|
|
{ |
|
78
|
1 |
|
if(!is_int($capacity)) |
|
79
|
1 |
|
throw new InvalidCapacityException(); |
|
80
|
|
|
|
|
81
|
1 |
|
if($this->count() > $capacity) |
|
82
|
1 |
|
throw new InvalidCapacityException(); |
|
83
|
|
|
|
|
84
|
1 |
|
$this->capacity = $capacity; |
|
85
|
1 |
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Executa callback para cada elemento armazenado |
|
89
|
|
|
* |
|
90
|
|
|
* @param callable $callback |
|
91
|
|
|
* @return void |
|
92
|
|
|
*/ |
|
93
|
3 |
|
public function each(callable $callback) |
|
94
|
|
|
{ |
|
95
|
3 |
|
foreach ($this->storage as $key => $item) |
|
96
|
3 |
|
$callback($item, $key); |
|
97
|
3 |
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Filtra elementos armazenados de acordo com callback |
|
101
|
|
|
* |
|
102
|
|
|
* @param callable $callback |
|
103
|
|
|
* @return StorageInterface |
|
104
|
|
|
*/ |
|
105
|
2 |
|
public function filter(callable $callback) |
|
106
|
|
|
{ |
|
107
|
2 |
|
$storage = clone $this; |
|
108
|
2 |
|
$storage->clear(); |
|
109
|
|
|
|
|
110
|
|
|
$this->each(function($item, $key) use(&$storage, $callback){ |
|
111
|
2 |
|
if($callback($item, $key) === true) |
|
112
|
2 |
|
$storage[$key] = $item; |
|
113
|
2 |
|
}); |
|
114
|
|
|
|
|
115
|
2 |
|
return $storage; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Copia o conteúdo do storage para dentro de outra array ou storage |
|
120
|
|
|
* |
|
121
|
|
|
* @param $array |
|
122
|
|
|
* @param bool $override |
|
123
|
|
|
*/ |
|
124
|
|
|
public function copyTo(&$array, $override = true) |
|
125
|
|
|
{ |
|
126
|
1 |
|
$this->each(function($item, $key) use(&$array, $override){ |
|
127
|
1 |
|
if(!$override && isset($array[$key])) |
|
128
|
1 |
|
return ; |
|
129
|
|
|
|
|
130
|
1 |
|
$array[$key] = $item; |
|
131
|
1 |
|
}); |
|
132
|
1 |
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Limpa o storage completamente |
|
136
|
|
|
* |
|
137
|
|
|
* @throws ReadOnlyStorageException |
|
138
|
|
|
*/ |
|
139
|
2 |
|
public function clear() |
|
140
|
|
|
{ |
|
141
|
2 |
|
if($this->isReadOnly()) |
|
142
|
2 |
|
throw new ReadOnlyStorageException(); |
|
143
|
|
|
|
|
144
|
2 |
|
$this->storage = array(); |
|
145
|
2 |
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Retorna o número de elementos armazenados |
|
149
|
|
|
* |
|
150
|
|
|
* @return int |
|
151
|
|
|
*/ |
|
152
|
9 |
|
public function count() |
|
153
|
|
|
{ |
|
154
|
9 |
|
return count($this->storage); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Return the current element |
|
159
|
|
|
* |
|
160
|
|
|
* @link http://php.net/manual/en/iterator.current.php |
|
161
|
|
|
* @return mixed Can return any type. |
|
162
|
|
|
* @since 5.0.0 |
|
163
|
|
|
*/ |
|
164
|
1 |
|
public function current() |
|
165
|
|
|
{ |
|
166
|
1 |
|
return $this->storage[$this->current]; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Move forward to next element |
|
171
|
|
|
* |
|
172
|
|
|
* @link http://php.net/manual/en/iterator.next.php |
|
173
|
|
|
* @return void Any returned value is ignored. |
|
174
|
|
|
* @since 5.0.0 |
|
175
|
|
|
*/ |
|
176
|
5 |
|
public function next() |
|
177
|
|
|
{ |
|
178
|
5 |
|
++$this->current; |
|
179
|
5 |
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Return the key of the current element |
|
183
|
|
|
* |
|
184
|
|
|
* @link http://php.net/manual/en/iterator.key.php |
|
185
|
|
|
* @return mixed scalar on success, or null on failure. |
|
186
|
|
|
* @since 5.0.0 |
|
187
|
|
|
*/ |
|
188
|
3 |
|
public function key() |
|
189
|
|
|
{ |
|
190
|
3 |
|
return $this->current; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Checks if current position is valid |
|
195
|
|
|
* |
|
196
|
|
|
* @link http://php.net/manual/en/iterator.valid.php |
|
197
|
|
|
* @return boolean The return value will be casted to boolean and then evaluated. |
|
198
|
|
|
* Returns true on success or false on failure. |
|
199
|
|
|
* @since 5.0.0 |
|
200
|
|
|
*/ |
|
201
|
3 |
|
public function valid() |
|
202
|
|
|
{ |
|
203
|
3 |
|
return isset($this->storage[$this->current]); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Rewind the Iterator to the first element |
|
208
|
|
|
* |
|
209
|
|
|
* @link http://php.net/manual/en/iterator.rewind.php |
|
210
|
|
|
* @return void Any returned value is ignored. |
|
211
|
|
|
* @since 5.0.0 |
|
212
|
|
|
*/ |
|
213
|
5 |
|
public function rewind() |
|
214
|
|
|
{ |
|
215
|
5 |
|
$this->current = 0; |
|
216
|
5 |
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Whether a offset exists |
|
220
|
|
|
* |
|
221
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetexists.php |
|
222
|
|
|
* @param mixed $offset <p> |
|
223
|
|
|
* An offset to check for. |
|
224
|
|
|
* </p> |
|
225
|
|
|
* @return boolean true on success or false on failure. |
|
226
|
|
|
* </p> |
|
227
|
|
|
* <p> |
|
228
|
|
|
* The return value will be casted to boolean if non-boolean was returned. |
|
229
|
|
|
* @since 5.0.0 |
|
230
|
|
|
*/ |
|
231
|
6 |
|
public function offsetExists($offset) |
|
232
|
|
|
{ |
|
233
|
6 |
|
return isset($this->storage[$offset]); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Offset to retrieve |
|
238
|
|
|
* |
|
239
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetget.php |
|
240
|
|
|
* @param mixed $offset <p> |
|
241
|
|
|
* The offset to retrieve. |
|
242
|
|
|
* </p> |
|
243
|
|
|
* @return mixed Can return all value types. |
|
244
|
|
|
* @throws InvalidOffsetException |
|
245
|
|
|
* @since 5.0.0 |
|
246
|
|
|
*/ |
|
247
|
4 |
|
public function offsetGet($offset) |
|
248
|
|
|
{ |
|
249
|
4 |
|
if(!$this->offsetExists($offset)) |
|
250
|
4 |
|
throw new InvalidOffsetException(); |
|
251
|
|
|
|
|
252
|
3 |
|
return $this->storage[$offset]; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Offset to set |
|
257
|
|
|
* |
|
258
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetset.php |
|
259
|
|
|
* @param mixed $offset <p> |
|
260
|
|
|
* The offset to assign the value to. |
|
261
|
|
|
* </p> |
|
262
|
|
|
* @param mixed $value <p> |
|
263
|
|
|
* The value to set. |
|
264
|
|
|
* </p> |
|
265
|
|
|
* @throws InvalidOffsetTypeException |
|
266
|
|
|
* @throws MaxCapacityReachedException |
|
267
|
|
|
* @throws ReadOnlyStorageException |
|
268
|
|
|
* @since 5.0.0 |
|
269
|
|
|
*/ |
|
270
|
9 |
|
public function offsetSet($offset, $value) |
|
271
|
|
|
{ |
|
272
|
9 |
|
if($this->isReadOnly()) |
|
273
|
9 |
|
throw new ReadOnlyStorageException(); |
|
274
|
|
|
|
|
275
|
9 |
|
if($this->count() === $this->getCapacity()) |
|
276
|
9 |
|
throw new MaxCapacityReachedException(); |
|
277
|
|
|
|
|
278
|
9 |
|
$this->storage[$offset] = $value; |
|
279
|
9 |
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* Offset to unset |
|
283
|
|
|
* |
|
284
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetunset.php |
|
285
|
|
|
* @param mixed $offset <p> |
|
286
|
|
|
* The offset to unset. |
|
287
|
|
|
* </p> |
|
288
|
|
|
* @throws InvalidOffsetException |
|
289
|
|
|
* @throws ReadOnlyStorageException |
|
290
|
|
|
* @since 5.0.0 |
|
291
|
|
|
*/ |
|
292
|
3 |
|
public function offsetUnset($offset) |
|
293
|
|
|
{ |
|
294
|
3 |
|
if(!$this->offsetExists($offset)) |
|
295
|
3 |
|
throw new InvalidOffsetException(); |
|
296
|
|
|
|
|
297
|
2 |
|
if($this->isReadOnly()) |
|
298
|
2 |
|
throw new ReadOnlyStorageException(); |
|
299
|
|
|
|
|
300
|
1 |
|
unset($this->storage[$offset]); |
|
301
|
1 |
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* Converte o objeto para array |
|
305
|
|
|
* |
|
306
|
|
|
* @return array |
|
307
|
|
|
*/ |
|
308
|
1 |
|
public function toArray() |
|
309
|
|
|
{ |
|
310
|
1 |
|
return $this->storage; |
|
311
|
|
|
} |
|
312
|
|
|
} |