Collection   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 249
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 31
eloc 48
c 2
b 0
f 0
dl 0
loc 249
ccs 68
cts 68
cp 1
rs 9.92

22 Methods

Rating   Name   Duplication   Size   Complexity  
A reset() 0 7 3
A getIterator() 0 3 1
A has() 0 3 1
A __construct() 0 4 2
A first() 0 3 1
A put() 0 4 1
A pull() 0 8 2
A __toString() 0 3 1
A prepend() 0 4 1
A pop() 0 4 1
A get() 0 6 2
A isEmpty() 0 3 1
A all() 0 3 1
A each() 0 8 3
A shift() 0 3 1
A remove() 0 7 2
A random() 0 3 1
A implode() 0 3 1
A count() 0 3 1
A last() 0 3 1
A push() 0 8 2
A shuffle() 0 4 1
1
<?php
2
3
namespace Ballen\Collection;
4
5
use ArrayIterator;
6
7
/**
8
 * Collection
9
 *
10
 * A Collection class (library) which provides OOP replacement for the
11
 * traditional array data structure.
12
 *
13
 * @author Bobby Allen <[email protected]>
14
 * @license https://opensource.org/licenses/MIT
15
 * @link https://github.com/allebb/collection
16
 * @link http://bobbyallen.me
17
 *
18
 */
19
class Collection
20
{
21
22
    /**
23
     * The collection data.
24
     * @var array
25
     */
26
    private $items = [];
27
28
    /**
29
     * Create new instance of a Collection.
30
     * @param array $items
31
     */
32 35
    public function __construct(array $items = [])
33
    {
34 35
        if (!empty($items)) {
35 27
            $this->push($items);
36
        }
37
    }
38
39
    /**
40
     * Resets the collection with the specified array content.
41
     * @param array $items
42
     * @return Collection
43
     */
44 2
    public function reset($items = null)
45
    {
46 2
        $this->items = [];
47 2
        if ((func_get_args() > 0) && is_array($items)) {
48 1
            $this->items = $items;
49
        }
50 2
        return $this;
51
    }
52
53
    /**
54
     * Set an item or items into the the collection.
55
     * @param mixed $key
56
     * @param mixed $item
57
     * @return Collection
58
     */
59 1
    public function put($key, $item)
60
    {
61 1
        $this->items[$key] = $item;
62 1
        return $this;
63
    }
64
65
    /**
66
     * Push a new item (or array of items) into the collection onto the end
67
     * of the collection.
68
     * @param mixed $item
69
     * @return Collection
70
     */
71 33
    public function push($item)
72
    {
73 33
        if (!is_array($item)) {
74 3
            $this->items = array_merge($this->items, [$item]);
75 3
            return $this;
76
        }
77 30
        $this->items = array_merge($this->items, $item);
78 30
        return $this;
79
    }
80
81
    /**
82
     * Push an item (or array of items) onto the beginning of the collection.
83
     * @param mixed $item
84
     * @return Collection
85
     */
86 1
    public function prepend($item)
87
    {
88 1
        array_unshift($this->items, $item);
89 1
        return $this;
90
    }
91
92
    /**
93
     * Get and remove the first item from the collection.
94
     * @return mixed
95
     */
96 1
    public function shift()
97
    {
98 1
        return array_shift($this->items);
99
    }
100
101
    /**
102
     * Pop an item off the end of the collection.
103
     * @return Collection
104
     */
105 2
    public function pop()
106
    {
107 2
        array_pop($this->items);
108 2
        return $this;
109
    }
110
111
    /**
112
     * Pull an item from the collection and remove it from the collection.
113
     * @param string $key
114
     * @return mixed|false
115
     */
116 2
    public function pull($key)
117
    {
118 2
        if ($this->has($key)) {
119 1
            $pulled = $this->get($key);
120 1
            $this->remove($key);
121 1
            return $pulled;
122
        }
123 1
        return false;
124
    }
125
126
    /**
127
     * Removes an item from the collection.
128
     * @param string $key
129
     * @return Collection
130
     */
131 3
    public function remove($key)
132
    {
133 3
        if ($this->has($key)) {
134 3
            $this->items[$key] = null;
135 3
            unset($this->items[$key]);
136
        }
137 3
        return $this;
138
    }
139
140
    /**
141
     * Get all items from the collection.
142
     * @return CollectionExport
143
     */
144 11
    public function all()
145
    {
146 11
        return new CollectionExport($this->items);
147
    }
148
149
    /**
150
     * Checks if the collection has a the specified key set.
151
     * @param string $key The key name to check if it exists.
152
     * @return boolean
153
     */
154 8
    public function has($key)
155
    {
156 8
        return isset($this->items[$key]);
157
    }
158
159
    /**
160
     * Get a specific item from the collection.
161
     * @param string $key The collection (array) key to return.
162
     * @param mixed $default Optional default value if the key doesn't exist (defaulted to null)
163
     * @return mixed
164
     */
165 3
    public function get($key, $default = null)
166
    {
167 3
        if (!isset($this->items[$key])) {
168 1
            return $default;
169
        }
170 2
        return $this->items[$key];
171
    }
172
173
    /**
174
     * The total number of items in the collection.
175
     * @return int
176
     */
177 7
    public function count()
178
    {
179 7
        return count($this->items);
180
    }
181
182
    /**
183
     * Iterate over each of the items in the collection and execute the callback.
184
     * @param callable $callback
185
     * @return Collection
186
     */
187 2
    public function each(callable $callback)
188
    {
189 2
        foreach ($this->items as $key => $item) {
190 2
            if ($callback($key, $item) === false) {
191 1
                break;
192
            }
193
        }
194 2
        return $this;
195
    }
196
197
    /**
198
     * Retrieve a random item from the collection.
199
     * @return mixed
200
     */
201 1
    public function random()
202
    {
203 1
        return array_rand($this->items);
204
    }
205
206
    /**
207
     * Return the first item in the collection.
208
     * @return  mixed
209
     */
210 1
    public function first()
211
    {
212 1
        return array_values($this->items)[0];
213
    }
214
215
    /**
216
     * Returns the last item in the collection.
217
     * @return mixed
218
     */
219 1
    public function last()
220
    {
221 1
        return array_values($this->items)[$this->count() - 1];
222
    }
223
224
    /**
225
     * Shuffles (randomises) the items in the collection.
226
     * @return Collection
227
     */
228 1
    public function shuffle()
229
    {
230 1
        shuffle($this->items);
231 1
        return $this;
232
    }
233
234
    /**
235
     * Converts the collection into a string.
236
     * @return string
237
     */
238 1
    public function implode($glue = ' ')
239
    {
240 1
        return implode($glue, $this->items);
241
    }
242
243
    /**
244
     * Checks to see if the collection is empty.
245
     * @return boolean
246
     */
247 2
    public function isEmpty()
248
    {
249 2
        return empty($this->items);
250
    }
251
252
    /**
253
     * Get an iterator for the collection.
254
     * @return \ArrayIterator
255
     */
256 1
    public function getIterator()
257
    {
258 1
        return new ArrayIterator($this->items);
259
    }
260
261
    /**
262
     * Converts the collection to it's string representation (JSON)
263
     * @return string
264
     */
265 1
    public function __toString()
266
    {
267 1
        return $this->all()->toJson();
268
    }
269
}
270