Passed
Push — feature/events ( 9d660f...96fd77 )
by Mathieu
02:50
created

Collection::isEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Suricate;
6
7
class Collection implements
8
    \Iterator,
9
    \Countable,
10
    \ArrayAccess,
11
    Interfaces\ICollection
12
{
13
    protected $iteratorPosition = 0;
14
15
    protected $items = [];
16
    public $pagination = [
17
        'nbPages' => 0,
18
        'page' => 1,
19
        'nbItems' => 0
20
    ];
21
22 36
    public function __construct($items = [])
23
    {
24 36
        $this->items = $items;
25 36
    }
26
27 1
    public function paginate($nbItemPerPage, $currentPage = 1)
28
    {
29 1
        $this->pagination['page'] = $currentPage;
30 1
        $this->pagination['nbItems'] = count($this->items);
31 1
        $this->pagination['nbPages'] = ceil(
32 1
            $this->pagination['nbItems'] / $nbItemPerPage
33
        );
34
35 1
        $this->items = array_slice(
36 1
            $this->items,
37 1
            ($currentPage - 1) * $nbItemPerPage,
38
            $nbItemPerPage
39
        );
40
41 1
        return $this;
42
    }
43
44 1
    public function getPossibleValuesFor($args, $key = null)
45
    {
46 1
        if (!is_array($args)) {
47
            $args = [
48 1
                'format' => '%s',
49 1
                'data' => [$args]
50
            ];
51
        }
52
53 1
        $values = [];
54 1
        foreach ($this->items as $item) {
55 1
            $itemValues = [];
56 1
            foreach ($args['data'] as $arg) {
57 1
                $itemValues[] = dataGet($item, $arg);
58
            }
59
60 1
            $arrayKey = $key !== null ? dataGet($item, $key) : null;
61 1
            if (is_null($arrayKey)) {
62 1
                $values[] = vsprintf($args['format'], $itemValues);
63
            } else {
64 1
                $values[$arrayKey] = vsprintf($args['format'], $itemValues);
65
            }
66
        }
67
68 1
        return $values;
69
    }
70
71 1
    public function getValuesFor($name)
72
    {
73 1
        $values = [];
74 1
        foreach ($this->items as $item) {
75 1
            $values[] = dataGet($item, $name);
76
        }
77
78 1
        return $values;
79
    }
80
81 14
    public function getItems()
82
    {
83 14
        return $this->items;
84
    }
85
86
    /**
87
     * Implementation of countable interface
88
     *
89
     * @return int
90
     */
91 4
    public function count(): int
92
    {
93 4
        return count($this->items);
94
    }
95
96
    public function key()
97
    {
98
        return $this->iteratorPosition;
99
    }
100
101
    public function next()
102
    {
103
        ++$this->iteratorPosition;
104
    }
105
    public function current()
106
    {
107
        return $this->offsetGet($this->iteratorPosition);
108
    }
109
    public function rewind()
110
    {
111
        $this->iteratorPosition = 0;
112
    }
113
    public function valid()
114
    {
115
        return isset($this->items[$this->iteratorPosition]);
116
    }
117
    /**
118
     * Implementation of ArrayAccess interface
119
     *
120
     * @param  mixed $offset Offset to verify
121
     * @return bool
122
     */
123 2
    public function offsetExists($offset): bool
124
    {
125 2
        return \array_key_exists($offset, $this->items);
126
    }
127
128
    /**
129
     * Implementation of ArrayAccess Interface
130
     *
131
     * @param  mixed $offset Offset to get
132
     * @return mixed
133
     */
134 3
    public function offsetGet($offset)
135
    {
136 3
        if (array_key_exists($offset, $this->items)) {
137 3
            return $this->items[$offset];
138
        }
139 1
        return null;
140
    }
141
142
    /**
143
     * Implementation of ArrayAccess Interface
144
     *
145
     * @param mixed $offset Offset to set
146
     * @param mixed $value  Value to set
147
     */
148 1
    public function offsetSet($offset, $value)
149
    {
150 1
        if (is_null($offset)) {
151 1
            $this->items[] = $value;
152
        } else {
153 1
            $this->items[$offset] = $value;
154
        }
155 1
    }
156
157
    /**
158
     * Implementation of ArrayAccess Interface
159
     *
160
     * @param mixed $offset Offset to unset
161
     */
162 1
    public function offsetUnset($offset)
163
    {
164 1
        unset($this->items[$offset]);
165 1
    }
166
167
    // Helpers
168
169
    /**
170
     * Get first item of the collection
171
     *
172
     * @return mixed
173
     */
174 1
    public function first()
175
    {
176 1
        foreach ($this->items as $currentItem) {
177 1
            return $currentItem;
178
        }
179 1
    }
180
181
    /**
182
     * Get last item of the collection
183
     *
184
     * @return mixed
185
     */
186 1
    public function last()
187
    {
188 1
        if (count($this->items)) {
189 1
            return end($this->items);
190
        }
191
192 1
        return null;
193
    }
194
195
    /**
196
     * Check if collection is empty
197
     *
198
     * @return bool
199
     */
200 1
    public function isEmpty(): bool
201
    {
202 1
        return empty($this->items);
203
    }
204
205
    /**
206
     * Return the sum of the collection
207
     *
208
     * @param mixed $field Field to use for sum
209
     * @return double|integer
210
     */
211 1
    public function sum($field = null)
212
    {
213 1
        if ($field === null) {
214 1
            return array_sum($this->items);
215
        }
216 1
        $result = 0;
217 1
        foreach ($this->items as $item) {
218 1
            $result += dataGet($item, $field);
219
        }
220 1
        return $result;
221
    }
222
223
    public function random($nbItems = 1)
224
    {
225
        if ($this->isEmpty()) {
226
            return null;
227
        }
228
229
        $keys = array_rand($this->items, $nbItems);
230
231
        if (is_array($keys)) {
232
            return array_intersect_key($this->items, array_flip($keys));
233
        }
234
235
        return $this->items[$keys];
236
    }
237
238
    public function shuffle()
239
    {
240
        shuffle($this->items);
241
242
        return $this;
243
    }
244
245 1
    public function unique()
246
    {
247 1
        return new static(array_unique($this->items));
248
    }
249
250
    /**
251
     * Apply a closure to each element of the collection
252
     *
253
     * @param \Closure $callback Closure to apply
254
     * @return Collection
255
     */
256
    public function each(\Closure $callback): Collection
257
    {
258
        array_map($callback, $this->items);
259
        return $this;
260
    }
261
262
    /**
263
     * Sort a collection using a closure
264
     *
265
     * @param \Closure $closure Closure to apply for sorting, similar to uasort() closure
266
     * @return Collection
267
     */
268
    public function sort(\Closure $closure): Collection
269
    {
270
        uasort($this->items, $closure);
271
272
        return $this;
273
    }
274
275
    public function sortBy($field, $reverse = false)
276
    {
277
        if ($reverse) {
278
            $sortFunction = function ($a, $b) use ($field) {
279
                $first = dataGet($a, $field);
280
                $second = dataGet($b, $field);
281
                if ($first == $second) {
282
                    return 0;
283
                }
284
                return $first > $second ? -1 : 1;
285
            };
286
        } else {
287
            $sortFunction = function ($a, $b) use ($field) {
288
                $first = dataGet($a, $field);
289
                $second = dataGet($b, $field);
290
                if ($first == $second) {
291
                    return 0;
292
                }
293
                return $first < $second ? -1 : 1;
294
            };
295
        }
296
297
        usort($this->items, $sortFunction);
298
299
        return $this;
300
    }
301
302 1
    public function filter(\Closure $closure)
303
    {
304 1
        return new static(array_filter($this->items, $closure));
305
    }
306
307 1
    public function search($value, $strict = false)
308
    {
309 1
        return array_search($value, $this->items, $strict);
310
    }
311
312 1
    public function has($key)
313
    {
314 1
        return $this->offsetExists($key);
315
    }
316
317 1
    public function keys()
318
    {
319 1
        return array_keys($this->items);
320
    }
321
322 1
    public function prepend($item)
323
    {
324 1
        array_unshift($this->items, $item);
325
326 1
        return $this;
327
    }
328
329 1
    public function push($item)
330
    {
331 1
        $this->items[] = $item;
332
333 1
        return $this;
334
    }
335
336 1
    public function put($key, $val)
337
    {
338 1
        $this->items[$key] = $val;
339
340 1
        return $this;
341
    }
342 1
    public function shift()
343
    {
344 1
        return array_shift($this->items);
345
    }
346
347 1
    public function pop()
348
    {
349 1
        return array_pop($this->items);
350
    }
351
352 1
    public function reverse()
353
    {
354 1
        return new static(array_reverse($this->items));
355
    }
356
357 1
    public function reduce(callable $callback, $initial = null)
358
    {
359 1
        return array_reduce($this->items, $callback, $initial);
360
    }
361
362 2
    public function slice($offset, $length = null, $preserveKeys = false)
363
    {
364 2
        return new static(
365 2
            array_slice($this->items, $offset, $length, $preserveKeys)
366
        );
367
    }
368
369 1
    public function take($limit = null)
370
    {
371 1
        if ($limit < 0) {
372
            return $this->slice(abs($limit), $limit);
373
        }
374
375 1
        return $this->slice(0, $limit);
376
    }
377
378
    public function splice($offset, $length = null, $replacement = [])
379
    {
380
        return new static(
381
            array_splice($this->items, $offset, $length, $replacement)
382
        );
383
    }
384
385
    public function chunk($size, $preserveKeys = false)
386
    {
387
        $result = new static();
388
        foreach (array_chunk($this->items, $size, $preserveKeys) as $chunk) {
389
            $result->push(new static($chunk));
390
        }
391
        return $result;
392
    }
393
}
394