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
|
|
|
* Push item to end of collection |
20
|
|
|
* |
21
|
|
|
* @param mixed $item Item to push. |
22
|
|
|
* @return Collection |
23
|
|
|
*/ |
24
|
3 |
|
public function push($item) |
25
|
|
|
{ |
26
|
3 |
|
array_push($this->source, $item); |
27
|
3 |
|
return new static($this->source); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Pop item from end of collection |
32
|
|
|
* |
33
|
|
|
* @return mixed |
34
|
|
|
*/ |
35
|
3 |
|
public function pop() |
36
|
|
|
{ |
37
|
3 |
|
return array_pop($this->source); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Push item to start of collection |
42
|
|
|
* |
43
|
|
|
* @param mixed $item Item to push. |
44
|
|
|
* @return Collection |
45
|
|
|
*/ |
46
|
3 |
|
public function unshift($item) |
47
|
|
|
{ |
48
|
3 |
|
array_unshift($this->source, $item); |
49
|
3 |
|
return new static($this->source); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Pop item from start of collection |
54
|
|
|
* |
55
|
|
|
* @return mixed |
56
|
|
|
*/ |
57
|
3 |
|
public function shift() |
58
|
|
|
{ |
59
|
3 |
|
return array_shift($this->source); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Sort collection |
64
|
|
|
* |
65
|
|
|
* @param callable|null $callback The callback to use. |
66
|
|
|
* @return Collection |
67
|
|
|
*/ |
68
|
6 |
|
public function sort(callable $callback = null) |
69
|
|
|
{ |
70
|
6 |
|
if ($callback) { |
71
|
3 |
|
usort($this->source, $callback); |
72
|
|
|
} else { |
73
|
3 |
|
sort($this->source); |
74
|
|
|
} |
75
|
6 |
|
return new static($this->source); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Reverse collection |
80
|
|
|
* |
81
|
|
|
* @return Collection |
82
|
|
|
*/ |
83
|
3 |
|
public function reverse() |
84
|
|
|
{ |
85
|
3 |
|
return new static(array_reverse($this->source)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Return keys |
90
|
|
|
* |
91
|
|
|
* @return Collection |
92
|
|
|
*/ |
93
|
3 |
|
public function keys() |
94
|
|
|
{ |
95
|
3 |
|
return new static(array_keys($this->source)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Return values |
100
|
|
|
* |
101
|
|
|
* @return Collection |
102
|
|
|
*/ |
103
|
3 |
|
public function values(): Collection |
104
|
|
|
{ |
105
|
3 |
|
return new static(array_values($this->source)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Return chunked collection |
110
|
|
|
* |
111
|
|
|
* @param integer $size Chunk size. |
112
|
|
|
* @return Collection |
113
|
|
|
*/ |
114
|
3 |
|
public function chunk(int $size): Collection |
115
|
|
|
{ |
116
|
3 |
|
return new static(array_chunk($this->source, $size)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Merge another array into the collection |
121
|
|
|
* |
122
|
|
|
* @param mixed $merge Array to merge. |
123
|
|
|
* @return Collection |
124
|
|
|
*/ |
125
|
3 |
|
public function merge($merge): Collection |
126
|
|
|
{ |
127
|
3 |
|
return new static(array_merge($this->source, $merge)); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Group by a given key |
132
|
|
|
* |
133
|
|
|
* @param string $key Key to group by. |
134
|
|
|
* @return Collection |
135
|
|
|
*/ |
136
|
3 |
|
public function groupBy(string $key): Collection |
137
|
|
|
{ |
138
|
3 |
|
$items = []; |
139
|
3 |
|
foreach ($this->source as $item) { |
140
|
3 |
|
$items[$item[$key]][] = $item; |
141
|
|
|
} |
142
|
3 |
|
return new static($items); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Flatten items |
147
|
|
|
* |
148
|
|
|
* @return Collection |
149
|
|
|
*/ |
150
|
3 |
|
public function flatten(): Collection |
151
|
|
|
{ |
152
|
3 |
|
$return = []; |
153
|
|
|
array_walk_recursive($this->source, function ($a) use (&$return) { |
154
|
3 |
|
$return[] = $a; |
155
|
3 |
|
}); |
156
|
3 |
|
return new static($return); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Paginate items |
161
|
|
|
* |
162
|
|
|
* @return Collection |
163
|
|
|
*/ |
164
|
3 |
|
public function paginate(int $perPage, int $page): Collection |
165
|
|
|
{ |
166
|
3 |
|
$offset = ($page - 1) * $perPage; |
167
|
3 |
|
return new static(array_slice($this->source, $offset, $perPage)); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|