1 | <?php |
||
2 | |||
3 | namespace BaoPham\DynamoDb; |
||
4 | |||
5 | /** |
||
6 | * Class H |
||
7 | * |
||
8 | * Short for "Helper". |
||
9 | * We often get breaking changes from Laravel Helpers, so to ensure this won't happen again, we port the helpers here. |
||
10 | * |
||
11 | * @package BaoPham\DynamoDb |
||
12 | */ |
||
13 | // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
||
14 | class H |
||
15 | { |
||
16 | 80 | public static function array_first($array, callable $callback = null, $default = null) |
|
17 | { |
||
18 | 80 | if (is_null($callback)) { |
|
19 | 2 | if (empty($array)) { |
|
20 | return static::value($default); |
||
21 | } |
||
22 | 2 | foreach ($array as $item) { |
|
23 | 2 | return $item; |
|
24 | } |
||
25 | } |
||
26 | 78 | foreach ($array as $key => $value) { |
|
27 | 78 | if (call_user_func($callback, $value, $key)) { |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
28 | 78 | return $value; |
|
29 | } |
||
30 | } |
||
31 | 60 | return static::value($default); |
|
32 | } |
||
33 | |||
34 | 60 | public static function value($value) |
|
35 | { |
||
36 | 60 | return $value instanceof \Closure ? $value() : $value; |
|
37 | } |
||
38 | } |
||
39 | // phpcs:enable Squiz.Classes.ValidClassName.NotCamelCaps |
||
40 |