1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Almendra\Http; |
5
|
|
|
|
6
|
|
|
use Almendra\Http\Interfaces\CollectionInterface; |
7
|
|
|
|
8
|
|
|
use ArrayIterator; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Collection |
12
|
|
|
*/ |
13
|
|
|
class Collection implements CollectionInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The source data |
17
|
|
|
* |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $data = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Create new collection |
24
|
|
|
* |
25
|
|
|
* @param array $items Pre-populate collection with this key-value array |
26
|
|
|
*/ |
27
|
|
|
public function __construct(array $items = []) |
28
|
|
|
{ |
29
|
|
|
$this->replace($items); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Set collection item |
34
|
|
|
* |
35
|
|
|
* @param string $key The data key |
36
|
|
|
* @param mixed $value The data value |
37
|
|
|
*/ |
38
|
|
|
public function set($key, $value) |
39
|
|
|
{ |
40
|
|
|
$this->data[$key] = $value; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get collection item for key |
45
|
|
|
* |
46
|
|
|
* @param string $key The data key |
47
|
|
|
* @param mixed $default The default value to return if data key does not exist |
48
|
|
|
* |
49
|
|
|
* @return mixed The key's value, or the default value |
50
|
|
|
*/ |
51
|
|
|
public function get($key, $default = null) |
52
|
|
|
{ |
53
|
|
|
return $this->has($key) ? $this->data[$key] : $default; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Add item to collection, replacing existing items with the same data key |
58
|
|
|
* |
59
|
|
|
* @param array $items Key-value array of data to append to this collection |
60
|
|
|
*/ |
61
|
|
|
public function replace(array $items) |
62
|
|
|
{ |
63
|
|
|
foreach ($items as $key => $value) { |
64
|
|
|
$this->set($key, $value); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get all items in collection |
70
|
|
|
* |
71
|
|
|
* @return array The collection's source data |
72
|
|
|
*/ |
73
|
|
|
public function all() |
74
|
|
|
{ |
75
|
|
|
return $this->data; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get collection keys |
80
|
|
|
* |
81
|
|
|
* @return array The collection's source data keys |
82
|
|
|
*/ |
83
|
|
|
public function keys() |
84
|
|
|
{ |
85
|
|
|
return array_keys($this->data); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Does this collection have a given key? |
90
|
|
|
* |
91
|
|
|
* @param string $key The data key |
92
|
|
|
* |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
public function has($key) |
96
|
|
|
{ |
97
|
|
|
return array_key_exists($key, $this->data); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Remove item from collection |
102
|
|
|
* |
103
|
|
|
* @param string $key The data key |
104
|
|
|
*/ |
105
|
|
|
public function remove($key) |
106
|
|
|
{ |
107
|
|
|
unset($this->data[$key]); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Remove all items from collection |
112
|
|
|
*/ |
113
|
|
|
public function clear() |
114
|
|
|
{ |
115
|
|
|
$this->data = []; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/******************************************************************************** |
119
|
|
|
* ArrayAccess interface |
120
|
|
|
*******************************************************************************/ |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Does this collection have a given key? |
124
|
|
|
* |
125
|
|
|
* @param string $key The data key |
126
|
|
|
* |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
|
|
public function offsetExists($key) |
130
|
|
|
{ |
131
|
|
|
return $this->has($key); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get collection item for key |
136
|
|
|
* |
137
|
|
|
* @param string $key The data key |
138
|
|
|
* |
139
|
|
|
* @return mixed The key's value, or the default value |
140
|
|
|
*/ |
141
|
|
|
public function offsetGet($key) |
142
|
|
|
{ |
143
|
|
|
return $this->get($key); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Set collection item |
148
|
|
|
* |
149
|
|
|
* @param string $key The data key |
150
|
|
|
* @param mixed $value The data value |
151
|
|
|
*/ |
152
|
|
|
public function offsetSet($key, $value) |
153
|
|
|
{ |
154
|
|
|
$this->set($key, $value); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Remove item from collection |
159
|
|
|
* |
160
|
|
|
* @param string $key The data key |
161
|
|
|
*/ |
162
|
|
|
public function offsetUnset($key) |
163
|
|
|
{ |
164
|
|
|
$this->remove($key); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get number of items in collection |
169
|
|
|
* |
170
|
|
|
* @return int |
171
|
|
|
*/ |
172
|
|
|
public function count() |
173
|
|
|
{ |
174
|
|
|
return count($this->data); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/******************************************************************************** |
178
|
|
|
* IteratorAggregate interface |
179
|
|
|
*******************************************************************************/ |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Get collection iterator |
183
|
|
|
* |
184
|
|
|
* @return \ArrayIterator |
185
|
|
|
*/ |
186
|
|
|
public function getIterator() |
187
|
|
|
{ |
188
|
|
|
return new ArrayIterator($this->data); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|