Completed
Push — master ( a5f6dd...461f27 )
by Andrii
09:23
created

src/BaseTrait.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
trait BaseTrait
20
{
21
    /**
22
     * @var array default items
23
     */
24
    protected static $_defaults = [];
25
26
    /**
27
     * @var array items
28
     */
29
    protected $_items = [];
30
31
    /**
32
     * Straight put an item.
33
     *
34
     * @param string $name  item name.
35
     * @param array  $value item value.
36
     */
37 4
    public function putItem($name, $value = null)
38
    {
39 4
        if (is_null($name) || is_int($name)) {
40
            $this->_items[] = $value;
41
        } else {
42 4
            $this->_items[$name] = $value;
43
        }
44 4
    }
45
46
    /**
47
     * Get raw item.
48
     *
49
     * @param string $name item name.
50
     *
51
     * @return mixed item value.
52
     */
53
    public function rawItem($name, $default = null)
54
    {
55
        return isset($this->_items[$name]) ? $this->_items[$name] : $default;
56
    }
57
58
    /**
59
     * Adds an item. Doesn't touch if already exists.
60
     *
61
     * @param string       $name  item name.
62
     * @param array        $value item value.
63
     * @param string|array $where where to put, @see setItem
64
     *
65
     * @return $this for chaining
66
     */
67
    public function addItem($name, $value = null, $where = '')
68
    {
69
        if (!$this->hasItem($name)) {
70
            $this->setItem($name, $value, $where);
71
        }
72
73
        return $this;
74
    }
75
76
    /**
77
     * Sets an item. Silently resets if already exists and mov.
78
     *
79
     * @param string       $name  item name.
80
     * @param array        $value item value.
81
     * @param string|array $where where to put, can be empty, first, last and array of before and after
82
     */
83 8
    public function setItem($name, $value = null, $where = '')
84
    {
85 8
        if ($name === null || $where === '') {
86 4
            $this->putItem($name, $value);
87 4
        } else {
88 4
            $this->setItems([$name => $value], $where);
89
        }
90 8
    }
91
92
    /**
93
     * Returns item by name.
94
     *
95
     * @param string $name item name.
96
     *
97
     * @return mixed item value.
98
     */
99 5
    public function getItem($name)
100
    {
101 5
        return $this->_items[$name];
102
    }
103
104
    /**
105
     * Check collection has the item.
106
     *
107
     * @param string $name item name.
108
     *
109
     * @return bool whether item exist.
110
     */
111 10
    public function hasItem($name)
112
    {
113 10
        return array_key_exists($name, $this->_items);
114
    }
115
116
    public function mergeItem($name, array $value)
117
    {
118
        if (!is_null($name)) {
119
            $this->_items[$name] = ArrayHelper::merge($this->_items[$name], $value);
120
        }
121
    }
122
123
    /**
124
     * Check is item set.
125
     *
126
     * @param string $name item name.
127
     *
128
     * @return bool whether item is set.
129
     */
130 1
    public function issetItem($name)
131
    {
132 1
        return isset($this->_items[$name]);
133
    }
134
135
    /**
136
     * Delete an item.
137
     *
138
     * @param $name
139
     */
140 2
    public function unsetItem($name)
141
    {
142 2
        unset($this->_items[$name]);
143 2
    }
144
145
    /**
146
     * Get specified items as array.
147
     *
148
     * @param mixed $keys specification
149
     *
150
     * @return array list of items
151
     */
152 2
    public function getItems($keys = null)
153
    {
154 2
        return ArrayHelper::getItems($this->_items, $keys);
155
    }
156
157
    /**
158
     * Straight put items.
159
     *
160
     * @param array $items list of items
161
     *
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
     *
174
     * @param array $items list of items
175
     * @param mixed $where
176
     *
177
     * @see setItem()
178
     */
179 4
    public function setItems($items, $where = '')
180
    {
181 4
        if (!$items) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $items of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
182
            return;
183 4
        } elseif ($where === '') {
184
            $this->putItems($items);
185 4
        } elseif ($where === 'last') {
186 2
            $this->_items = ArrayHelper::insertLast($this->_items, $items);
187 4
        } elseif ($where === 'first') {
188 2
            $this->_items = ArrayHelper::insertFirst($this->_items, $items);
189 2
        } else {
190
            $this->_items = ArrayHelper::insertInside($this->_items, $items, $where);
191
        }
192 4
    }
