Completed
Push — master ( c718c6...f978f1 )
by Bobby
02:15
created

Collection::remove()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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