Skip to content

Commit 4f8bcfc

Browse files
authored
Merge pull request #2 from adhocore/develop
Refactor core, add more collection and array functionalities
2 parents 5518eee + ae3685c commit 4f8bcfc

11 files changed

+813
-354
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"psr-4": {
1515
"Ahc\\Underscore\\": "src/"
1616
},
17-
"files": []
17+
"files": ["src/functions.php"]
1818
},
1919
"autoload-dev": {
2020
"psr-4": {

src/Helper.php

-40
This file was deleted.

src/Underscore.php

+1-268
Original file line numberDiff line numberDiff line change
@@ -2,273 +2,6 @@
22

33
namespace Ahc\Underscore;
44

5-
class Underscore implements \ArrayAccess, \Countable, \IteratorAggregate, \JsonSerializable
5+
final class Underscore extends UnderscoreCollection
66
{
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-
}
2727
}
273-
274-
\class_alias('Ahc\Underscore\Underscore', 'Ahc\Underscore');

src/UnderscoreArray.php

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Ahc\Underscore;
4+
5+
class UnderscoreArray extends UnderscoreBase
6+
{
7+
public function first()
8+
{
9+
return \reset($this->data);
10+
}
11+
12+
public function last()
13+
{
14+
return \end($this->data);
15+
}
16+
17+
public function compact()
18+
{
19+
return new static(\array_filter($this->data));
20+
}
21+
22+
public function flatten()
23+
{
24+
return new static($this->flat($this->data));
25+
}
26+
27+
public function unique($fn = null)
28+
{
29+
if (null === $fn) {
30+
return new static(\array_unique($this->data));
31+
}
32+
33+
$ids = $data = [];
34+
$fn = $this->valueFn($fn);
35+
36+
foreach ($this->data as $index => $value) {
37+
if (!isset($ids[$id = $fn($value, $index)])) {
38+
$ids[$id] = true;
39+
40+
$data[$index] = $value;
41+
}
42+
}
43+
44+
return new static($data);
45+
}
46+
47+
public function uniq($fn)
48+
{
49+
return $this->unique($fn);
50+
}
51+
}

0 commit comments

Comments
 (0)