Skip to content

Commit 5750c38

Browse files
authored
Merge pull request #6 from adhocore/develop
Develop
2 parents ba7b1fd + 0815174 commit 5750c38

12 files changed

+384
-346
lines changed

src/Arrayizes.php

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Ahc\Underscore;
4+
5+
trait Arrayizes
6+
{
7+
/**
8+
* Get data as array.
9+
*
10+
* @param mixed $data Arbitrary data.
11+
* @param bool $cast Force casting to array!
12+
*
13+
* @return array
14+
*/
15+
public function asArray($data, $cast = true)
16+
{
17+
if (\is_array($data)) {
18+
return $data;
19+
}
20+
21+
if ($data instanceof static) {
22+
return $data->get();
23+
}
24+
25+
// @codeCoverageIgnoreStart
26+
if ($data instanceof \Traversable) {
27+
return \iterator_to_array($data);
28+
}
29+
// @codeCoverageIgnoreEnd
30+
31+
if ($data instanceof \JsonSerializable) {
32+
return $data->jsonSerialize();
33+
}
34+
35+
if (\method_exists($data, 'toArray')) {
36+
return $data->toArray();
37+
}
38+
39+
return $cast ? (array) $data : $data;
40+
}
41+
42+
/**
43+
* Convert the data items to array.
44+
*
45+
* @return array
46+
*/
47+
public function toArray()
48+
{
49+
return \array_map(function ($value) {
50+
if (\is_scalar($value)) {
51+
return $value;
52+
}
53+
54+
return $this->asArray($value, false);
55+
}, $this->getData());
56+
}
57+
}

src/Underscore.php

+22
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,28 @@
44

55
final class Underscore extends UnderscoreFunction
66
{
7+
/**
8+
* Constructor.
9+
*
10+
* @param array|mixed $data
11+
*/
12+
public function __construct($data = [])
13+
{
14+
parent::__construct($data);
15+
}
16+
17+
/**
18+
* A static shortcut to constructor.
19+
*
20+
* @param array|mixed $data Array or array like or array convertible.
21+
*
22+
* @return self
23+
*/
24+
public static function _($data = null)
25+
{
26+
return new static($data);
27+
}
28+
729
/**
830
* Generates a function that always returns a constant value.
931
*

src/UnderscoreAliases.php

+196
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
<?php
2+
3+
namespace Ahc\Underscore;
4+
5+
trait UnderscoreAliases
6+
{
7+
/**
8+
* Alias of first().
9+
*
10+
* @param int $n
11+
*
12+
* @return array|mixed With n = 1 (default), it gives one item, which may not be array.
13+
*/
14+
public function head($n = 1)
15+
{
16+
return $this->first($n);
17+
}
18+
19+
/**
20+
* Alias of first().
21+
*
22+
* @param int $n
23+
*
24+
* @return array|mixed With n = 1 (default), it gives one item, which may not be array.
25+
*/
26+
public function take($n = 1)
27+
{
28+
return $this->first($n);
29+
}
30+
31+
/**
32+
* Alias of last().
33+
*
34+
* @param int $n
35+
*
36+
* @return array|mixed With n = 1 (default), it gives one item, which may not be array.
37+
*/
38+
public function tail($n = 1)
39+
{
40+
return $this->last($n);
41+
}
42+
43+
/**
44+
* Alias of last().
45+
*
46+
* @param int $n
47+
*
48+
* @return array|mixed With n = 1 (default), it gives one item, which may not be array.
49+
*/
50+
public function drop($n = 1)
51+
{
52+
return $this->last($n);
53+
}
54+
55+
/**
56+
* Alias of unique().
57+
*
58+
* @param callable|string $fn The callback. String is resolved to value of that index.
59+
*
60+
* @return self
61+
*/
62+
public function uniq($fn = null)
63+
{
64+
return $this->unique($fn);
65+
}
66+
67+
/**
68+
* Alias of difference().
69+
*
70+
* @param array|mixed $data Array or array like or array convertible.
71+
*
72+
* @return self
73+
*/
74+
public function without($data)
75+
{
76+
return $this->difference($data);
77+
}
78+
79+
/**
80+
* Alias of map().
81+
*
82+
* @param callable $fn The callback.
83+
*
84+
* @return self
85+
*/
86+
public function collect(callable $fn)
87+
{
88+
return $this->map($fn);
89+
}
90+
91+
/**
92+
* Alias of reduce().
93+
*
94+
* @param callable $fn The callback.
95+
* @param mixed $memo The initial value carried over to each iteration and returned finally.
96+
*
97+
* @return mixed
98+
*/
99+
public function foldl(callable $fn, $memo)
100+
{
101+
return $this->reduce($fn, $memo);
102+
}
103+
104+
/**
105+
* Alias of reduce().
106+
*
107+
* @param callable $fn The callback.
108+
* @param mixed $memo The initial value carried over to each iteration and returned finally.
109+
*
110+
* @return mixed
111+
*/
112+
public function inject(callable $fn, $memo)
113+
{
114+
return $this->reduce($fn, $memo);
115+
}
116+
117+
/**
118+
* Alias of reduceRight().
119+
*
120+
* @param callable $fn The callback.
121+
* @param mixed $memo The initial value carried over to each iteration and returned finally.
122+
*
123+
* @return mixed
124+
*/
125+
public function foldr(callable $fn, $memo)
126+
{
127+
return $this->reduceRight($fn, $memo);
128+
}
129+
130+
/**
131+
* Alias of find().
132+
*
133+
* @param callable $fn The truth test callback.
134+
* @param bool $useValue Whether to return value or the index on match.
135+
*
136+
* @return mixed|null
137+
*/
138+
public function detect(callable $fn)
139+
{
140+
return $this->find($fn);
141+
}
142+
143+
/**
144+
* Alias of filter().
145+
*
146+
* @param callable|string|null $fn The truth test callback.
147+
*
148+
* @return self
149+
*/
150+
public function select(callable $fn = null)
151+
{
152+
return $this->filter($fn);
153+
}
154+
155+
/**
156+
* Alias of every().
157+
*
158+
* @param callable $fn The truth test callback.
159+
*
160+
* @return bool
161+
*/
162+
public function all(callable $fn)
163+
{
164+
return $this->every($fn);
165+
}
166+
167+
/**
168+
* Alias of some().
169+
*
170+
* @param callable $fn The truth test callback.
171+
*
172+
* @return bool
173+
*/
174+
public function any(callable $fn)
175+
{
176+
return $this->some($fn);
177+
}
178+
179+
/**
180+
* Alias of contains().
181+
*/
182+
public function includes($item)
183+
{
184+
return $this->contains($item);
185+
}
186+
187+
/**
188+
* Alias of count().
189+
*
190+
* @return int
191+
*/
192+
public function size()
193+
{
194+
return $this->count();
195+
}
196+
}

0 commit comments

Comments
 (0)