@@ -94,7 +94,7 @@ public function foldr(callable $fn, $memo)
94
94
public function find (callabe $ fn )
95
95
{
96
96
foreach ($ this ->data as $ index => $ value ) {
97
- if ($ fn ($ value , $ key )) {
97
+ if ($ fn ($ value , $ index )) {
98
98
return $ value ;
99
99
}
100
100
}
@@ -190,15 +190,34 @@ public function invoke(callable $fn)
190
190
191
191
public function pluck ($ columnKey , $ indexKey = null )
192
192
{
193
- if (\function_exists ('array_column ' )) {
194
- $ data = \array_column ($ this ->data , $ columnKey , $ indexKey );
195
- } else {
196
- $ data = Helper::arrayColumn ($ this ->data , $ columnKey , $ indexKey );
197
- }
193
+ $ data = \array_column ($ this ->data , $ columnKey , $ indexKey );
198
194
199
195
return new static ($ data );
200
196
}
201
197
198
+ public function where (array $ props )
199
+ {
200
+ return $ this ->filter ($ this ->matcher ($ props ));
201
+ }
202
+
203
+ public function findWhere (array $ props )
204
+ {
205
+ return $ this ->find ($ this ->matcher ($ props ));
206
+ }
207
+
208
+ protected function matcher (array $ props )
209
+ {
210
+ return function ($ value , $ index ) use ($ props ) {
211
+ foreach ($ props as $ prop => $ criteria ) {
212
+ if ($ value !== $ criteria || $ prop != $ index ) {
213
+ return false ;
214
+ }
215
+ }
216
+
217
+ return true ;
218
+ };
219
+ }
220
+
202
221
/**
203
222
* {@inheritdoc}
204
223
*/
@@ -262,6 +281,11 @@ public function __toString()
262
281
{
263
282
return \json_encode ($ this ->data );
264
283
}
284
+
285
+ public static function _ ($ data )
286
+ {
287
+ return new static ($ data );
288
+ }
265
289
}
266
290
267
291
\class_alias ('Ahc\Underscore\Underscore ' , 'Ahc\Underscore ' );
0 commit comments