|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link https://github.com/old-town/old-town-propertyset |
|
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace OldTown\PropertySet\Providers\Memory; |
|
7
|
|
|
|
|
8
|
|
|
use \OldTown\PropertySet\AbstractPropertySet; |
|
9
|
|
|
use OldTown\PropertySet\Exception; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class PropertySetManager |
|
14
|
|
|
* |
|
15
|
|
|
* @package OldTown\PropertySet |
|
16
|
|
|
*/ |
|
17
|
|
|
class MemoryPropertySet extends AbstractPropertySet |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var ValueEntry[] |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $map = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param string|null $prefix |
|
26
|
|
|
* @param integer|null $type |
|
27
|
|
|
* |
|
28
|
|
|
* @return array |
|
29
|
|
|
* |
|
30
|
|
|
* @throws Exception\PropertyException |
|
31
|
|
|
*/ |
|
32
|
|
|
public function getKeys($prefix = null, $type = null) |
|
33
|
|
|
{ |
|
34
|
|
|
$map = $this->getMap(); |
|
35
|
|
|
$keys = array_keys($map); |
|
36
|
|
|
|
|
37
|
|
|
$result = []; |
|
38
|
|
|
|
|
39
|
|
|
foreach ($keys as $key) { |
|
40
|
|
|
if (null === $prefix || (is_string($prefix) && strpos($key, $prefix))) { |
|
41
|
|
|
if (0 === $type) { |
|
42
|
|
|
$result[$key] = $key; |
|
43
|
|
|
} else { |
|
44
|
|
|
$v = $map[$key]; |
|
45
|
|
|
|
|
46
|
|
|
if ($type === $v->getType()) { |
|
47
|
|
|
$result[$key] = $key; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $result; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $key |
|
58
|
|
|
* |
|
59
|
|
|
* @return integer |
|
60
|
|
|
* |
|
61
|
|
|
* @throws Exception\PropertyException |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getType($key) |
|
64
|
|
|
{ |
|
65
|
|
|
$map = $this->getMap(); |
|
66
|
|
|
if (array_key_exists($key, $map)) { |
|
67
|
|
|
$type = $map[$key]->getType(); |
|
68
|
|
|
return $type; |
|
69
|
|
|
} |
|
70
|
|
|
return 0; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string $key |
|
75
|
|
|
* |
|
76
|
|
|
* @return boolean |
|
77
|
|
|
* |
|
78
|
|
|
* @throws \OldTown\PropertySet\Exception\PropertyException |
|
79
|
|
|
*/ |
|
80
|
|
|
public function exists($key) |
|
81
|
|
|
{ |
|
82
|
|
|
$exists = $this->getType($key) > 0; |
|
83
|
|
|
|
|
84
|
|
|
return $exists; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param string $key |
|
89
|
|
|
* |
|
90
|
|
|
* @return $this |
|
91
|
|
|
* |
|
92
|
|
|
* @throws Exception\PropertyException |
|
93
|
|
|
*/ |
|
94
|
|
|
public function remove($key) |
|
95
|
|
|
{ |
|
96
|
|
|
$this->map = []; |
|
97
|
|
|
|
|
98
|
|
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return ValueEntry[] |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function getMap() |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->map; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param integer $type |
|
114
|
|
|
* @param string $key |
|
115
|
|
|
* @param mixed $value |
|
116
|
|
|
* |
|
117
|
|
|
* @return $this |
|
118
|
|
|
* |
|
119
|
|
|
* @throws Exception\PropertyException |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function setImpl($type, $key, $value) |
|
122
|
|
|
{ |
|
123
|
|
|
if ($this->exists($key)) { |
|
124
|
|
|
$v = $this->getMap()[$key]; |
|
125
|
|
|
|
|
126
|
|
|
if ($type !== $v->getType()) { |
|
127
|
|
|
$errMsg = 'Ошибка с типами'; |
|
128
|
|
|
throw new Exception\DuplicatePropertyKeyException($errMsg); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$v->setValue($value); |
|
132
|
|
|
} else { |
|
133
|
|
|
$valueEntry = new ValueEntry($type, $value); |
|
134
|
|
|
$this->map[$key] = $valueEntry; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param integer $type |
|
142
|
|
|
* @param string $key |
|
143
|
|
|
* |
|
144
|
|
|
* @return mixed |
|
145
|
|
|
* |
|
146
|
|
|
* @throws Exception\InvalidPropertyTypeException |
|
147
|
|
|
* @throws \OldTown\PropertySet\Exception\PropertyException |
|
148
|
|
|
* @throws \OldTown\PropertySet\Exception\DuplicatePropertyKeyException |
|
149
|
|
|
*/ |
|
150
|
|
|
protected function get($type, $key) |
|
151
|
|
|
{ |
|
152
|
|
|
if ($this->exists($key)) { |
|
153
|
|
|
$v = $this->getMap()[$key]; |
|
154
|
|
|
|
|
155
|
|
|
if ($type !== $v->getType()) { |
|
156
|
|
|
$errMsg = 'Ошибка с типами'; |
|
157
|
|
|
throw new Exception\DuplicatePropertyKeyException($errMsg); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
$value = $v->getValue(); |
|
161
|
|
|
return $value; |
|
162
|
|
|
} |
|
163
|
|
|
return null; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|