193
194
    /**
195
     * Adds items to specified place.
196
     * Does not touch those items that already exists.
197
     *
198
     * @param array        $items array of items.
199
     * @param string|array $where where to add. See [[setItem()]]
200
     *
201
     * @return $this for chaining
202
     *
203
     * @see setItem()
204
     */
205
    public function addItems(array $items, $where = '')
206
    {
207
        foreach ($items as $k => $v) {
208
            if (!is_int($k) && $this->hasItem($k)) {
209
                unset($items[$k]);
210
            }
211
        }
212
        if ($items) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $items of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
213
            $this->setItems($items, $where);
214
        }
215
216
        return $this;
217
    }
218
219
    public function mergeItems(array $items)
220
    {
221
        $this->_items = ArrayHelper::merge($this->_items, $items);
222
    }
223
224
    /**
225
     * Unset specified items.
226
     *
227
     * @param mixed $keys specification
228
     *
229
     * @return array list of items
230
     */
231 2
    public function unsetItems($keys = null)
232
    {
233 2
        if (is_null($keys)) {
234 1
            $this->_items = [];
235 2
        } elseif (is_scalar($keys)) {
236 1
            unset($this->_items[$keys]);
237 1
        } else {
238 1
            foreach ($keys as $k) {
239 1
                unset($this->_items[$k]);
240 1
            }
241
        }
242 2
    }
243
244 1
    public function resetItems(array $items)
245
    {
246 1
        $this->_items = $items;
247 1
    }
248
    /**
249
     * Get keys.
250
     *
251
     * @return array for chaining
252
     */
253 9
    public function keys()
254
    {
255 9
        return array_keys($this->_items);
256
    }
257
258
    /**
259
     * The default implementation of this method returns [[attributes()]] indexed by the same attribute names.
260
     *
261
     * @return array the list of field names or field definitions.
262
     *
263
     * @see toArray()
264
     */
265
    public function fields()
266
    {
267
        $fields = $this->keys();
268
269
        return array_combine($fields, $fields);
270
    }
271
272
    /**
273
     * Returns number of items in the collection.
274
     *
275
     * @return int
276
     */
277
    public function count()
278
    {
279
        return count($this->_items);
280
    }
281
    /**
282
     * Returns the element at the specified offset.
283
     * This method is required by the SPL interface `ArrayAccess`.
284
     * It is implicitly called when you use something like `$value = $collection[$offset];`.
285
     *
286
     * @param mixed $offset the offset to retrieve element.
287
     *
288
     * @return mixed the element at the offset, null if no element is found at the offset
289
     */
290
    public function offsetGet($offset)
291
    {
292
        return $this->getItem($offset);
293
    }
294
295
    /**
296
     * Sets the element at the specified offset.
297
     * This method is required by the SPL interface `ArrayAccess`.
298
     * It is implicitly called when you use something like `$collection[$offset] = $value;`.
299
     *
300
     * @param int   $offset the offset to set element
301
     * @param mixed $value  the element value
302
     */
303
    public function offsetSet($offset, $value)
304
    {
305
        $this->setItem($offset, $value);
306
    }
307
308
    /**
309
     * Returns whether there is an element at the specified offset.
310
     * This method is required by the SPL interface `ArrayAccess`.
311
     * It is implicitly called when you use something like `isset($collection[$offset])`.
312
     *
313
     * @param mixed $offset the offset to check on
314
     *
315
     * @return bool
316
     */
317
    public function offsetExists($offset)
318
    {
319
        return $this->hasItem($offset);
320
    }
321
322
    /**
323
     * Sets the element value at the specified offset to null.
324
     * This method is required by the SPL interface ArrayAccess.
325
     * It is implicitly called when you use something like `unset($collection[$offset])`.
326
     *
327
     * @param mixed $offset the offset to unset element
328
     */
329
    public function offsetUnset($offset)
330
    {
331
        $this->unsetItem($offset);
332
    }
333
334
    /**
335
     * Method for IteratorAggregate interface.
336
     * Enables foreach'ing the object.
337
     *
338
     * @return ArrayIterator
339
     */
340 1
    public function getIterator()
341
    {
342 1
        return new ArrayIterator($this->_items);
343
    }
344
}
345