1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Collection library for PHP |
5
|
|
|
* |
6
|
|
|
* @link https://github.com/hiqdev/php-collection |
7
|
|
|
* @package php-collection |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
* @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace hiqdev\php\collection; |
13
|
|
|
|
14
|
|
|
use ArrayIterator; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Base Trait. |
18
|
|
|
* |
19
|
|
|
* @author Andrii Vasyliev <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
trait BaseTrait |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var array default items |
25
|
|
|
*/ |
26
|
|
|
protected static $_defaults = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array items |
30
|
|
|
*/ |
31
|
|
|
protected $_items = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Straight put an item. |
35
|
|
|
* |
36
|
|
|
* @param string $name item name. |
37
|
|
|
* @param array $value item value. |
38
|
|
|
*/ |
39
|
4 |
|
public function putItem($name, $value = null) |
40
|
|
|
{ |
41
|
|
|
if (is_null($name) || is_int($name)) { |
42
|
|
|
$this->_items[] = $value; |
43
|
|
|
} else { |
44
|
4 |
|
$this->_items[$name] = $value; |
45
|
|
|
} |
46
|
4 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get raw item. |
50
|
|
|
* |
51
|
|
|
* @param string $name item name. |
52
|
|
|
* |
53
|
|
|
* @return mixed item value. |
54
|
|
|
*/ |
55
|
|
|
public function rawItem($name, $default = null) |
56
|
|
|
{ |
57
|
|
|
return isset($this->_items[$name]) ? $this->_items[$name] : $default; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Adds an item. Doesn't touch if already exists. |
62
|
|
|
* |
63
|
|
|
* @param string $name item name. |
64
|
|
|
* @param array $value item value. |
65
|
|
|
* @param string|array $where where to put, @see setItem |
66
|
|
|
* |
67
|
|
|
* @return $this for chaining |
68
|
|
|
*/ |
69
|
|
|
public function addItem($name, $value = null, $where = '') |
70
|
|
|
{ |
71
|
|
|
if (!$this->hasItem($name)) { |
72
|
|
|
$this->setItem($name, $value, $where); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Sets an item. Silently resets if already exists and mov. |
80
|
|
|
* |
81
|
|
|
* @param string $name item name. |
82
|
|
|
* @param array $value item value. |
83
|
|
|
* @param string|array $where where to put, can be empty, first, last and array of before and after |
84
|
|
|
*/ |
85
|
8 |
|
public function setItem($name, $value = null, $where = '') |
86
|
|
|
{ |
87
|
8 |
|
if ($name === null || $where === '') { |
88
|
|
|
$this->putItem($name, $value); |
89
|
|
|
} else { |
90
|
|
|
$this->setItems([$name => $value], $where); |
91
|
4 |
|
} |
92
|
6 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns item by name. |
96
|
|
|
* |
97
|
|
|
* @param string $name item name. |
98
|
|
|
* |
99
|
|
|
* @return mixed item value. |
100
|
|
|
*/ |
101
|
5 |
|
public function getItem($name) |
102
|
|
|
{ |
103
|
5 |
|
return $this->_items[$name]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Check collection has the item. |
108
|
|
|
* |
109
|
|
|
* @param string $name item name. |
110
|
|
|
* |
111
|
|
|
* @return bool whether item exist. |
112
|
|
|
*/ |
113
|
|
|
public function hasItem($name) |
114
|
|
|
{ |
115
|
|
|
return array_key_exists($name, $this->_items); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function mergeItem($name, array $value) |
119
|
|
|
{ |
120
|
|
|
if (!is_null($name)) { |
121
|
|
|
$this->_items[$name] = ArrayHelper::merge($this->_items[$name], $value); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Check is item set. |
127
|
|
|
* |
128
|
|
|
* @param string $name item name. |
129
|
|
|
* |
130
|
|
|
* @return bool whether item is set. |
131
|
|
|
*/ |
132
|
|
|
public function issetItem($name) |
133
|
|
|
{ |
134
|
|
|
return isset($this->_items[$name]); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Delete an item. |
139
|
|
|
* |
140
|
|
|
* @param $name |
141
|
|
|
*/ |
142
|
1 |
|
public function unsetItem($name) |
143
|
|
|
{ |
144
|
1 |
|
unset($this->_items[$name]); |
145
|
1 |
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get specified items as array. |
149
|
|
|
* |
150
|
|
|
* @param mixed $keys specification |
151
|
|
|
* |
152
|
|
|
* @return array list of items |
153
|
|
|
*/ |
154
|
2 |
|
public function getItems($keys = null) |
155
|
|
|
{ |
156
|
|
|
return ArrayHelper::getItems($this->_items, $keys); |
157
|
2 |
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Straight put items. |
161
|
|
|
* @param array $items list of items |
162
|
|
|
* @see setItem |
163
|
|
|
*/ |
164
|
|
|
public function putItems(array $items) |
165
|
|
|
{ |
166
|
|
|
foreach ($items as $k => $v) { |
167
|
|
|
$this->putItem($k, $v); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Adds items to specified place. |
173
|
|
|
* @param array $items list of items |
174
|
|
|
* @param mixed $where |
175
|
|
|
* @see setItem() |
176
|
|
|
*/ |
177
|
4 |
|
public function setItems($items, $where = '') |
178
|
|
|
{ |
179
|
4 |
|
if (empty($items)) { |
180
|
|
|
return; |
181
|
|
|
} |
182
|
4 |
|
if (empty($where)) { |
183
|
|
|
$this->putItems($items); |
184
|
4 |
|
} elseif ($where === 'last') { |
185
|
|
|
$this->_items = ArrayHelper::insertLast($this->_items, $items); |
186
|
2 |
|
} elseif ($where === 'first') { |
187
|
|
|
$this->_items = ArrayHelper::insertFirst($this->_items, $items); |
188
|
|
|
} else { |
189
|
|
|
$this->_items = ArrayHelper::insertInside($this->_items, $items, $where); |
190
|
4 |
|
} |
191
|
4 |
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Adds items to specified place. |
195
|
|
|
* Does not touch those items that already exists. |
196
|
|
|
* @param array $items array of items. |
197
|
|
|
* @param string|array $where where to add. See [[setItem()]] |
198
|
|
|
* @return $this for chaining |
199
|
|
|
* |
200
|
|
|
* @see setItem() |
201
|
|
|
*/ |
202
|
|
|
public function addItems(array $items, $where = '') |
203
|
|
|
{ |
204
|
|
|
foreach (array_keys($items) as $k) { |
205
|
|
|
if (!is_int($k) && $this->hasItem($k)) { |
206
|
|
|
unset($items[$k]); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
if (!empty($items)) { |
210
|
|
|
$this->setItems($items, $where); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return $this; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function mergeItems(array $items) |
217
|
|
|
{ |
218
|
|
|
$this->_items = ArrayHelper::merge($this->_items, $items); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Unset specified items. |
223
|
|
|
* @param mixed $keys specification |
224
|
|
|
* @return array list of items |
225
|
|
|
*/ |
226
|
2 |
|
public function unsetItems($keys = null) |
227
|
|
|
{ |
228
|
|
|
if (is_null($keys)) { |
229
|
1 |
|
$this->_items = []; |
230
|
|
|
} elseif (is_scalar($keys)) { |
231
|
|
|
unset($this->_items[$keys]); |
232
|
|
|
} else { |
233
|
|
|
foreach ($keys as $k) { |
234
|
|
|
unset($this->_items[$k]); |
235
|
|
|
} |
236
|
2 |
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
1 |
|
public function resetItems(array $items) |
240
|
|
|
{ |
241
|
1 |
|
$this->_items = $items; |
242
|
|
|
} |
243
|
|
|
/** |
244
|
|
|
* Get keys. |
245
|
|
|
* |
246
|
|
|
* @return array for chaining |
247
|
|
|
*/ |
248
|
|
|
public function keys() |
249
|
|
|
{ |
250
|
|
|
return array_keys($this->_items); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* The default implementation of this method returns [[attributes()]] indexed by the same attribute names. |
255
|
|
|
* |
256
|
|
|
* @return array the list of field names or field definitions. |
257
|
|
|
* |
258
|
|
|
* @see toArray() |
259
|
|
|
*/ |
260
|
|
|
public function fields() |
261
|
|
|
{ |
262
|
|
|
$fields = $this->keys(); |
263
|
|
|
|
264
|
|
|
return array_combine($fields, $fields); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Returns number of items in the collection. |
269
|
|
|
* |
270
|
|
|
* @return int |
271
|
|
|
*/ |
272
|
|
|
public function count() |
273
|
|
|
{ |
274
|
|
|
return count($this->_items); |
275
|
|
|
} |
276
|
|
|
/** |
277
|
|
|
* Returns the element at the specified offset. |
278
|
|
|
* This method is required by the SPL interface `ArrayAccess`. |
279
|
|
|
* It is implicitly called when you use something like `$value = $collection[$offset];`. |
280
|
|
|
* |
281
|
|
|
* @param mixed $offset the offset to retrieve element. |
282
|
|
|
* |
283
|
|
|
* @return mixed the element at the offset, null if no element is found at the offset |
284
|
|
|
*/ |
285
|
|
|
public function offsetGet($offset) |
286
|
|
|
{ |
287
|
|
|
return $this->getItem($offset); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Sets the element at the specified offset. |
292
|
|
|
* This method is required by the SPL interface `ArrayAccess`. |
293
|
|
|
* It is implicitly called when you use something like `$collection[$offset] = $value;`. |
294
|
|
|
* |
295
|
|
|
* @param int $offset the offset to set element |
296
|
|
|
* @param mixed $value the element value |
297
|
|
|
*/ |
298
|
|
|
public function offsetSet($offset, $value) |
299
|
|
|
{ |
300
|
|
|
$this->setItem($offset, $value); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Returns whether there is an element at the specified offset. |
305
|
|
|
* This method is required by the SPL interface `ArrayAccess`. |
306
|
|
|
* It is implicitly called when you use something like `isset($collection[$offset])`. |
307
|
|
|
* |
308
|
|
|
* @param mixed $offset the offset to check on |
309
|
|
|
* |
310
|
|
|
* @return bool |
311
|
|
|
*/ |
312
|
|
|
public function offsetExists($offset) |
313
|
|
|
{ |
314
|
|
|
return $this->hasItem($offset); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Sets the element value at the specified offset to null. |
319
|
|
|
* This method is required by the SPL interface ArrayAccess. |
320
|
|
|
* It is implicitly called when you use something like `unset($collection[$offset])`. |
321
|
|
|
* |
322
|
|
|
* @param mixed $offset the offset to unset element |
323
|
|
|
*/ |
324
|
|
|
public function offsetUnset($offset) |
325
|
|
|
{ |
326
|
|
|
$this->unsetItem($offset); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Method for IteratorAggregate interface. |
331
|
|
|
* Enables foreach'ing the object. |
332
|
|
|
* |
333
|
|
|
* @return ArrayIterator |
334
|
|
|
*/ |
335
|
|
|
public function getIterator() |
336
|
|
|
{ |
337
|
|
|
return new ArrayIterator($this->_items); |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|