4
4
5
5
class UnderscoreArray extends UnderscoreCollection
6
6
{
7
+ /**
8
+ * Get the first n items.
9
+ *
10
+ * @param integer $n
11
+ *
12
+ * @return array
13
+ */
7
14
public function first ($ n = 1 )
8
15
{
9
16
return $ this ->slice ($ n , true );
10
17
}
11
18
19
+ /**
20
+ * Alias of first().
21
+ */
12
22
public function head ($ n = 1 )
13
23
{
14
24
return $ this ->first ($ n );
15
25
}
16
26
27
+ /**
28
+ * Alias of first().
29
+ */
17
30
public function take ($ n = 1 )
18
31
{
19
32
return $ this ->first ($ n );
20
33
}
21
34
35
+ /**
36
+ * Get the last n items.
37
+ *
38
+ * @param integer $n
39
+ *
40
+ * @return array
41
+ */
22
42
public function last ($ n = 1 )
23
43
{
24
44
return $ this ->slice ($ n , false );
25
45
}
26
46
47
+ /**
48
+ * Alias of last().
49
+ */
27
50
public function tail ($ n = 1 )
28
51
{
29
52
return $ this ->last ($ n );
30
53
}
31
54
55
+ /**
56
+ * Alias of last().
57
+ */
32
58
public function drop ($ n = 1 )
33
59
{
34
60
return $ this ->last ($ n );
35
61
}
36
62
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
+ */
37
73
protected function slice ($ n , $ isFirst = true )
38
74
{
39
75
if ($ n < 2 ) {
@@ -47,16 +83,33 @@ protected function slice($n, $isFirst = true)
47
83
return \array_slice ($ this ->data , $ isFirst ? 0 : $ c - $ n , $ isFirst ? $ n : null , true );
48
84
}
49
85
86
+ /**
87
+ * Get only the truthy items.
88
+ *
89
+ * @return self
90
+ */
50
91
public function compact ()
51
92
{
52
- return new static ( \array_filter ( $ this ->data ) );
93
+ return $ this ->filter ( null );
53
94
}
54
95
96
+ /**
97
+ * Gets the flattened version of multidimensional items.
98
+ *
99
+ * @return self
100
+ */
55
101
public function flatten ()
56
102
{
57
103
return new static ($ this ->flat ($ this ->data ));
58
104
}
59
105
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
+ */
60
113
public function unique ($ fn = null )
61
114
{
62
115
if (null === $ fn ) {
@@ -71,28 +124,57 @@ public function unique($fn = null)
71
124
});
72
125
}
73
126
127
+ /**
128
+ * Alias of unique().
129
+ */
74
130
public function uniq ($ fn = null )
75
131
{
76
132
return $ this ->unique ($ fn );
77
133
}
78
134
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
+ */
79
142
public function difference ($ data )
80
143
{
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
+ });
82
149
}
83
150
151
+ /**
152
+ * Alias of without().
153
+ */
84
154
public function without ($ data )
85
155
{
86
156
return $ this ->difference ($ data );
87
157
}
88
158
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
+ */
89
166
public function union ($ data )
90
167
{
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 )));
94
169
}
95
170
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
+ */
96
178
public function intersection ($ data )
97
179
{
98
180
$ data = $ this ->asArray ($ data );
@@ -102,6 +184,13 @@ public function intersection($data)
102
184
});
103
185
}
104
186
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
+ */
105
194
public function zip ($ data )
106
195
{
107
196
$ data = $ this ->asArray ($ data );
@@ -111,38 +200,77 @@ public function zip($data)
111
200
});
112
201
}
113
202
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
+ */
119
210
public function object ($ className = null )
120
211
{
121
- return $ this ->map (function ($ value , $ index ) {
212
+ return $ this ->map (function ($ value , $ index ) use ( $ className ) {
122
213
return $ className ? new $ className ($ value , $ index ) : (object ) \compact ('value ' , 'index ' );
123
214
});
124
215
}
125
216
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 )
127
225
{
128
- return $ this ->find ($ fn , false );
226
+ return $ this ->find ($ this -> valueFn ( $ fn ) , false );
129
227
}
130
228
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 )
132
237
{
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 );
134
239
}
135
240
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
+ */
136
248
public function indexOf ($ value )
137
249
{
138
- return \array_search ($ value , $ this ->data );
250
+ return ( false === $ index = \array_search ($ value , $ this ->data )) ? null : $ index ;
139
251
}
140
252
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
+ */
141
260
public function lastIndexOf ($ value )
142
261
{
143
- return \array_search ($ value , \array_reverse ($ this ->data , true ));
262
+ return ( false === $ index = \array_search ($ value , \array_reverse ($ this ->data , true ))) ? null : $ index ;
144
263
}
145
264
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
+ */
146
274
public function range ($ start , $ stop , $ step = 1 )
147
275
{
148
276
return new static (\range ($ start , $ stop , $ step ));
0 commit comments