Completed
Push — master ( 53b748...dab7a1 )
by Matthew
03:13
created

Collection::reduceToCollection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PublishingKit\Utilities\Collections;
6
7
use Countable;
8
use ArrayAccess;
9
use IteratorAggregate;
10
use Serializable;
11
use OutOfBoundsException;
12
13
/**
14
 * Collection class
15
 */
16
class Collection extends BaseCollection implements Countable, ArrayAccess, IteratorAggregate, Serializable
17
{
18
    /**
19
     * Does item exist?
20
     *
21
     * @param mixed $offset The offset.
22
     * @return boolean
23
     */
24 3
    public function offsetExists($offset)
25
    {
26 3
        return isset($this->source[$offset]);
27
    }
28
29
    /**
30
     * Get offset
31
     *
32
     * @param mixed $offset The offset.
33
     * @return mixed
34
     */
35 15
    public function offsetGet($offset)
36
    {
37 15
        return isset($this->source[$offset]) ? $this->source[$offset] : null;
38
    }
39
40
    /**
41
     * Set offset
42
     *
43
     * @param mixed $offset The offset.
44
     * @param mixed $value  The value to set.
45
     * @return void
46
     */
47 6
    public function offsetSet($offset, $value): void
48
    {
49 6
        if (is_null($offset)) {
50 3
            $this->source[] = $value;
51 3
            return;
52
        }
53 3
        $this->source[$offset] = $value;
54 3
    }
55
56
    /**
57
     * Unset offset
58
     *
59
     * @param mixed $offset The offset.
60
     * @return void
61
     */
62 3
    public function offsetUnset($offset)
63
    {
64 3
        unset($this->source[$offset]);
65 3
    }
66
67
    /**
68
     * Push item to end of collection
69
     *
70
     * @param mixed $item Item to push.
71
     * @return Collection
72
     */
73 3
    public function push($item)
74
    {
75 3
        array_push($this->source, $item);
76 3
        return new static($this->source);
77
    }
78
79
    /**
80
     * Pop item from end of collection
81
     *
82
     * @return mixed
83
     */
84 3
    public function pop()
85
    {
86 3
        return array_pop($this->source);
87
    }
88
89
    /**
90
     * Push item to start of collection
91
     *
92
     * @param mixed $item Item to push.
93
     * @return Collection
94
     */
95 3
    public function unshift($item)
96
    {
97 3
        array_unshift($this->source, $item);
98 3
        return new static($this->source);
99
    }
100
101
    /**
102
     * Pop item from start of collection
103
     *
104
     * @return mixed
105
     */
106 3
    public function shift()
107
    {
108 3
        return array_shift($this->source);
109
    }
110
111
    /**
112
     * Sort collection
113
     *
114
     * @param callable|null $callback The callback to use.
115
     * @return Collection
116
     */
117 6
    public function sort(callable $callback = null)
118
    {
119 6
        if ($callback) {
120 3
            usort($this->source, $callback);
121
        } else {
122 3
            sort($this->source);
123
        }
124 6
        return new static($this->source);
125
    }
126
127
    /**
128
     * Reverse collection
129
     *
130
     * @return Collection
131
     */
132 3
    public function reverse()
133
    {
134 3
        return new static(array_reverse($this->source));
135
    }
136
137
    /**
138
     * Return keys
139
     *
140
     * @return Collection
141
     */
142 3
    public function keys()
143
    {
144 3
        return new static(array_keys($this->source));
145
    }
146
147
    /**
148
     * Return values
149
     *
150
     * @return Collection
151
     */
152 3
    public function values(): Collection
153
    {
154 3
        return new static(array_values($this->source));
155
    }
156
157
    /**
158
     * Return chunked collection
159
     *
160
     * @param integer $size Chunk size.
161
     * @return Collection
162
     */
163 3
    public function chunk(int $size): Collection
164
    {
165 3
        return new static(array_chunk($this->source, $size));
166
    }
167
168
    /**
169
     * Merge another array into the collection
170
     *
171
     * @param mixed $merge Array to merge.
172
     * @return Collection
173
     */
174 3
    public function merge($merge): Collection
175
    {
176 3
        return new static(array_merge($this->source, $merge));
177
    }
178
179
    /**
180
     * Group by a given key
181
     *
182
     * @param string $key Key to group by.
183
     * @return Collection
184
     */
185 3
    public function groupBy(string $key): Collection
186
    {
187 3
        $items = [];
188 3
        foreach ($this->source as $item) {
189 3
            $items[$item[$key]][] = $item;
190
        }
191 3
        return new static($items);
192
    }
193
194
    /**
195
     * Flatten items
196
     *
197
     * @return Collection
198
     */
199 3
    public function flatten(): Collection
200
    {
201 3
        $return = [];
202
        array_walk_recursive($this->source, function ($a) use (&$return) {
203 3
            $return[] = $a;
204 3
        });
205 3
        return new static($return);
206
    }
207
208
    /**
209
     * Paginate items
210
     *
211
     * @return Collection
212
     */
213 3
    public function paginate(int $perPage, int $page): Collection
214
    {
215 3
        $offset = ($page - 1) * $perPage;
216 3
        return new static(array_slice($this->source, $offset, $perPage));
217
    }
218
}
219