|
2 | 2 |
|
3 | 3 | namespace Ahc\Underscore; |
4 | 4 |
|
5 | | -class Underscore implements \ArrayAccess, \Countable, \IteratorAggregate, \JsonSerializable |
| 5 | +final class Underscore extends UnderscoreCollection |
6 | 6 | { |
7 | | - const VERSION = '0.0.1'; |
8 | | - |
9 | | - /** @var array The array manipulated by this object */ |
10 | | - protected $data; |
11 | | - |
12 | | - /** |
13 | | - * Constructor. |
14 | | - * |
15 | | - * @param array|mixed $data. |
16 | | - */ |
17 | | - public function __construct($data = []) |
18 | | - { |
19 | | - $this->data = \is_array($data) ? $data : Helper::asArray($data); |
20 | | - } |
21 | | - |
22 | | - /** |
23 | | - * Get the underlying array data. |
24 | | - * |
25 | | - * @param string|int|null $index |
26 | | - * |
27 | | - * @return mixed |
28 | | - */ |
29 | | - public function get($index = null) |
30 | | - { |
31 | | - if (null === $index) { |
32 | | - return $this->data; |
33 | | - } |
34 | | - |
35 | | - return $this->data[$index]; |
36 | | - } |
37 | | - |
38 | | - public function each(callable $fn) |
39 | | - { |
40 | | - foreach ($this->data as $index => $value) { |
41 | | - $fn($value, $index); |
42 | | - } |
43 | | - |
44 | | - return $this; |
45 | | - } |
46 | | - |
47 | | - public function map(callable $fn) |
48 | | - { |
49 | | - $data = []; |
50 | | - |
51 | | - foreach ($this->data as $index => $value) { |
52 | | - $data[$index] = $fn($value, $index); |
53 | | - } |
54 | | - |
55 | | - return new static($data); |
56 | | - } |
57 | | - |
58 | | - public function collect(callable $fn) |
59 | | - { |
60 | | - return $this->map($fn); |
61 | | - } |
62 | | - |
63 | | - public function reduce(callable $fn, $memo) |
64 | | - { |
65 | | - return \array_reduce($this->data, $fn, $memo); |
66 | | - } |
67 | | - |
68 | | - public function foldl(callable $fn, $memo) |
69 | | - { |
70 | | - return $this->reduce($fn, $memo); |
71 | | - } |
72 | | - |
73 | | - public function inject(callable $fn, $memo) |
74 | | - { |
75 | | - return $this->reduce($fn, $memo); |
76 | | - } |
77 | | - |
78 | | - public function reduceRight(callable $fn, $memo) |
79 | | - { |
80 | | - return \array_reduce(\array_reverse($this->data), $fn, $memo); |
81 | | - } |
82 | | - |
83 | | - public function foldr(callable $fn, $memo) |
84 | | - { |
85 | | - return $this->reduceRight($fn, $memo); |
86 | | - } |
87 | | - |
88 | | - public function find(callable $fn) |
89 | | - { |
90 | | - foreach ($this->data as $index => $value) { |
91 | | - if ($fn($value, $index)) { |
92 | | - return $value; |
93 | | - } |
94 | | - } |
95 | | - } |
96 | | - |
97 | | - public function detect(callable $fn) |
98 | | - { |
99 | | - return $this->find($fn); |
100 | | - } |
101 | | - |
102 | | - public function filter(callable $fn) |
103 | | - { |
104 | | - $data = \array_filter($this->data, $fn, \ARRAY_FILTER_USE_BOTH); |
105 | | - |
106 | | - return new static($data); |
107 | | - } |
108 | | - |
109 | | - public function select(callable $fn) |
110 | | - { |
111 | | - return $this->filter($fn); |
112 | | - } |
113 | | - |
114 | | - public function reject(callable $fn) |
115 | | - { |
116 | | - $data = \array_filter($this->data, $this->negate($fn), \ARRAY_FILTER_USE_BOTH); |
117 | | - |
118 | | - return new static($data); |
119 | | - } |
120 | | - |
121 | | - protected function negate(callable $fn) |
122 | | - { |
123 | | - return function () use ($fn) { |
124 | | - return !\call_user_func_array($fn, \func_get_args()); |
125 | | - }; |
126 | | - } |
127 | | - |
128 | | - public function every(callable $fn) |
129 | | - { |
130 | | - return $this->match($fn, true); |
131 | | - } |
132 | | - |
133 | | - public function all(callable $fn) |
134 | | - { |
135 | | - return $this->every($fn); |
136 | | - } |
137 | | - |
138 | | - public function some(callable $fn) |
139 | | - { |
140 | | - return $this->match($fn, false); |
141 | | - } |
142 | | - |
143 | | - public function any(callable $fn) |
144 | | - { |
145 | | - return $this->some($fn); |
146 | | - } |
147 | | - |
148 | | - protected function match(callable $fn, $all = true) |
149 | | - { |
150 | | - foreach ($this->data as $index => $value) { |
151 | | - if ($all ^ $fn($value, $index)) { |
152 | | - return !$all; |
153 | | - } |
154 | | - } |
155 | | - |
156 | | - return $all; |
157 | | - } |
158 | | - |
159 | | - public function contains($item) |
160 | | - { |
161 | | - return \in_array($item, $this->data); |
162 | | - } |
163 | | - |
164 | | - public function includes($item) |
165 | | - { |
166 | | - return $this->contains($item); |
167 | | - } |
168 | | - |
169 | | - public function invoke(callable $fn) |
170 | | - { |
171 | | - return \call_user_func_array($fn, $this->data); |
172 | | - } |
173 | | - |
174 | | - public function pluck($columnKey, $indexKey = null) |
175 | | - { |
176 | | - $data = \array_column($this->data, $columnKey, $indexKey); |
177 | | - |
178 | | - return new static($data); |
179 | | - } |
180 | | - |
181 | | - public function where(array $props) |
182 | | - { |
183 | | - return $this->filter($this->matcher($props)); |
184 | | - } |
185 | | - |
186 | | - public function findWhere(array $props) |
187 | | - { |
188 | | - return $this->find($this->matcher($props)); |
189 | | - } |
190 | | - |
191 | | - protected function matcher(array $props) |
192 | | - { |
193 | | - return function ($value, $index) use ($props) { |
194 | | - foreach ($props as $prop => $criteria) { |
195 | | - if (\array_column([$value], $prop) != [$criteria]) { |
196 | | - return false; |
197 | | - } |
198 | | - } |
199 | | - |
200 | | - return true; |
201 | | - }; |
202 | | - } |
203 | | - |
204 | | - /** |
205 | | - * {@inheritdoc} |
206 | | - */ |
207 | | - public function offsetExists($index) |
208 | | - { |
209 | | - return \array_key_exists($index, $this->data); |
210 | | - } |
211 | | - |
212 | | - /** |
213 | | - * {@inheritdoc} |
214 | | - */ |
215 | | - public function offsetGet($index) |
216 | | - { |
217 | | - return $this->data[$index]; |
218 | | - } |
219 | | - |
220 | | - /** |
221 | | - * {@inheritdoc} |
222 | | - */ |
223 | | - public function offsetSet($index, $value) |
224 | | - { |
225 | | - $this->data[$index] = $value; |
226 | | - } |
227 | | - |
228 | | - /** |
229 | | - * {@inheritdoc} |
230 | | - */ |
231 | | - public function offsetUnset($index) |
232 | | - { |
233 | | - unset($this->data[$index]); |
234 | | - } |
235 | | - |
236 | | - /** |
237 | | - * {@inheritdoc} |
238 | | - */ |
239 | | - public function count() |
240 | | - { |
241 | | - return \count($this->data); |
242 | | - } |
243 | | - |
244 | | - /** |
245 | | - * {@inheritdoc} |
246 | | - */ |
247 | | - public function getIterator() |
248 | | - { |
249 | | - return new \ArrayIterator($this->data); |
250 | | - } |
251 | | - |
252 | | - /** |
253 | | - * {@inheritdoc} |
254 | | - */ |
255 | | - public function jsonSerialize() |
256 | | - { |
257 | | - return $this->data; |
258 | | - } |
259 | | - |
260 | | - /** |
261 | | - * {@inheritdoc} |
262 | | - */ |
263 | | - public function __toString() |
264 | | - { |
265 | | - return \json_encode($this->data); |
266 | | - } |
267 | | - |
268 | | - public static function _($data) |
269 | | - { |
270 | | - return new static($data); |
271 | | - } |
272 | 7 | } |
273 | | - |
274 | | -\class_alias('Ahc\Underscore\Underscore', 'Ahc\Underscore'); |
0 commit comments