Test Setup Failed
Push — master ( ece6de...d8d046 )
by Luke
06:36
created

Collection::isEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
    public function __construct(array $items = [])
24
    {
25
        $this->items = $items;
26
        $this->rewind();
27
    }
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
    public static function factory(array $items = [])
38
    {
39
        return new Collection($items);
40
    }
41
42
    /**
43
     * Get collection as an array
44
     *
45
     * @return array
46
     */
47
    public function toArray()
48
    {
49
        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
    public function has($key)
60
    {
61
        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
    public function get($key, $default = null)
73
    {
74
        if ($this->has($key)) {
75
            return $this->items[$key];
76
        }
77
78
        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
    public function add($value)
89
    {
90
        $this->items[] = $value;
91
92
        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
    public function set($key, $value)
104
    {
105
        $this->items[$key] = $value;
106
107
        return $this;
108
    }
109
110
    /**
111
     * Delete an item by key
112
     *
113
     * @param mixed $key
114
     *
115
     * @return $this
116
     */
117
    public function delete($key)
118
    {
119
        unset($this->items[$key]);
120
121
        return $this;
122
    }
123
124
    /**
125
     * Clear the collection of all its items.
126
     *
127
     * @return $this
128
     */
129
    public function clear()
130
    {
131
        $this->items = [];
132
133
        return $this;
134
    }
135
136
    /**
137
     * Determine if collection contains given value
138
     *
139
     * @param mixed $val
140
     *
141
     * @return bool
142
     */
143
    public function contains($val)
144
    {
145
        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
    public function pull($key)
156
    {
157
        if ($this->has($key)) {
158
            $value = $this->get($key);
159
            $this->delete($key);
160
            return $value;
161
        }
162
    }
163
164
    /**
165
     * Join collection items using a delimiter
166
     *
167
     * @param string $delim
168
     *
169
     * @return string
170
     */
171
    public function join($delim = '')
172
    {
173
        return implode($delim, $this->items);
174
    }
175
176
    /**
177
     * Determine if collection has any items
178
     *
179
     * @return bool
180
     */
181
    public function isEmpty()
182
    {
183
        return $this->count() == 0;
184
    }
185
186
    /** ++++                  ++++ **/
187
    /** ++ Interface Compliance ++ **/
188
    /** ++++                  ++++ **/
189
190
    /**
191
     * @return array
192
     */
193
    public function jsonSerialize()
194
    {
195
        return $this->toArray();
196
    }
197
198
    /** ++++                  ++++ **/
199
    /** ++ Array Access Methods ++ **/
200
    /** ++++                  ++++ **/
201
202
    /**
203
     * {@inheritDoc}
204
     */
205
    public function offsetExists($offset)
206
    {
207
        return $this->has($offset);
208
    }
209
210
    /**
211
     * {@inheritDoc}
212
     */
213
    public function offsetGet($offset)
214
    {
215
        if (!$this->has($offset)) {
216
            throw new RuntimeException("Unknown offset: {$offset}");
217
        }
218
219
        return $this->get($offset);
220
    }
221
222
    /**
223
     * {@inheritDoc}
224
     */
225
    public function offsetUnset($offset)
226
    {
227
        $this->delete($offset);
228
    }
229
230
    /**
231
     * {@inheritDoc}
232
     */
233
    public function offsetSet($offset, $value)
234
    {
235
        if (!isset($offset)) {
236
            $this->add($value);
237
        }
238
239
        $this->set($offset, $value);
240
    }
241
242
    /** ++++                  ++++ **/
243
    /** ++   Iterator Methods   ++ **/
244
    /** ++++                  ++++ **/
245
246
    /**
247
     * {@inheritDoc}
248
     */
249
    public function current()
250
    {
251
        return current($this->items);
252
    }
253
254
    /**
255
     * {@inheritDoc}
256
     */
257
    public function key()
258
    {
259
        return key($this->items);
260
    }
261
262
    /**
263
     * {@inheritDoc}
264
     */
265
    public function next()
266
    {
267
        return next($this->items);
268
    }
269
270
    /**
271
     * {@inheritDoc}
272
     */
273
    public function rewind()
274
    {
275
        reset($this->items);
276
    }
277
278
    /**
279
     * {@inheritDoc}
280
     */
281
    public function valid()
282
    {
283
        return $this->has(key($this->items));
284
    }
285
286
    /** ++++                  ++++ **/
287
    /** ++   Countable Method   ++ **/
288
    /** ++++                  ++++ **/
289
290
    /**
291
     * {@inheritDoc}
292
     */
293
    public function count()
294
    {
295
        return count($this->items);
296
    }
297
}