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