|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Minerva\Collections; |
|
4
|
|
|
|
|
5
|
|
|
use Minerva\Collections\Basis\Abstractions\AbstractStorage; |
|
6
|
|
|
use Minerva\Collections\Basis\Exceptions\InvalidOffsetTypeException; |
|
7
|
|
|
use Minerva\Collections\Basis\Exceptions\MaxCapacityReachedException; |
|
8
|
|
|
use Minerva\Collections\Basis\Exceptions\OverrideOperationException; |
|
9
|
|
|
use Minerva\Collections\Basis\Exceptions\ReadOnlyStorageException; |
|
10
|
|
|
use Minerva\Collections\Basis\Interfaces\DictionaryInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Dicionário de dados |
|
14
|
|
|
* |
|
15
|
|
|
* @author Lucas A. de Araújo <[email protected]> |
|
16
|
|
|
* @package Minerva\Collections |
|
17
|
|
|
*/ |
|
18
|
|
|
class Dictionary extends AbstractStorage implements DictionaryInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Define se o override de dados é permitido |
|
22
|
|
|
* |
|
23
|
|
|
* @var bool |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $overrideAllowed = true; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Retorna se a classe permite override |
|
29
|
|
|
* |
|
30
|
|
|
* @return boolean |
|
31
|
|
|
*/ |
|
32
|
1 |
|
public function isOverrideAllowed() |
|
33
|
|
|
{ |
|
34
|
1 |
|
return $this->overrideAllowed; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Define o comportamento do objeto acerca do override |
|
39
|
|
|
* |
|
40
|
|
|
* @param boolean $overrideAllowed |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public function setOverrideAllowed($overrideAllowed) |
|
43
|
|
|
{ |
|
44
|
1 |
|
$this->overrideAllowed = $overrideAllowed; |
|
45
|
1 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Define um valor para uma chave |
|
49
|
|
|
* |
|
50
|
|
|
* Esse método tem por finalidade apenas adicionar uma especialização |
|
51
|
|
|
* para os contextos de utilização de um dicionário de dados. |
|
52
|
|
|
* |
|
53
|
|
|
* @param mixed $offset |
|
54
|
|
|
* @param mixed $value |
|
55
|
|
|
* @throws InvalidOffsetTypeException |
|
56
|
|
|
* @throws MaxCapacityReachedException |
|
57
|
|
|
* @throws ReadOnlyStorageException |
|
58
|
|
|
* @throws OverrideOperationException |
|
59
|
|
|
*/ |
|
60
|
2 |
|
public function offsetSet($offset, $value) |
|
61
|
|
|
{ |
|
62
|
2 |
|
if($this->offsetExists($offset) && !$this->isOverrideAllowed()) |
|
63
|
2 |
|
throw new OverrideOperationException(); |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
2 |
|
parent::offsetSet($offset, $value); |
|
67
|
2 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Retorna a chave do elemento atual no dicionário |
|
71
|
|
|
* |
|
72
|
|
|
* @return int|float|bool|string |
|
73
|
|
|
*/ |
|
74
|
2 |
|
public function key() |
|
75
|
|
|
{ |
|
76
|
2 |
|
$keys = []; |
|
77
|
|
|
|
|
78
|
2 |
|
foreach ($this->storage as $key => $item){ |
|
79
|
2 |
|
$keys[] = $key; |
|
80
|
2 |
|
} |
|
81
|
|
|
|
|
82
|
2 |
|
if(isset($keys[$this->current])) |
|
83
|
2 |
|
return $keys[$this->current]; |
|
84
|
2 |
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Retorna o elemento atual |
|
88
|
|
|
* |
|
89
|
|
|
* @return mixed |
|
90
|
|
|
*/ |
|
91
|
1 |
|
public function current() |
|
92
|
|
|
{ |
|
93
|
1 |
|
return $this->storage[$this->key()]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Verifica se a posição atual é válida |
|
98
|
|
|
* |
|
99
|
|
|
* @return bool |
|
100
|
|
|
*/ |
|
101
|
2 |
|
public function valid() |
|
102
|
|
|
{ |
|
103
|
2 |
|
return isset($this->storage[$this->key()]); |
|
104
|
|
|
} |
|
105
|
|
|
} |