Completed
Push — master ( 6af305...285eba )
by Andrii
02:22
created

BaseTrait::addItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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