Passed
Branch version-1.x (4225f6)
by Sebastian
02:41 queued 58s
created

ArrayListTrait::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: seboettg
5
 * Date: 09.05.18
6
 * Time: 17:50
7
 */
8
9
namespace Seboettg\Collection;
10
11
12
trait ArrayListTrait
13
{
14
    /**
15
     * internal array
16
     *
17
     * @var array
18
     */
19
    protected $array;
20
21
    /**
22
     * flush array list
23
     *
24
     * @return $this
25
     */
26 1
    public function clear()
27
    {
28 1
        $this->array = [];
29 1
        return $this;
30
    }
31
32
    /**
33
     * returns element with key $key
34
     * @param $key
35
     * @return mixed|null
36
     */
37 6
    public function get($key)
38
    {
39 6
        return isset($this->array[$key]) ? $this->array[$key] : null;
40
    }
41
42
    /**
43
     * Returns the value of the array element that's currently being pointed to by the
44
     * internal pointer. It does not move the pointer in any way. If the
45
     * internal pointer points beyond the end of the elements list or the array is
46
     * empty, current returns false.
47
     *
48
     * @return mixed|false
49
     */
50 2
    public function current()
51
    {
52 2
        return current($this->array);
53
    }
54
55
    /**
56
     * Advance the internal array pointer of an array.
57
     * Returns the array value in the next place that's pointed to by the
58
     * internal array pointer, or false if there are no more elements.
59
     *
60
     * @return mixed|false
61
     */
62 3
    public function next()
63
    {
64 3
        return next($this->array);
65
    }
66
67
    /**
68
     * Rewind the internal array pointer.
69
     * Returns the array value in the previous place that's pointed to by
70
     * the internal array pointer, or false if there are no more
71
     *
72
     * @return mixed|false
73
     */
74 1
    public function prev()
75
    {
76 1
        return prev($this->array);
77
    }
78
79
    /**
80
     * Inserts or replaces the element at the specified position in this list with the specified element.
81
     *
82
     * @param $key
83
     * @param $element
84
     * @return $this
85
     */
86 1
    public function set($key, $element)
87
    {
88 1
        $this->array[$key] = $element;
89 1
        return $this;
90
    }
91
92
    /**
93
     * overrides contents of ArrayList with the contents of $array
94
     * @param array $array
95
     * @return $this
96
     */
97 1
    public function setArray(array $array)
98
    {
99 1
        return $this->replace($array);
100
    }
101
102
    /**
103
     * Appends the specified element to the end of this list.
104
     *
105
     * @param $element
106
     * @return $this
107
     */
108 2
    public function append($element)
109
    {
110 2
        $this->array[] = $element;
111 2
        return $this;
112
    }
113
114
    /**
115
     * Inserts the specified element at the specified position in this list. If an other element already exist at the
116
     * specified position the affected positions will transformed into a numerated array. As well the existing element
117
     * as the specified element will be appended to this array.
118
     *
119
     * @param $key
120
     * @param $element
121
     * @return $this
122
     */
123 1
    public function add($key, $element)
124
    {
125
126 1
        if (!array_key_exists($key, $this->array)) {
127 1
            $this->array[$key] = $element;
128 1
        } elseif (is_array($this->array[$key])) {
129
            $this->array[$key][] = $element;
130
        } else {
131 1
            $this->array[$key] = [$this->array[$key], $element];
132
        }
133
134 1
        return $this;
135
    }
136
137
    /**
138
     * Removes the element at the specified position in this list.
139
     *
140
     * @param $key
141
     * @return $this
142
     */
143 1
    public function remove($key)
144
    {
145 1
        unset($this->array[$key]);
146 1
        return $this;
147
    }
148
149
    /**
150
     * Returns true if an element exists on the specified position.
151
     *
152
     * @param mixed $key
153
     * @return bool
154
     */
155 4
    public function hasKey($key)
156
    {
157 4
        return array_key_exists($key, $this->array);
158
    }
159
160
    /**
161
     * Returns true if the specified value exists in this list. Uses PHP's array_search function
162
     * @link http://php.net/manual/en/function.array-search.php
163
     *
164
     * @param string $value
165
     *
166
     * @return mixed
167
     */
168 2
    public function hasValue($value)
169
    {
170 2
        $result = array_search($value, $this->array, true);
171 2
        return ($result !== false);
172
    }
173
174
    /**
175
     * replaces this list by the specified array
176
     * @param array $data
177
     *
178
     * @return ArrayList
179
     */
180 5
    public function replace(array $data)
181
    {
182 5
        $this->array = $data;
183 5
        return $this;
184
    }
185
186
    /**
187
     * {@inheritDoc}
188
     */
189 1
    public function getIterator()
190
    {
191 1
        return new \ArrayIterator($this->array);
192
    }
193
194
    /**
195
     * Offset to retrieve
196
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
197
     * @param mixed $offset The offset to retrieve.
198
     *
199
     * @return mixed Can return all value types.
200
     */
201 2
    public function offsetGet($offset)
202
    {
203 2
        return isset($this->array[$offset]) ? $this->array[$offset] : null;
204
    }
205
206
    /**
207
     * Offset to set
208
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
209
     * @param mixed $offset The offset to assign the value to.
210
     * @param mixed $value The value to set.
211
     */
212 1
    public function offsetSet($offset, $value)
213
    {
214 1
        $this->array[$offset] = $value;
215 1
    }
216
217
    /**
218
     * Whether a offset exists
219
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
220
     *
221
     * @param  mixed $offset
222
     * @return bool
223
     */
224 1
    public function offsetExists($offset)
225
    {
226 1
        return isset($this->array[$offset]);
227
    }
228
229
    /**
230
     * Offset to unset
231
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
232
     * @param mixed $offset The offset to unset.
233
     */
234 1
    public function offsetUnset($offset)
235
    {
236 1
        unset($this->array[$offset]);
237 1
    }
238
239
    /**
240
     * {@inheritDoc}
241
     */
242 11
    public function toArray()
243
    {
244 11
        return $this->array;
245
    }
246
247
    /**
248
     * {@inheritDoc}
249
     */
250 6
    public function count()
251
    {
252 6
        return count($this->array);
253
    }
254
255
    /**
256
     * Shuffles this list (randomizes the order of the elements in). It uses the PHP function shuffle
257
     * @see http://php.net/manual/en/function.shuffle.php
258
     * @return $this
259
     */
260 1
    public function shuffle() {
261 1
        shuffle($this->array);
262 1
        return $this;
263
    }
264
265
    /**
266
     * returns a clone of this ArrayList, filtered by the given closure function
267
     * @param \Closure $closure
268
     * @return ArrayList
269
     */
270 1
    public function filter(\Closure $closure)
271
    {
272 1
        return new ArrayList(array_filter($this->array, $closure));
273
    }
274
275
    /**
276
     * returns a clone of this ArrayList, filtered by the given array keys
277
     * @param array $keys
278
     * @return ArrayList
279
     */
280 1
    public function filterByKeys(array $keys)
281
    {
282 1
        return new ArrayList(
283 1
            array_filter($this->array, function($key) use ($keys) {
284 1
                return array_search($key, $keys) !== false;
285 1
            }, ARRAY_FILTER_USE_KEY)
286
        );
287
    }
288
289
    /**
290
     * returns a new ArrayList containing all the elements of this ArrayList after applying the callback function to each one.
291
     * @param \closure $mapFunction
292
     * @return ArrayList
293
     */
294 1
    public function map(\closure $mapFunction)
295
    {
296 1
        return new ArrayList(array_map($mapFunction, $this->array));
297
    }
298
299
    /**
300
     * Returns a new ArrayList containing an one-dimensional array of all elements of this ArrayList. Keys are going lost.
301
     * @return ArrayList
302
     */
303 1
    public function flatten()
304
    {
305 1
        $flattenedArray = [];
306 1
        array_walk_recursive($this->array, function($item) use(&$flattenedArray) {
307 1
            $flattenedArray[] = $item;
308 1
        });
309 1
        return new ArrayList($flattenedArray);
310
    }
311
}
312