Skip to content

Commit c4540b1

Browse files
committed
fix+doc: fix array methods as per test, add docs
1 parent 85a2002 commit c4540b1

File tree

1 file changed

+145
-17
lines changed

1 file changed

+145
-17
lines changed

src/UnderscoreArray.php

Lines changed: 145 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,72 @@
44

55
class UnderscoreArray extends UnderscoreCollection
66
{
7+
/**
8+
* Get the first n items.
9+
*
10+
* @param integer $n
11+
*
12+
* @return array
13+
*/
714
public function first($n = 1)
815
{
916
return $this->slice($n, true);
1017
}
1118

19+
/**
20+
* Alias of first().
21+
*/
1222
public function head($n = 1)
1323
{
1424
return $this->first($n);
1525
}
1626

27+
/**
28+
* Alias of first().
29+
*/
1730
public function take($n = 1)
1831
{
1932
return $this->first($n);
2033
}
2134

35+
/**
36+
* Get the last n items.
37+
*
38+
* @param integer $n
39+
*
40+
* @return array
41+
*/
2242
public function last($n = 1)
2343
{
2444
return $this->slice($n, false);
2545
}
2646

47+
/**
48+
* Alias of last().
49+
*/
2750
public function tail($n = 1)
2851
{
2952
return $this->last($n);
3053
}
3154

55+
/**
56+
* Alias of last().
57+
*/
3258
public function drop($n = 1)
3359
{
3460
return $this->last($n);
3561
}
3662

63+
/**
64+
* Extracts n items from first or last.
65+
*
66+
* @internal
67+
*
68+
* @param integer $n
69+
* @param boolean $isFirst From first if true, else last.
70+
*
71+
* @return array
72+
*/
3773
protected function slice($n, $isFirst = true)
3874
{
3975
if ($n < 2) {
@@ -47,16 +83,33 @@ protected function slice($n, $isFirst = true)
4783
return \array_slice($this->data, $isFirst ? 0 : $c - $n, $isFirst ? $n : null, true);
4884
}
4985

86+
/**
87+
* Get only the truthy items.
88+
*
89+
* @return self
90+
*/
5091
public function compact()
5192
{
52-
return new static(\array_filter($this->data));
93+
return $this->filter(null);
5394
}
5495

96+
/**
97+
* Gets the flattened version of multidimensional items.
98+
*
99+
* @return self
100+
*/
55101
public function flatten()
56102
{
57103
return new static($this->flat($this->data));
58104
}
59105

106+
/**
107+
* Gets the unique items using the id resulted from callback.
108+
*
109+
* @param callback|string $fn The callback. String is resolved to value of that index.
110+
*
111+
* @return self
112+
*/
60113
public function unique($fn = null)
61114
{
62115
if (null === $fn) {
@@ -71,28 +124,57 @@ public function unique($fn = null)
71124
});
72125
}
73126

127+
/**
128+
* Alias of unique().
129+
*/
74130
public function uniq($fn = null)
75131
{
76132
return $this->unique($fn);
77133
}
78134

135+
/**
136+
* Get the items whose value is not in given data.
137+
*
138+
* @param array|mixed $data Array or array like or array convertible.
139+
*
140+
* @return self
141+
*/
79142
public function difference($data)
80143
{
81-
return new static(\array_diff($this->data, $this->asArray($data)));
144+
$data = $this->asArray($data);
145+
146+
return $this->filter(function ($value) use ($data) {
147+
return !\in_array($value, $data);
148+
});
82149
}
83150

151+
/**
152+
* Alias of without().
153+
*/
84154
public function without($data)
85155
{
86156
return $this->difference($data);
87157
}
88158

159+
/**
160+
* Get the union/merger of items with given data.
161+
*
162+
* @param array|mixed $data Array or array like or array convertible.
163+
*
164+
* @return self
165+
*/
89166
public function union($data)
90167
{
91-
return new static(\array_unique(
92-
\array_merge($this->data, $this->asArray($data))
93-
));
168+
return new static(\array_merge($this->data, $this->asArray($data)));
94169
}
95170

171+
/**
172+
* Gets the items whose value is common with given data.
173+
*
174+
* @param array|mixed $data Array or array like or array convertible.
175+
*
176+
* @return self
177+
*/
96178
public function intersection($data)
97179
{
98180
$data = $this->asArray($data);
@@ -102,6 +184,13 @@ public function intersection($data)
102184
});
103185
}
104186

187+
/**
188+
* Group the values from data and items having same indexes together.
189+
*
190+
* @param array|mixed $data Array or array like or array convertible.
191+
*
192+
* @return self
193+
*/
105194
public function zip($data)
106195
{
107196
$data = $this->asArray($data);
@@ -111,38 +200,77 @@ public function zip($data)
111200
});
112201
}
113202

114-
public function unzip()
115-
{
116-
//
117-
}
118-
203+
/**
204+
* Hydrate the items into given class or stdClass.
205+
*
206+
* @param string $className FQCN of the class whose constructor accepts two parameters: value and index.
207+
*
208+
* @return self
209+
*/
119210
public function object($className = null)
120211
{
121-
return $this->map(function ($value, $index) {
212+
return $this->map(function ($value, $index) use ($className) {
122213
return $className ? new $className($value, $index) : (object) \compact('value', 'index');
123214
});
124215
}
125216

126-
public function firstIndex($fn)
217+
/**
218+
* Find the first index that passes given truth test.
219+
*
220+
* @param callable $fn The truth test callback.
221+
*
222+
* @return mixed|null
223+
*/
224+
public function firstIndex($fn = null)
127225
{
128-
return $this->find($fn, false);
226+
return $this->find($this->valueFn($fn), false);
129227
}
130228

131-
public function lastIndex($fn)
229+
/**
230+
* Find the larst index that passes given truth test.
231+
*
232+
* @param callable $fn The truth test callback.
233+
*
234+
* @return mixed|null
235+
*/
236+
public function lastIndex($fn = null)
132237
{
133-
return (new static(\array_reverse($this->data, true)))->find($fn, false);
238+
return (new static(\array_reverse($this->data, true)))->find($this->valueFn($fn), false);
134239
}
135240

241+
/**
242+
* Find the first index of given value if available null otherwise.
243+
*
244+
* @param callable $fn The truth test callback.
245+
*
246+
* @return string|int|null
247+
*/
136248
public function indexOf($value)
137249
{
138-
return \array_search($value, $this->data);
250+
return (false === $index = \array_search($value, $this->data)) ? null : $index;
139251
}
140252

253+
/**
254+
* Find the last index of given value if available null otherwise.
255+
*
256+
* @param callable $fn The truth test callback.
257+
*
258+
* @return string|int|null
259+
*/
141260
public function lastIndexOf($value)
142261
{
143-
return \array_search($value, \array_reverse($this->data, true));
262+
return (false === $index = \array_search($value, \array_reverse($this->data, true))) ? null : $index;
144263
}
145264

265+
/**
266+
* Creates a new range from start to stop with given step.
267+
*
268+
* @param int $start
269+
* @param int $stop
270+
* @param int $step
271+
*
272+
* @return self
273+
*/
146274
public function range($start, $stop, $step = 1)
147275
{
148276
return new static(\range($start, $stop, $step));

0 commit comments

Comments
 (0)