|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
// Copyright (c) italolelis. All rights reserved. See License.txt in the project root for license information. |
|
4
|
|
|
namespace Collections; |
|
5
|
|
|
|
|
6
|
|
|
use Collections\Iterator\MapIterator; |
|
7
|
|
|
use Collections\Traits\MapLikeTrait; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Map is an ordered dictionary-style collection. |
|
11
|
|
|
* |
|
12
|
|
|
* Like all objects in PHP, Maps have reference-like semantics. When a caller |
|
13
|
|
|
* passes a Map to a callee, the callee can modify the Map and the caller will |
|
14
|
|
|
* see the changes. Maps do not have "copy-on-write" semantics. |
|
15
|
|
|
* |
|
16
|
|
|
* Maps preserve insertion order of key/value pairs. When iterating over a Map, |
|
17
|
|
|
* the key/value pairs appear in the order they were inserted. Also, Maps do |
|
18
|
|
|
* not automagically convert integer-like string keys (ex. "123") into integer |
|
19
|
|
|
* keys. |
|
20
|
|
|
* |
|
21
|
|
|
* Maps only support integer keys and string keys. If a key of a different |
|
22
|
|
|
* type is used, an exception will be thrown. |
|
23
|
|
|
* |
|
24
|
|
|
* Maps support "$m[$k]" style syntax for getting and setting values by key. |
|
25
|
|
|
* Maps also support "isset($m[$k])" and "empty($m[$k])" syntax, and they |
|
26
|
22 |
|
* provide similar semantics as arrays. Adding an element using "$m[] = .." |
|
27
|
|
|
* syntax is not supported. |
|
28
|
22 |
|
* |
|
29
|
|
|
* Maps do not support iterating while new keys are being added or elements |
|
30
|
22 |
|
* are being removed. When a new key is added or an element is removed, all |
|
31
|
|
|
* iterators that point to the Map shall be considered invalid. |
|
32
|
|
|
* |
|
33
|
|
|
* Maps do not support taking elements by reference. If binding assignment (=&) |
|
34
|
|
|
* is used with an element of a Map, or if an element of a Map is passed by |
|
35
|
|
|
* reference, of if a Map is used with foreach by reference, an exception will |
|
36
|
8 |
|
* be thrown. |
|
37
|
|
|
*/ |
|
38
|
8 |
|
class Dictionary implements MapInterface, \ArrayAccess |
|
39
|
1 |
|
{ |
|
40
|
|
|
use MapLikeTrait, SortTrait; |
|
41
|
|
|
|
|
42
|
7 |
|
public function __construct($array = null) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->init($array); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
12 |
|
* Gets the collection's iterator |
|
49
|
|
|
* @return MapIterator |
|
50
|
12 |
|
*/ |
|
51
|
1 |
|
public function getIterator() |
|
52
|
|
|
{ |
|
53
|
12 |
|
return new MapIterator($this->container); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|