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
|
|
|
* Valida um offset |
50
|
|
|
* |
51
|
|
|
* @param $offset |
52
|
|
|
* @throws InvalidOffsetTypeException |
53
|
|
|
*/ |
54
|
8 |
|
private function validateOffsetType($offset) |
55
|
|
|
{ |
56
|
8 |
|
if(!is_bool($offset) |
57
|
8 |
|
&& !is_int($offset) |
58
|
8 |
|
&& !is_float($offset) |
59
|
8 |
|
&& !is_string($offset)) |
60
|
8 |
|
throw new InvalidOffsetTypeException(); |
61
|
8 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return boolean |
65
|
|
|
*/ |
66
|
8 |
|
public function isReadOnly() |
67
|
|
|
{ |
68
|
8 |
|
return $this->readOnly; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Fecha a coleção para apenas leitura |
73
|
|
|
*/ |
74
|
2 |
|
public function lock() |
75
|
|
|
{ |
76
|
2 |
|
$this->readOnly = true; |
77
|
2 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return int|null |
81
|
|
|
*/ |
82
|
8 |
|
public function getCapacity() |
83
|
|
|
{ |
84
|
8 |
|
return $this->capacity; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param int|null $capacity |
89
|
|
|
* @throws InvalidCapacityException |
90
|
|
|
*/ |
91
|
1 |
|
public function setCapacity($capacity) |
92
|
|
|
{ |
93
|
1 |
|
if(!is_int($capacity)) |
94
|
1 |
|
throw new InvalidCapacityException(); |
95
|
|
|
|
96
|
1 |
|
if($this->count() > $capacity) |
97
|
1 |
|
throw new InvalidCapacityException(); |
98
|
|
|
|
99
|
1 |
|
$this->capacity = $capacity; |
100
|
1 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Executa callback para cada elemento armazenado |
104
|
|
|
* |
105
|
|
|
* @param callable $callback |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
3 |
|
public function each(callable $callback) |
109
|
|
|
{ |
110
|
3 |
|
foreach ($this->storage as $key => $item) |
111
|
3 |
|
$callback($item, $key); |
112
|
3 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Filtra elementos armazenados de acordo com callback |
116
|
|
|
* |
117
|
|
|
* @param callable $callback |
118
|
|
|
* @return StorageInterface |
119
|
|
|
*/ |
120
|
2 |
|
public function filter(callable $callback) |
121
|
|
|
{ |
122
|
2 |
|
$storage = clone $this; |
123
|
2 |
|
$storage->clear(); |
124
|
|
|
|
125
|
|
|
$this->each(function($item, $key) use(&$storage, $callback){ |
126
|
2 |
|
if($callback($item, $key) === true) |
127
|
2 |
|
$storage[$key] = $item; |
128
|
2 |
|
}); |
129
|
|
|
|
130
|
2 |
|
return $storage; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Copia o conteúdo do storage para dentro de outra array ou storage |
135
|
|
|
* |
136
|
|
|
* @param $array |
137
|
|
|
* @param bool $override |
138
|
|
|
*/ |
139
|
|
|
public function copyTo(&$array, $override = true) |
140
|
|
|
{ |
141
|
1 |
|
$this->each(function($item, $key) use(&$array, $override){ |
142
|
1 |
|
if(!$override && isset($array[$key])) |
143
|
1 |
|
return ; |
144
|
|
|
|
145
|
1 |
|
$array[$key] = $item; |
146
|
1 |
|
}); |
147
|
1 |
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Limpa o storage completamente |
151
|
|
|
* |
152
|
|
|
* @throws ReadOnlyStorageException |
153
|
|
|
*/ |
154
|
2 |
|
public function clear() |
155
|
|
|
{ |
156
|
2 |
|
if($this->isReadOnly()) |
157
|
2 |
|
throw new ReadOnlyStorageException(); |
158
|
|
|
|
159
|
2 |
|
$this->storage = array(); |
160
|
2 |
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Retorna o número de elementos armazenados |
164
|
|
|
* |
165
|
|
|
* @return int |
166
|
|
|
*/ |
167
|
8 |
|
public function count() |
168
|
|
|
{ |
169
|
8 |
|
return count($this->storage); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Return the current element |
174
|
|
|
* |
175
|
|
|
* @link http://php.net/manual/en/iterator.current.php |
176
|
|
|
* @return mixed Can return any type. |
177
|
|
|
* @since 5.0.0 |
178
|
|
|
*/ |
179
|
|
|
public function current() |
180
|
|
|
{ |
181
|
|
|
return $this->storage[$this->current]; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Move forward to next element |
186
|
|
|
* |
187
|
|
|
* @link http://php.net/manual/en/iterator.next.php |
188
|
|
|
* @return void Any returned value is ignored. |
189
|
|
|
* @since 5.0.0 |
190
|
|
|
*/ |
191
|
4 |
|
public function next() |
192
|
|
|
{ |
193
|
4 |
|
++$this->current; |
194
|
4 |
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Return the key of the current element |
198
|
|
|
* |
199
|
|
|
* @link http://php.net/manual/en/iterator.key.php |
200
|
|
|
* @return mixed scalar on success, or null on failure. |
201
|
|
|
* @since 5.0.0 |
202
|
|
|
*/ |
203
|
3 |
|
public function key() |
204
|
|
|
{ |
205
|
3 |
|
return $this->current; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Checks if current position is valid |
210
|
|
|
* |
211
|
|
|
* @link http://php.net/manual/en/iterator.valid.php |
212
|
|
|
* @return boolean The return value will be casted to boolean and then evaluated. |
213
|
|
|
* Returns true on success or false on failure. |
214
|
|
|
* @since 5.0.0 |
215
|
|
|
*/ |
216
|
3 |
|
public function valid() |
217
|
|
|
{ |
218
|
3 |
|
return isset($this->storage[$this->current]); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Rewind the Iterator to the first element |
223
|
|
|
* |
224
|
|
|
* @link http://php.net/manual/en/iterator.rewind.php |
225
|
|
|
* @return void Any returned value is ignored. |
226
|
|
|
* @since 5.0.0 |
227
|
|
|
*/ |
228
|
4 |
|
public function rewind() |
229
|
|
|
{ |
230
|
4 |
|
$this->current = 0; |
231
|
4 |
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Whether a offset exists |
235
|
|
|
* |
236
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetexists.php |
237
|
|
|
* @param mixed $offset <p> |
238
|
|
|
* An offset to check for. |
239
|
|
|
* </p> |
240
|
|
|
* @return boolean true on success or false on failure. |
241
|
|
|
* </p> |
242
|
|
|
* <p> |
243
|
|
|
* The return value will be casted to boolean if non-boolean was returned. |
244
|
|
|
* @since 5.0.0 |
245
|
|
|
*/ |
246
|
5 |
|
public function offsetExists($offset) |
247
|
|
|
{ |
248
|
5 |
|
return isset($this->storage[$offset]); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Offset to retrieve |
253
|
|
|
* |
254
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetget.php |
255
|
|
|
* @param mixed $offset <p> |
256
|
|
|
* The offset to retrieve. |
257
|
|
|
* </p> |
258
|
|
|
* @return mixed Can return all value types. |
259
|
|
|
* @throws InvalidOffsetException |
260
|
|
|
* @since 5.0.0 |
261
|
|
|
*/ |
262
|
4 |
|
public function offsetGet($offset) |
263
|
|
|
{ |
264
|
4 |
|
if(!$this->offsetExists($offset)) |
265
|
4 |
|
throw new InvalidOffsetException(); |
266
|
|
|
|
267
|
3 |
|
return $this->storage[$offset]; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Offset to set |
272
|
|
|
* |
273
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetset.php |
274
|
|
|
* @param mixed $offset <p> |
275
|
|
|
* The offset to assign the value to. |
276
|
|
|
* </p> |
277
|
|
|
* @param mixed $value <p> |
278
|
|
|
* The value to set. |
279
|
|
|
* </p> |
280
|
|
|
* @throws InvalidOffsetTypeException |
281
|
|
|
* @throws MaxCapacityReachedException |
282
|
|
|
* @throws ReadOnlyStorageException |
283
|
|
|
* @since 5.0.0 |
284
|
|
|
*/ |
285
|
8 |
View Code Duplication |
public function offsetSet($offset, $value) |
|
|
|
|
286
|
|
|
{ |
287
|
8 |
|
if($this->isReadOnly()) |
288
|
8 |
|
throw new ReadOnlyStorageException(); |
289
|
|
|
|
290
|
8 |
|
if($this->count() === $this->getCapacity()) |
291
|
8 |
|
throw new MaxCapacityReachedException(); |
292
|
|
|
|
293
|
8 |
|
$this->validateOffsetType($offset); |
294
|
8 |
|
$this->storage[$offset] = $value; |
295
|
8 |
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Offset to unset |
299
|
|
|
* |
300
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetunset.php |
301
|
|
|
* @param mixed $offset <p> |
302
|
|
|
* The offset to unset. |
303
|
|
|
* </p> |
304
|
|
|
* @throws InvalidOffsetException |
305
|
|
|
* @throws ReadOnlyStorageException |
306
|
|
|
* @since 5.0.0 |
307
|
|
|
*/ |
308
|
2 |
View Code Duplication |
public function offsetUnset($offset) |
|
|
|
|
309
|
|
|
{ |
310
|
2 |
|
if(!$this->offsetExists($offset)) |
311
|
2 |
|
throw new InvalidOffsetException(); |
312
|
|
|
|
313
|
1 |
|
if($this->isReadOnly()) |
314
|
1 |
|
throw new ReadOnlyStorageException(); |
315
|
|
|
|
316
|
|
|
$this->validateOffsetType($offset); |
317
|
|
|
unset($this->storage[$offset]); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Converte o objeto para array |
322
|
|
|
* |
323
|
|
|
* @return array |
324
|
|
|
*/ |
325
|
1 |
|
public function toArray() |
326
|
|
|
{ |
327
|
1 |
|
return $this->storage; |
328
|
|
|
} |
329
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.