|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Gerard van Helden <[email protected]> |
|
4
|
|
|
* @copyright Zicht Online <http://zicht.nl> |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Zicht\Bundle\FrameworkExtraBundle\Util; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* A key based map of sorted sets. |
|
11
|
|
|
* |
|
12
|
|
|
* @deprecated Use sorting, unique, and mapBy functionality from `zicht/itertools` instead |
|
13
|
|
|
*/ |
|
14
|
|
|
class SortedSetMap |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var array|null |
|
18
|
|
|
*/ |
|
19
|
|
|
private $values = null; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Construct the set based on an array containing typically string keys and array values, where the |
|
23
|
|
|
* values correspond to unique parameter values per key. |
|
24
|
|
|
* |
|
25
|
|
|
* @param array $values |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct($values = array()) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->setValues($values); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Import all values from the array into the map, replacing all current values |
|
35
|
|
|
* |
|
36
|
|
|
* @param array $values |
|
37
|
|
|
* @return void |
|
38
|
|
|
*/ |
|
39
|
|
|
public function setValues($values) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->values = array(); |
|
42
|
|
|
foreach ($values as $key => $value) { |
|
43
|
|
|
$this->replace($key, (array)$value); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Add a value to the given map key. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $key |
|
52
|
|
|
* @param int|float|string|bool $value |
|
53
|
|
|
* @return void |
|
54
|
|
|
*/ |
|
55
|
|
|
public function add($key, $value) |
|
56
|
|
|
{ |
|
57
|
|
|
if (!isset($this->values[$key])) { |
|
58
|
|
|
$this->values[$key] = new SortedSet(); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
$this->values[$key]->add($value); |
|
61
|
|
|
$this->stateChanged(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Replaces the map key with the specified set of values. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $key |
|
69
|
|
|
* @param array $values |
|
70
|
|
|
* @return void |
|
71
|
|
|
*/ |
|
72
|
|
|
public function replace($key, $values) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->values[$key] = new SortedSet($values); |
|
|
|
|
|
|
75
|
|
|
$this->stateChanged(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Returns the set of values associated with the given key as an array. |
|
81
|
|
|
* Returns an empty array if the key is not present. |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $key |
|
84
|
|
|
* @return array |
|
85
|
|
|
*/ |
|
86
|
|
|
public function get($key) |
|
87
|
|
|
{ |
|
88
|
|
|
if (isset($this->values[$key])) { |
|
89
|
|
|
return $this->values[$key]->toArray(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return array(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Checks if a value is associated with the given key. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $key |
|
100
|
|
|
* @param int|float|string|bool $value |
|
101
|
|
|
* @return bool |
|
102
|
|
|
*/ |
|
103
|
|
|
public function contains($key, $value) |
|
104
|
|
|
{ |
|
105
|
|
|
if (isset($this->values[$key])) { |
|
106
|
|
|
return $this->values[$key]->contains($value); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return false; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Checks if the given key is present in the map. |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $key |
|
117
|
|
|
* @return bool |
|
118
|
|
|
*/ |
|
119
|
|
|
public function containsKey($key) |
|
120
|
|
|
{ |
|
121
|
|
|
return isset($this->values[$key]); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Removes the given value from the map associated with the given key. |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $key |
|
129
|
|
|
* @param int|float|string|bool $value |
|
130
|
|
|
* @return void |
|
131
|
|
|
*/ |
|
132
|
|
|
public function remove($key, $value) |
|
133
|
|
|
{ |
|
134
|
|
|
if (isset($this->values[$key])) { |
|
135
|
|
|
$this->values[$key]->remove($value); |
|
136
|
|
|
} |
|
137
|
|
|
$this->stateChanged(); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Removes an entire set of values associated with the given key. |
|
143
|
|
|
* |
|
144
|
|
|
* @param string $key |
|
145
|
|
|
* @return void |
|
146
|
|
|
*/ |
|
147
|
|
|
public function removeKey($key) |
|
148
|
|
|
{ |
|
149
|
|
|
if (isset($this->values[$key])) { |
|
150
|
|
|
unset($this->values[$key]); |
|
151
|
|
|
} |
|
152
|
|
|
$this->stateChanged(); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Merges a set of values into the given key's set. |
|
158
|
|
|
* |
|
159
|
|
|
* @param string $key |
|
160
|
|
|
* @param \Traversable $values |
|
161
|
|
|
* @return void |
|
162
|
|
|
*/ |
|
163
|
|
|
public function merge($key, $values) |
|
164
|
|
|
{ |
|
165
|
|
|
foreach ((array)$values as $value) { |
|
166
|
|
|
$this->add($key, $value); |
|
167
|
|
|
} |
|
168
|
|
|
$this->stateChanged(); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Merge an entire map into the current map. |
|
174
|
|
|
* |
|
175
|
|
|
* @param array $values |
|
176
|
|
|
* @return void |
|
177
|
|
|
*/ |
|
178
|
|
|
public function mergeAll(array $values) |
|
179
|
|
|
{ |
|
180
|
|
|
foreach ($values as $key => $value) { |
|
181
|
|
|
$this->merge($key, $value); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Returns the map as an array, with all values representing the set of |
|
188
|
|
|
* values associated with that key as an array |
|
189
|
|
|
* |
|
190
|
|
|
* @return array |
|
191
|
|
|
*/ |
|
192
|
|
|
public function toArray() |
|
193
|
|
|
{ |
|
194
|
|
|
$ret = array(); |
|
195
|
|
|
foreach (array_keys($this->values) as $key) { |
|
|
|
|
|
|
196
|
|
|
$ret[$key] = $this->get($key); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
return $ret; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Ensures all empty sets are removed, and sorts the sets by key name. |
|
205
|
|
|
* |
|
206
|
|
|
* @return void |
|
207
|
|
|
*/ |
|
208
|
|
|
private function stateChanged() |
|
209
|
|
|
{ |
|
210
|
|
|
$keys = array_keys($this->values); |
|
|
|
|
|
|
211
|
|
|
foreach ($keys as $key) { |
|
212
|
|
|
if (!count($this->values[$key])) { |
|
213
|
|
|
unset($this->values[$key]); |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
ksort($this->values); |
|
|
|
|
|
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Returns the number of values in the set. |
|
221
|
|
|
* |
|
222
|
|
|
* @return int |
|
223
|
|
|
*/ |
|
224
|
|
|
public function getCount() |
|
225
|
|
|
{ |
|
226
|
|
|
return count($this->values); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Implements the __clone magic method to clone internal SortedSets |
|
231
|
|
|
* |
|
232
|
|
|
* @return void |
|
233
|
|
|
*/ |
|
234
|
|
|
public function __clone() |
|
235
|
|
|
{ |
|
236
|
|
|
foreach (array_keys($this->values) as $key) { |
|
|
|
|
|
|
237
|
|
|
$this->values[$key] = clone $this->values[$key]; |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|