|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* \AppserverIo\Collections\Dictionary |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tim Wagner <[email protected]> |
|
15
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link https://github.com/appserver-io/collections |
|
18
|
|
|
* @link http://www.appserver.io |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace AppserverIo\Collections; |
|
22
|
|
|
|
|
23
|
|
|
use AppserverIo\Lang\Object; |
|
24
|
|
|
use AppserverIo\Lang\ClassCastException; |
|
25
|
|
|
use AppserverIo\Lang\NullPointerException; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* This class is the implementation of a Dictionary. |
|
29
|
|
|
* |
|
30
|
|
|
* A dictionary uses objects as keys instead of integers |
|
31
|
|
|
* like a HashMap. |
|
32
|
|
|
* |
|
33
|
|
|
* @author Tim Wagner <[email protected]> |
|
34
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
|
35
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
36
|
|
|
* @link https://github.com/appserver-io/collections |
|
37
|
|
|
* @link http://www.appserver.io |
|
38
|
|
|
*/ |
|
39
|
|
|
class Dictionary extends Object implements SetInterface |
|
40
|
|
|
{ |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Holds the keys with the objects. |
|
44
|
|
|
* |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $keys = array(); |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Holds the items of the Dictionary |
|
51
|
|
|
* |
|
52
|
|
|
* @var array |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $items = array(); |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Holds the internal counter for the keys. |
|
58
|
|
|
* |
|
59
|
|
|
* @var integer |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $key = 0; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Standard constructor that adds the values of the array passed |
|
65
|
|
|
* as parameter to the internal member variable. |
|
66
|
|
|
* |
|
67
|
|
|
* @param array $items An array to initialize the Dictionary |
|
68
|
|
|
* |
|
69
|
|
|
* @throws \AppserverIo\Lang\ClassCastException Is thrown if nor NULL or an object that is not a Dictionary is passed |
|
70
|
|
|
* @see \AppserverIo\Collections\Dictionary::add($key, $value) |
|
71
|
|
|
*/ |
|
72
|
|
|
public function __construct($items = null) |
|
73
|
|
|
{ |
|
74
|
|
|
// check if NULL is passed, is yes, to nothing |
|
75
|
|
|
if (is_null($items)) { |
|
76
|
|
|
return; |
|
77
|
|
|
} |
|
78
|
|
|
// check if an array is passed |
|
79
|
|
|
if (is_array($items)) { |
|
80
|
|
|
// initialize the Dictionary with the values of the passed array |
|
81
|
|
|
foreach ($items as $key => $item) { |
|
82
|
|
|
$this->add($key, $item); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
// if not a array is passed throw an exception |
|
87
|
|
|
throw new ClassCastException('Passed object is not an array'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* This method adds the passed value with the passed key |
|
92
|
|
|
* to the Dictionary. |
|
93
|
|
|
* |
|
94
|
|
|
* @param object $key Holds the key as object to add the value under |
|
95
|
|
|
* @param mixed $value Holds the value to add |
|
96
|
|
|
* |
|
97
|
|
|
* @return \AppserverIo\Collections\Dictionary The instance |
|
98
|
|
|
* @throws \AppserverIo\Collections\InvalidKeyException Is thrown if the passed key is NOT an object |
|
99
|
|
|
* @throws \AppserverIo\Lang\NullPointerException Is thrown if the passed key OR value are NULL |
|
100
|
|
|
*/ |
|
101
|
|
|
public function add($key, $value) |
|
102
|
|
|
{ |
|
103
|
|
|
if (is_null($key)) { |
|
104
|
|
|
throw new NullPointerException('Passed key is null'); |
|
105
|
|
|
} |
|
106
|
|
|
if (is_null($value)) { |
|
107
|
|
|
throw new NullPointerException('Passed value with key ' . $key . ' is null'); |
|
108
|
|
|
} |
|
109
|
|
|
if (!is_object($key)) { |
|
110
|
|
|
throw new InvalidKeyException('Passed key has to be an object'); |
|
111
|
|
|
} |
|
112
|
|
|
// get the next id |
|
113
|
|
|
$id = $this->key ++; |
|
114
|
|
|
// add the key to the array with the keys |
|
115
|
|
|
$this->keys[$id] = $key; |
|
116
|
|
|
// and add the value with the IDENTICAL id to internal |
|
117
|
|
|
// array with the values |
|
118
|
|
|
$this->items[$id] = $value; |
|
119
|
|
|
// return the instance |
|
120
|
|
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* This method returns the element with the passed key |
|
125
|
|
|
* from the Dictionary. |
|
126
|
|
|
* |
|
127
|
|
|
* @param object $key Holds the key of the element to return |
|
128
|
|
|
* |
|
129
|
|
|
* @return mixed The requested element |
|
130
|
|
|
* @throws \AppserverIo\Collections\InvalidKeyException Is thrown if the passed key is NOT an object |
|
131
|
|
|
* @throws \AppserverIo\Lang\NullPointerException Is thrown if the passed key OR value are NULL |
|
132
|
|
|
* @throws \AppserverIo\Collections\IndexOutOfBoundsException Is thrown if no element with the passed key exists in the Dictionary |
|
133
|
|
|
*/ |
|
134
|
|
|
public function get($key) |
|
135
|
|
|
{ |
|
136
|
|
|
if (is_null($key)) { |
|
137
|
|
|
throw new NullPointerException('Passed key is null'); |
|
138
|
|
|
} |
|
139
|
|
|
if (!is_object($key)) { |
|
140
|
|
|
throw new InvalidKeyException('Passed key has to be an object'); |
|
141
|
|
|
} |
|
142
|
|
|
// run over all keys and check if one is equal to the passed one |
|
143
|
|
|
foreach ($this->keys as $id => $value) { |
|
144
|
|
|
// if the actual is equal to the passed key .. |
|
145
|
|
|
if ($key == $value) { |
|
146
|
|
|
// return the item with the passed key |
|
147
|
|
|
if (array_key_exists($id, $this->items)) { |
|
148
|
|
|
return $this->items[$id]; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
// if no value is found throw an exception |
|
153
|
|
|
throw new IndexOutOfBoundsException('Index out of bounds'); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* This method initializes the Collection and removes |
|
158
|
|
|
* all exiting entries. |
|
159
|
|
|
* |
|
160
|
|
|
* @return void |
|
161
|
|
|
* @see \AppserverIo\Collections\CollectionInterface::clear() |
|
162
|
|
|
*/ |
|
163
|
|
|
public function clear() |
|
164
|
|
|
{ |
|
165
|
|
|
// initialize the internal arrays and keys |
|
166
|
|
|
$this->keys = array(); |
|
167
|
|
|
$this->items = array(); |
|
168
|
|
|
$this->key = 0; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* This method returns the number of entries of the Collection. |
|
173
|
|
|
* |
|
174
|
|
|
* @return integer The number of entries |
|
175
|
|
|
*/ |
|
176
|
|
|
public function size() |
|
177
|
|
|
{ |
|
178
|
|
|
return sizeof($this->keys); // return the size of the keys array |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* This method checks if the element with the passed |
|
183
|
|
|
* key exists in the Dictionary. |
|
184
|
|
|
* |
|
185
|
|
|
* @param object $key Holds the key to check the elements of the Dictionary for |
|
186
|
|
|
* |
|
187
|
|
|
* @return boolean Returns true if an element with the passed key exists in the Dictionary |
|
188
|
|
|
* @throws \AppserverIo\Collections\InvalidKeyException Is thrown if the passed key is NOT an object |
|
189
|
|
|
* @throws \AppserverIo\Lang\NullPointerException Is thrown if the passed key is NULL |
|
190
|
|
|
*/ |
|
191
|
|
View Code Duplication |
public function exists($key) |
|
|
|
|
|
|
192
|
|
|
{ |
|
193
|
|
|
if (is_null($key)) { |
|
194
|
|
|
throw new NullPointerException('Passed key is null'); |
|
195
|
|
|
} |
|
196
|
|
|
if (!is_object($key)) { |
|
197
|
|
|
throw new InvalidKeyException('Passed key has to be an object'); |
|
198
|
|
|
} |
|
199
|
|
|
// run over all keys and check if one is equal to the passed one |
|
200
|
|
|
foreach ($this->keys as $id => $value) { |
|
201
|
|
|
// if the actual is equal to the passed key .. |
|
202
|
|
|
if ($key == $value) { |
|
203
|
|
|
// return TRUE if the key is found |
|
204
|
|
|
return true; |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
// return FALSE if the key is not found |
|
208
|
|
|
return false; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* This returns true if the Collection has no |
|
213
|
|
|
* entries, otherwise false. |
|
214
|
|
|
* |
|
215
|
|
|
* @return boolean |
|
216
|
|
|
* @see \AppserverIo\Collections\CollectionInterface::isEmpty() |
|
217
|
|
|
*/ |
|
218
|
|
|
public function isEmpty() |
|
219
|
|
|
{ |
|
220
|
|
|
// if no items are set return true |
|
221
|
|
|
if ($this->key == 0) { |
|
222
|
|
|
return true; |
|
223
|
|
|
} |
|
224
|
|
|
return false; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* This method returns an array with the |
|
229
|
|
|
* items of the Dictionary. |
|
230
|
|
|
* |
|
231
|
|
|
* The keys are lost in the array. |
|
232
|
|
|
* |
|
233
|
|
|
* @return array Holds an array with the items of the Dictionary |
|
234
|
|
|
* @see \AppserverIo\Collections\CollectionInterface::toArray() |
|
235
|
|
|
*/ |
|
236
|
|
|
public function toArray() |
|
237
|
|
|
{ |
|
238
|
|
|
return $this->items; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* This method returns the keys as |
|
243
|
|
|
* an array. |
|
244
|
|
|
* |
|
245
|
|
|
* @return array Holds an array with the keys |
|
246
|
|
|
*/ |
|
247
|
|
|
public function keysToArray() |
|
248
|
|
|
{ |
|
249
|
|
|
return $this->keys; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* This method removes the element with the passed |
|
254
|
|
|
* key, that has to be an object, from the Dictionary. |
|
255
|
|
|
* |
|
256
|
|
|
* @param object $key Holds the key of the element to remove |
|
257
|
|
|
* |
|
258
|
|
|
* @return void |
|
259
|
|
|
* @throws \AppserverIo\Lang\NullPointerException Is thrown if the passed key is NULL |
|
260
|
|
|
* @throws \AppserverIo\Collections\InvalidKeyException Is thrown if the passed key is NOT an object |
|
261
|
|
|
* @throws \AppserverIo\Collections\IndexOutOfBoundsException Is thrown if no element with the passed key exists in the Dictionary |
|
262
|
|
|
*/ |
|
263
|
|
View Code Duplication |
public function remove($key) |
|
|
|
|
|
|
264
|
|
|
{ |
|
265
|
|
|
if (is_null($key)) { |
|
266
|
|
|
throw new NullPointerException('Passed key is null'); |
|
267
|
|
|
} |
|
268
|
|
|
if (!is_object($key)) { |
|
269
|
|
|
throw new InvalidKeyException('Passed key has to be an object'); |
|
270
|
|
|
} |
|
271
|
|
|
// run over all keys and check if one is equal to the passed one |
|
272
|
|
|
foreach ($this->keys as $id => $value) { |
|
273
|
|
|
// if the actual is equal to the passed key .. |
|
274
|
|
|
if ($key == $value) { |
|
275
|
|
|
// unset the elements |
|
276
|
|
|
unset($this->items[$id]); |
|
277
|
|
|
unset($this->keys[$id]); |
|
278
|
|
|
return; |
|
279
|
|
|
} |
|
280
|
|
|
} |
|
281
|
|
|
// throw an exception if key is not found in internal array |
|
282
|
|
|
throw new IndexOutOfBoundsException('Index out of bounds'); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* This method appends all elements of the |
|
287
|
|
|
* passed array to the Dictionary. |
|
288
|
|
|
* |
|
289
|
|
|
* @param array $array Holds the array with the values to add |
|
290
|
|
|
* |
|
291
|
|
|
* @return void |
|
292
|
|
|
*/ |
|
293
|
|
|
public function addAll($array) |
|
294
|
|
|
{ |
|
295
|
|
|
foreach ($array as $key => $value) { |
|
296
|
|
|
$this->add($key, $value); |
|
|
|
|
|
|
297
|
|
|
} |
|
298
|
|
|
} |
|
299
|
|
|
} |
|
300
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: