|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Geotools library. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Antoine Corcy <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace League\Geotools; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @author Gabriel Bull <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class ArrayCollection implements \Countable, \IteratorAggregate, \ArrayAccess, \JsonSerializable |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $elements; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param array $elements |
|
26
|
|
|
*/ |
|
27
|
44 |
|
public function __construct(array $elements = array()) |
|
28
|
|
|
{ |
|
29
|
44 |
|
$this->elements = $elements; |
|
30
|
44 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @return array |
|
34
|
|
|
*/ |
|
35
|
3 |
|
public function toArray() |
|
36
|
|
|
{ |
|
37
|
3 |
|
return $this->elements; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritDoc} |
|
42
|
|
|
*/ |
|
43
|
1 |
|
public function jsonSerialize() |
|
44
|
|
|
{ |
|
45
|
1 |
|
return $this->elements; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritDoc} |
|
50
|
|
|
*/ |
|
51
|
2 |
|
public function offsetExists($offset) |
|
52
|
|
|
{ |
|
53
|
2 |
|
return isset($this->elements[$offset]) || array_key_exists($offset, $this->elements); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritDoc} |
|
58
|
|
|
*/ |
|
59
|
3 |
|
public function offsetGet($offset) |
|
60
|
|
|
{ |
|
61
|
3 |
|
return $this->get($offset); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritDoc} |
|
66
|
|
|
*/ |
|
67
|
2 |
|
public function offsetSet($offset, $value) |
|
68
|
|
|
{ |
|
69
|
2 |
|
$this->set($offset, $value); |
|
70
|
2 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritDoc} |
|
74
|
|
|
*/ |
|
75
|
2 |
|
public function offsetUnset($offset) |
|
76
|
|
|
{ |
|
77
|
2 |
|
return $this->remove($offset); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* {@inheritDoc} |
|
82
|
|
|
*/ |
|
83
|
23 |
|
public function count() |
|
84
|
|
|
{ |
|
85
|
23 |
|
return count($this->elements); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritDoc} |
|
90
|
|
|
*/ |
|
91
|
39 |
|
public function getIterator() |
|
92
|
|
|
{ |
|
93
|
39 |
|
return new \ArrayIterator($this->elements); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param string $key |
|
98
|
|
|
* @return null|mixed |
|
99
|
|
|
*/ |
|
100
|
12 |
|
public function get($key) |
|
101
|
|
|
{ |
|
102
|
12 |
|
if (isset($this->elements[$key])) { |
|
103
|
12 |
|
return $this->elements[$key]; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
8 |
|
return null; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param string $key |
|
111
|
|
|
* @param mixed $value |
|
112
|
|
|
*/ |
|
113
|
3 |
|
public function set($key, $value) |
|
114
|
|
|
{ |
|
115
|
3 |
|
$this->elements[$key] = $value; |
|
116
|
3 |
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param mixed $value |
|
120
|
|
|
* @return bool |
|
121
|
|
|
*/ |
|
122
|
2 |
|
public function add($value) |
|
123
|
|
|
{ |
|
124
|
2 |
|
$this->elements[] = $value; |
|
125
|
|
|
|
|
126
|
2 |
|
return true; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param string $key |
|
131
|
|
|
* @return null|mixed |
|
132
|
|
|
*/ |
|
133
|
4 |
|
public function remove($key) |
|
134
|
|
|
{ |
|
135
|
4 |
|
if (isset($this->elements[$key]) || array_key_exists($key, $this->elements)) { |
|
136
|
4 |
|
$removed = $this->elements[$key]; |
|
137
|
4 |
|
unset($this->elements[$key]); |
|
138
|
|
|
|
|
139
|
4 |
|
return $removed; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
2 |
|
return null; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param ArrayCollection $collection |
|
147
|
|
|
* @return ArrayCollection |
|
148
|
|
|
*/ |
|
149
|
3 |
|
public function merge(ArrayCollection $collection) |
|
150
|
|
|
{ |
|
151
|
3 |
|
$merged = clone $this; |
|
152
|
|
|
|
|
153
|
3 |
|
foreach ($collection as $key => $element) { |
|
154
|
3 |
|
if (is_int($key)) { |
|
155
|
3 |
|
$merged->add($element); |
|
156
|
3 |
|
} else { |
|
157
|
2 |
|
$merged->set($key, $element); |
|
158
|
|
|
} |
|
159
|
3 |
|
} |
|
160
|
|
|
|
|
161
|
3 |
|
return $merged; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|