Completed
Push — master ( 92e184...6392fd )
by Luke
05:33
created

Collection::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
namespace Noz\Collection;
3
4
use Countable;
5
use JsonSerializable;
6
use Iterator;
7
use ArrayAccess;
8
use RuntimeException;
9
10
/**
11
 * Nozavroni Collection
12
 */
13
class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable
14
{
15
    /** @var array */
16
    protected $items;
17
18
    /**
19
     * Collection constructor.
20
     *
21
     * @param array $items
22
     */
23 22
    public function __construct(array $items = [])
24
    {
25 22
        $this->items = $items;
26 22
        $this->rewind();
27 22
    }
28
29
    /**
30
     * Generate a collection from an array of items.
31
     * I created this method so that it's possible to extend a collection more easily.
32
     *
33
     * @param array $items
34
     *
35
     * @return Collection
36
     */
37 2
    public static function factory(array $items = [])
38
    {
39 2
        return new Collection($items);
40
    }
41
42
    /**
43
     * Get collection as an array
44
     *
45
     * @return array
46
     */
47 7
    public function toArray()
48
    {
49 7
        return $this->items;
50
    }
51
52
    /**
53
     * Determine if collection has a given key
54
     *
55
     * @param mixed $key The key to look for
56
     *
57
     * @return bool
58
     */
59 11
    public function has($key)
60
    {
61 11
        return isset($this->items[$key]) || array_key_exists($key, $this->items);
62
    }
63
64
    /**
65
     * Get item by key, with an optional default return value
66
     *
67
     * @param mixed $key
68
     * @param mixed $default
69
     *
70
     * @return mixed
71
     */
72 5
    public function get($key, $default = null)
73
    {
74 5
        if ($this->has($key)) {
75 5
            return $this->items[$key];
76
        }
77
78 3
        return $default;
79
    }
80
81
    /**
82
     * Add an item with no regard to key
83
     *
84
     * @param mixed $value
85
     *
86
     * @return $this
87
     */
88 3
    public function add($value)
89
    {
90 3
        $this->items[] = $value;
91
92 3
        return $this;
93
    }
94
95
    /**
96
     * Set an item at a given key
97
     *
98
     * @param mixed $key
99
     * @param mixed $value
100
     *
101
     * @return $this
102
     */
103 2
    public function set($key, $value)
104
    {
105 2
        $this->items[$key] = $value;
106
107 2
        return $this;
108
    }
109
110
    /**
111
     * Delete an item by key
112
     *
113
     * @param mixed $key
114
     *
115
     * @return $this
116
     */
117 3
    public function delete($key)
118
    {
119 3
        unset($this->items[$key]);
120
121 3
        return $this;
122
    }
123
124
    /**
125
     * Clear the collection of all its items.
126
     *
127
     * @return $this
128
     */
129 1
    public function clear()
130
    {
131 1
        $this->items = [];
132
133 1
        return $this;
134
    }
135
136
    /**
137
     * Determine if collection contains given value
138
     *
139
     * @param mixed $val
140
     *
141
     * @return bool
142
     */
143 1
    public function contains($val)
144
    {
145 1
        return in_array($val, $this->items, true);
146
    }
147
148
    /**
149
     * Fetch item from collection by key and remove it from collection
150
     *
151
     * @param mixed $key
152
     *
153
     * @return mixed
154
     */
155 1
    public function pull($key)
156
    {
157 1
        if ($this->has($key)) {
158 1
            $value = $this->get($key);
159 1
            $this->delete($key);
160 1
            return $value;
161
        }
162 1
    }
163
164
    /**
165
     * Join collection items using a delimiter
166
     *
167
     * @param string $delim
168
     *
169
     * @return string
170
     */
171 1
    public function join($delim = '')
172
    {
173 1
        return implode($delim, $this->items);
174
    }
175
176
    /**
177
     * Determine if collection has any items
178
     *
179
     * @return bool
180
     */
181 3
    public function isEmpty()
182
    {
183 3
        return $this->count() == 0;
184
    }
185
186
187
188
    /** ++++                  ++++ **/
189
    /** ++ Interface Compliance ++ **/
190
    /** ++++                  ++++ **/
191
192
    /**
193
     * @return array
194
     */
195 1
    public function jsonSerialize()
196
    {
197 1
        return $this->toArray();
198
    }
199
200
    /** ++++                  ++++ **/
201
    /** ++ Array Access Methods ++ **/
202
    /** ++++                  ++++ **/
203
204
    /**
205
     * {@inheritDoc}
206
     */
207 1
    public function offsetExists($offset)
208
    {
209 1
        return $this->has($offset);
210
    }
211
212
    /**
213
     * {@inheritDoc}
214
     */
215 2
    public function offsetGet($offset)
216
    {
217 2
        if (!$this->has($offset)) {
218 1
            throw new RuntimeException("Unknown offset: {$offset}");
219
        }
220
221 1
        return $this->get($offset);
222
    }
223
224
    /**
225
     * {@inheritDoc}
226
     */
227 1
    public function offsetUnset($offset)
228
    {
229 1
        $this->delete($offset);
230 1
    }
231
232
    /**
233
     * {@inheritDoc}
234
     */
235 1
    public function offsetSet($offset, $value)
236
    {
237 1
        if (!isset($offset)) {
238 1
            $this->add($value);
239 1
        }
240
241 1
        $this->set($offset, $value);
242 1
    }
243
244
    /** ++++                  ++++ **/
245
    /** ++   Iterator Methods   ++ **/
246
    /** ++++                  ++++ **/
247
248
    /**
249
     * {@inheritDoc}
250
     */
251 1
    public function current()
252
    {
253 1
        return current($this->items);
254
    }
255
256
    /**
257
     * {@inheritDoc}
258
     */
259 1
    public function key()
260
    {
261 1
        return key($this->items);
262
    }
263
264
    /**
265
     * {@inheritDoc}
266
     */
267 1
    public function next()
268
    {
269 1
        return next($this->items);
270
    }
271
272
    /**
273
     * {@inheritDoc}
274
     */
275 22
    public function rewind()
276
    {
277 22
        reset($this->items);
278 22
    }
279
280
    /**
281
     * {@inheritDoc}
282
     */
283 1
    public function valid()
284
    {
285 1
        return $this->has(key($this->items));
286
    }
287
288
    /** ++++                  ++++ **/
289
    /** ++   Countable Method   ++ **/
290
    /** ++++                  ++++ **/
291
292
    /**
293
     * {@inheritDoc}
294
     */
295 4
    public function count()
296
    {
297 4
        return count($this->items);
298
    }
299
}