Test Setup Failed
Branch version-2.0 (c21687)
by Sebastian
04:11
created

ArrayListTrait::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * Copyright (C) 2018 Sebastian Böttger <[email protected]>
4
 * You may use, distribute and modify this code under the
5
 * terms of the MIT license.
6
 *
7
 * You should have received a copy of the MIT license with
8
 * this file. If not, please visit: https://opensource.org/licenses/mit-license.php
9
 */
10
11
namespace Seboettg\Collection\ArrayList;
12
13
/**
14
 * Trait ArrayListTrait
15
 * @package Seboettg\Collection
16
 * @author Sebastian Böttger <[email protected]>
17
 */
18
trait ArrayListTrait
19
{
20
21
    use ArrayAccessTrait;
22
23
    /**
24
     * flush array list
25
     *
26
     * @return $this
27
     */
28
    public function clear()
29
    {
30
        $this->array = [];
0 ignored issues
show
Bug Best Practice introduced by
The property array does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
31
        return $this;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function get($key)
38
    {
39
        return isset($this->array[$key]) ? $this->array[$key] : null;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function current()
46
    {
47
        return current($this->array);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function next()
54
    {
55
        return next($this->array);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function prev()
62
    {
63
        return prev($this->array);
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function set($key, $element)
70
    {
71
        $this->array[$key] = $element;
0 ignored issues
show
Bug Best Practice introduced by
The property array does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
72
        return $this;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function setArray(array $array)
79
    {
80
        return $this->replace($array);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function append($element)
87
    {
88
        $this->array[] = $element;
0 ignored issues
show
Bug Best Practice introduced by
The property array does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
89
        return $this;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function add($key, $element)
96
    {
97
98
        if (!array_key_exists($key, $this->array)) {
99
            $this->array[$key] = $element;
0 ignored issues
show
Bug Best Practice introduced by
The property array does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
100
        } elseif (is_array($this->array[$key])) {
101
            $this->array[$key][] = $element;
102
        } else {
103
            $this->array[$key] = [$this->array[$key], $element];
104
        }
105
106
        return $this;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function remove($key)
113
    {
114
        unset($this->array[$key]);
115
        return $this;
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function hasKey($key)
122
    {
123
        return array_key_exists($key, $this->array);
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function hasElement($value)
130
    {
131
        $result = array_search($value, $this->array, true);
132
        return ($result !== false);
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function replace(array $data)
139
    {
140
        $this->array = $data;
0 ignored issues
show
Bug Best Practice introduced by
The property array does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
141
        return $this;
142
    }
143
144
    /**
145
     * Returns the first element
146
     * @return mixed
147
     */
148
    public function first()
149
    {
150
        reset($this->array);
151
        return $this->array[key($this->array)];
152
    }
153
154
    /**
155
     * Returns the last element
156
     * @return mixed
157
     */
158
    public function last()
159
    {
160
        $item = end($this->array);
161
        reset($this->array);
162
        return $item;
163
    }
164
165
    /**
166
     * {@inheritDoc}
167
     */
168
    public function toArray()
169
    {
170
        return $this->array;
171
    }
172
173
    /**
174
     * Shuffles this list (randomizes the order of the elements in). It uses the PHP function shuffle
175
     * @see http://php.net/manual/en/function.shuffle.php
176
     * @return ArrayListInterface|ArrayListTrait
177
     */
178
    public function shuffle()
179
    {
180
        shuffle($this->array);
181
        return $this;
182
    }
183
184
    /**
185
     * returns a clone of this ArrayList, filtered by the given closure function
186
     * @param \Closure $closure
187
     * @return ArrayListInterface|ArrayListTrait
188
     */
189
    public function filter(\Closure $closure)
190
    {
191
        $newInstance = new self();
192
        $newInstance->setArray(array_filter($this->array, $closure));
193
        return $newInstance;
194
    }
195
196
    /**
197
     * returns a clone of this ArrayList, filtered by the given array keys
198
     * @param array $keys
199
     * @return ArrayListInterface|ArrayListTrait
200
     */
201
    public function filterByKeys(array $keys)
202
    {
203
        /** @noinspection PhpMethodParametersCountMismatchInspection */
204
        $newInstance = new self();
205
        $newInstance->setArray(array_filter($this->array, function ($key) use ($keys) {
206
            return array_search($key, $keys) !== false;
207
        }, ARRAY_FILTER_USE_KEY));
208
        return $newInstance;
209
    }
210
211
    /**
212
     * returns a new ArrayList containing all the elements of this ArrayList after applying the callback function to each one.
213
     * @param \closure $mapFunction
214
     * @return ArrayListInterface|ArrayListTrait
215
     */
216
    public function map(\closure $mapFunction)
217
    {
218
        $newInstance = new self();
219
        $newInstance->setArray(array_map($mapFunction, $this->array));
220
        return $newInstance;
221
    }
222
223
    /**
224
     * Returns a new ArrayList containing an one-dimensional array of all elements of this ArrayList. Keys are going lost.
225
     * @return ArrayListInterface|ArrayListTrait
226
     */
227
    public function flatten()
228
    {
229
        $flattenedArray = [];
230
        array_walk_recursive($this->array, function ($item) use (&$flattenedArray) {
231
            $flattenedArray[] = $item;
232
        });
233
        $newInstance = new self();
234
        $newInstance->setArray($flattenedArray);
235
        return $newInstance;
236
    }
237
}
238