Skip to content

Commit 4af6bb2

Browse files
committed
refactor: move aliases and array conversion, protect __construct()
1 parent 6b10d81 commit 4af6bb2

File tree

4 files changed

+37
-255
lines changed

4 files changed

+37
-255
lines changed

src/Underscore.php

Lines changed: 22 additions & 0 deletions
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/UnderscoreArray.php

Lines changed: 7 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -9,73 +9,25 @@ class UnderscoreArray extends UnderscoreCollection
99
*
1010
* @param int $n
1111
*
12-
* @return array
12+
* @return array|mixed With n = 1 (default), it gives one item, which may not be array.
1313
*/
1414
public function first($n = 1)
1515
{
1616
return $this->slice($n, true);
1717
}
1818

19-
/**
20-
* Alias of first().
21-
*
22-
* @param int $n
23-
*
24-
* @return array
25-
*/
26-
public function head($n = 1)
27-
{
28-
return $this->first($n);
29-
}
30-
31-
/**
32-
* Alias of first().
33-
*
34-
* @param int $n
35-
*
36-
* @return array
37-
*/
38-
public function take($n = 1)
39-
{
40-
return $this->first($n);
41-
}
42-
4319
/**
4420
* Get the last n items.
4521
*
4622
* @param int $n
4723
*
48-
* @return array
24+
* @return array|mixed With n = 1 (default), it gives one item, which may not be array.
4925
*/
5026
public function last($n = 1)
5127
{
5228
return $this->slice($n, false);
5329
}
5430

55-
/**
56-
* Alias of last().
57-
*
58-
* @param int $n
59-
*
60-
* @return array
61-
*/
62-
public function tail($n = 1)
63-
{
64-
return $this->last($n);
65-
}
66-
67-
/**
68-
* Alias of last().
69-
*
70-
* @param int $n
71-
*
72-
* @return array
73-
*/
74-
public function drop($n = 1)
75-
{
76-
return $this->last($n);
77-
}
78-
7931
/**
8032
* Extracts n items from first or last.
8133
*
@@ -84,7 +36,7 @@ public function drop($n = 1)
8436
* @param int $n
8537
* @param bool $isFirst From first if true, else last.
8638
*
87-
* @return array
39+
* @return array|mixed With n = 1 (default), it gives one item, which may not be array.
8840
*/
8941
protected function slice($n, $isFirst = true)
9042
{
@@ -140,18 +92,6 @@ public function unique($fn = null)
14092
});
14193
}
14294

143-
/**
144-
* Alias of unique().
145-
*
146-
* @param callable|string $fn The callback. String is resolved to value of that index.
147-
*
148-
* @return self
149-
*/
150-
public function uniq($fn = null)
151-
{
152-
return $this->unique($fn);
153-
}
154-
15595
/**
15696
* Get the items whose value is not in given data.
15797
*
@@ -168,18 +108,6 @@ public function difference($data)
168108
});
169109
}
170110

171-
/**
172-
* Alias of difference().
173-
*
174-
* @param array|mixed $data Array or array like or array convertible.
175-
*
176-
* @return self
177-
*/
178-
public function without($data)
179-
{
180-
return $this->difference($data);
181-
}
182-
183111
/**
184112
* Get the union/merger of items with given data.
185113
*
@@ -265,7 +193,7 @@ public function findLastIndex($fn = null)
265193
/**
266194
* Find the first index of given value if available null otherwise.
267195
*
268-
* @param callable $fn The truth test callback.
196+
* @param mixed $value The lookup value.
269197
*
270198
* @return string|int|null
271199
*/
@@ -277,7 +205,7 @@ public function indexOf($value)
277205
/**
278206
* Find the last index of given value if available null otherwise.
279207
*
280-
* @param callable $fn The truth test callback.
208+
* @param mixed $value The lookup value.
281209
*
282210
* @return string|int|null
283211
*/
@@ -291,8 +219,8 @@ public function lastIndexOf($value)
291219
*
292220
* Note that the initial stack must be sorted already.
293221
*
294-
* @param $object The new object which needs to be adjusted in stack.
295-
* @param callable|string $fn The comparator callback.
222+
* @param mixed $object The new object which needs to be adjusted in stack.
223+
* @param callable|string $fn The comparator callback.
296224
*
297225
* @return string|int|null
298226
*/

src/UnderscoreBase.php

Lines changed: 8 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
class UnderscoreBase implements \ArrayAccess, \Countable, \IteratorAggregate, \JsonSerializable
66
{
7+
use Arrayizes;
8+
use UnderscoreAliases;
9+
710
const VERSION = '0.0.2';
811

912
/** @var array The array manipulated by this Underscore instance */
@@ -13,11 +16,11 @@ class UnderscoreBase implements \ArrayAccess, \Countable, \IteratorAggregate, \J
1316
protected static $mixins = [];
1417

1518
/**
16-
* Constructor.
19+
* Constructor. Only allow `Ahc\Underscore\Underscore` to be instantiated in userland.
1720
*
1821
* @param array|mixed $data Array or array like or array convertible.
1922
*/
20-
public function __construct($data = [])
23+
protected function __construct($data = [])
2124
{
2225
$this->data = \is_array($data) ? $data : $this->asArray($data);
2326
}
@@ -39,54 +42,13 @@ public function get($index = null)
3942
}
4043

4144
/**
42-
* Get data as array.
43-
*
44-
* @param mixed $data Arbitrary data.
45-
* @param bool $cast Force casting to array!
46-
*
47-
* @return array
48-
*/
49-
public function asArray($data, $cast = true)
50-
{
51-
if (\is_array($data)) {
52-
return $data;
53-
}
54-
55-
if ($data instanceof static) {
56-
return $data->get();
57-
}
58-
59-
// @codeCoverageIgnoreStart
60-
if ($data instanceof \Traversable) {
61-
return \iterator_to_array($data);
62-
}
63-
// @codeCoverageIgnoreEnd
64-
65-
if ($data instanceof \JsonSerializable) {
66-
return $data->jsonSerialize();
67-
}
68-
69-
if (\method_exists($data, 'toArray')) {
70-
return $data->toArray();
71-
}
72-
73-
return $cast ? (array) $data : $data;
74-
}
75-
76-
/**
77-
* Convert the data items to array.
45+
* Get data.
7846
*
7947
* @return array
8048
*/
81-
public function toArray()
49+
public function getData()
8250
{
83-
return \array_map(function ($value) {
84-
if (\is_scalar($value)) {
85-
return $value;
86-
}
87-
88-
return $this->asArray($value, false);
89-
}, $this->data);
51+
return $this->data;
9052
}
9153

9254
/**
@@ -206,16 +168,6 @@ public function count()
206168
return \count($this->data);
207169
}
208170

209-
/**
210-
* Alias of count().
211-
*
212-
* @return int
213-
*/
214-
public function size()
215-
{
216-
return $this->count();
217-
}
218-
219171
/**
220172
* Gets the iterator for looping.
221173
*
@@ -398,16 +350,4 @@ public function valueOf()
398350
{
399351
return (string) $this;
400352
}
401-
402-
/**
403-
* A static shortcut to constructor.
404-
*
405-
* @param array|mixed $data Array or array like or array convertible.
406-
*
407-
* @return self
408-
*/
409-
public static function _($data = null)
410-
{
411-
return new static($data);
412-
}
413353
}

0 commit comments

Comments
 (0)