Passed
Push — master ( 68320a...e4fc6b )
by Sebastian
01:43
created

ArrayList::current()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * Copyright (C) 2016 Sebastian Böttger <[email protected]>
5
 * You may use, distribute and modify this code under the
6
 * terms of the MIT license.
7
 *
8
 * You should have received a copy of the MIT license with
9
 * this file. If not, please visit: https://opensource.org/licenses/mit-license.php
10
 */
11
12
namespace Seboettg\Collection;
13
14
/**
15
 * ArrayList is a useful wrapper class for an array, similar to Java's ArrayList
16
 * @package Seboettg\Collection
17
 *
18
 * @author Sebastian Böttger <[email protected]>
19
 */
20
class ArrayList implements Collection
21
{
22
23
    /**
24
     * internal array
25
     *
26
     * @var array
27
     */
28
    protected $array;
29
30
    /**
31
     * ArrayList constructor.
32
     * @param array $data
33
     */
34
    public function __construct(array $data = [])
35
    {
36
        $this->array = $data;
37
    }
38
39
    /**
40
     * flush array list
41
     *
42
     * @return $this
43
     */
44
    public function clear()
45
    {
46
        $this->array = [];
47
        return $this;
48
    }
49
50
    /**
51
     * returns element with key $key
52
     * @param $key
53
     * @return mixed|null
54
     */
55
    public function get($key)
56
    {
57
        return isset($this->array[$key]) ? $this->array[$key] : null;
58
    }
59
60
    /**
61
     * Returns the value of the array element that's currently being pointed to by the
62
     * internal pointer. It does not move the pointer in any way. If the
63
     * internal pointer points beyond the end of the elements list or the array is
64
     * empty, current returns false.
65
     *
66
     * @return mixed|false
67
     */
68
    public function current()
69
    {
70
        return current($this->array);
71
    }
72
73
    /**
74
     * Advance the internal array pointer of an array.
75
     * Returns the array value in the next place that's pointed to by the
76
     * internal array pointer, or false if there are no more elements.
77
     *
78
     * @return mixed|false
79
     */
80
    public function next()
81
    {
82
        return next($this->array);
83
    }
84
85
    /**
86
     * Rewind the internal array pointer.
87
     * Returns the array value in the previous place that's pointed to by
88
     * the internal array pointer, or false if there are no more
89
     *
90
     * @return mixed|false
91
     */
92
    public function prev()
93
    {
94
        return prev($this->array);
95
    }
96
97
    /**
98
     * Inserts or replaces the element at the specified position in this list with the specified element.
99
     *
100
     * @param $key
101
     * @param $element
102
     * @return $this
103
     */
104
    public function set($key, $element)
105
    {
106
        $this->array[$key] = $element;
107
        return $this;
108
    }
109
110
    /**
111
     * overrides contents of ArrayList with the contents of $array
112
     * @param array $array
113
     * @return $this
114
     */
115
    public function setArray(array $array)
116
    {
117
        return $this->replace($array);
118
    }
119
120
    /**
121
     * Appends the specified element to the end of this list.
122
     *
123
     * @param $element
124
     * @return $this
125
     */
126
    public function append($element)
127
    {
128
        $this->array[] = $element;
129
        return $this;
130
    }
131
132
    /**
133
     * Inserts the specified element at the specified position in this list. If an other element already exist at the
134
     * specified position the affected positions will transformed into a numerated array. As well the existing element
135
     * as the specified element will be appended to this array.
136
     *
137
     * @param $key
138
     * @param $element
139
     * @return $this
140
     */
141
    public function add($key, $element)
142
    {
143
144
        if (!array_key_exists($key, $this->array)) {
145
            $this->array[$key] = $element;
146
        } elseif (is_array($this->array[$key])) {
147
            $this->array[$key][] = $element;
148
        } else {
149
            $this->array[$key] = [$this->array[$key], $element];
150
        }
151
152
        return $this;
153
    }
154
155
    /**
156
     * Removes the element at the specified position in this list.
157
     *
158
     * @param $key
159
     * @return $this
160
     */
161
    public function remove($key)
162
    {
163
        unset($this->array[$key]);
164
        return $this;
165
    }
166
167
    /**
168
     * Returns true if an element exists on the specified position.
169
     *
170
     * @param mixed $key
171
     * @return bool
172
     */
173
    public function hasKey($key)
174
    {
175
        return array_key_exists($key, $this->array);
176
    }
177
178
    /**
179
     * Returns true if the specified value exists in this list. Uses PHP's array_search function
180
     * @link http://php.net/manual/en/function.array-search.php
181
     *
182
     * @param string $value
183
     *
184
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use boolean.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
185
     */
186
    public function hasValue($value)
187
    {
188
        $result = array_search($value, $this->array, true);
189
        return ($result !== false);
190
    }
191
192
    /**
193
     * replaces this list by the specified array
194
     * @param array $data
195
     *
196
     * @return ArrayList
197
     */
198
    public function replace(array $data)
199
    {
200
        $this->array = $data;
201
        return $this;
202
    }
203
204
    /**
205
     * {@inheritDoc}
206
     */
207
    public function getIterator()
208
    {
209
        return new \ArrayIterator($this->array);
210
    }
211
212
    /**
213
     * Offset to retrieve
214
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
215
     * @param mixed $offset The offset to retrieve.
216
     *
217
     * @return mixed Can return all value types.
218
     */
219
    public function offsetGet($offset)
220
    {
221
        return isset($this->array[$offset]) ? $this->array[$offset] : null;
222
    }
223
224
    /**
225
     * Offset to set
226
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
227
     * @param mixed $offset The offset to assign the value to.
228
     * @param mixed $value The value to set.
229
     */
230
    public function offsetSet($offset, $value)
231
    {
232
        $this->array[$offset] = $value;
233
    }
234
235
    /**
236
     * Whether a offset exists
237
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
238
     *
239
     * @param  mixed $offset
240
     * @return bool
241
     */
242
    public function offsetExists($offset)
243
    {
244
        return isset($this->array[$offset]);
245
    }
246
247
    /**
248
     * Offset to unset
249
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
250
     * @param mixed $offset The offset to unset.
251
     */
252
    public function offsetUnset($offset)
253
    {
254
        unset($this->array[$offset]);
255
    }
256
257
    /**
258
     * {@inheritDoc}
259
     */
260
    public function toArray()
261
    {
262
        return $this->array;
263
    }
264
265
    /**
266
     * {@inheritDoc}
267
     */
268
    public function count()
269
    {
270
        return count($this->array);
271
    }
272
273
    /**
274
     * Shuffles this list (randomizes the order of the elements in). It uses the PHP function shuffle
275
     * @see http://php.net/manual/en/function.shuffle.php
276
     * @return $this
277
     */
278
    public function shuffle() {
279
        shuffle($this->array);
280
        return $this;
281
    }
282
283
    /**
284
     * returns a clone of this ArrayList, filtered by the given closure function
285
     * @param \Closure $closure
286
     * @return ArrayList
287
     */
288
    public function filter(\Closure $closure)
289
    {
290
        return new self(array_filter($this->array, $closure));
291
    }
292
293
    /**
294
     * returns a clone of this ArrayList, filtered by the given array keys
295
     * @param array $keys
296
     * @return ArrayList
297
     */
298
    public function filterByKeys(array $keys)
299
    {
300
        return new ArrayList(
301
            array_filter($this->array, function($key) use ($keys) {
302
                return array_search($key, $keys) !== false;
303
            }, ARRAY_FILTER_USE_KEY)
304
        );
305
    }
306
}
307