Completed
Push — master ( c77456...de0918 )
by Sebastian
07:46
created

ArrayListTrait::remove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
188
     */
189
    public function replace(array $data)
190
    {
191
        $this->array = $data;
192
        return $this;
193
    }
194
195
    /**
196
     * {@inheritDoc}
197
     */
198
    public function getIterator()
199
    {
200
        return new \ArrayIterator($this->array);
201
    }
202
203
    /**
204
     * Offset to retrieve
205
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
206
     * @param mixed $offset The offset to retrieve.
207
     *
208
     * @return mixed Can return all value types.
209
     */
210
    public function offsetGet($offset)
211
    {
212
        return isset($this->array[$offset]) ? $this->array[$offset] : null;
213
    }
214
215
    /**
216
     * Offset to set
217
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
218
     * @param mixed $offset The offset to assign the value to.
219
     * @param mixed $value The value to set.
220
     */
221
    public function offsetSet($offset, $value)
222
    {
223
        $this->array[$offset] = $value;
224
    }
225
226
    /**
227
     * Whether a offset exists
228
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
229
     *
230
     * @param  mixed $offset
231
     * @return bool
232
     */
233
    public function offsetExists($offset)
0 ignored issues
show
Coding Style introduced by
function offsetExists() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
234
    {
235
        return isset($this->array[$offset]);
236
    }
237
238
    /**
239
     * Offset to unset
240
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
241
     * @param mixed $offset The offset to unset.
242
     */
243
    public function offsetUnset($offset)
244
    {
245
        unset($this->array[$offset]);
246
    }
247
248
    /**
249
     * {@inheritDoc}
250
     */
251
    public function toArray()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
252
    {
253
        return $this->array;
254
    }
255
256
    /**
257
     * {@inheritDoc}
258
     */
259
    public function count()
260
    {
261
        return count($this->array);
262
    }
263
264
    /**
265
     * Shuffles this list (randomizes the order of the elements in). It uses the PHP function shuffle
266
     * @see http://php.net/manual/en/function.shuffle.php
267
     * @return $this
268
     */
269
    public function shuffle() {
270
        shuffle($this->array);
271
        return $this;
272
    }
273
274
    /**
275
     * returns a clone of this ArrayList, filtered by the given closure function
276
     * @param \Closure $closure
277
     * @return ArrayList
0 ignored issues
show
Documentation introduced by
Should the return type not be ArrayListTrait?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
278
     */
279
    public function filter(\Closure $closure)
280
    {
281
        return new self(array_filter($this->array, $closure));
282
    }
283
284
    /**
285
     * returns a clone of this ArrayList, filtered by the given array keys
286
     * @param array $keys
287
     * @return ArrayList
288
     */
289
    public function filterByKeys(array $keys)
290
    {
291
        return new ArrayList(
292
            array_filter($this->array, function($key) use ($keys) {
293
                return array_search($key, $keys) !== false;
294
            }, ARRAY_FILTER_USE_KEY)
295
        );
296
    }
297
}