|
@@ 494-513 (lines=20) @@
|
| 491 |
|
public static function initial(array $array, $callback, $default = null) |
| 492 |
|
{ |
| 493 |
|
// When given a callable, keep counting as long as the callable returns a truthy value. |
| 494 |
|
if (is_callable($callback)) { |
| 495 |
|
$i = 0; |
| 496 |
|
|
| 497 |
|
foreach (array_reverse($array) as $key => $value) { |
| 498 |
|
if (!call_user_func($callback, $key, $value)) { |
| 499 |
|
break; |
| 500 |
|
} |
| 501 |
|
|
| 502 |
|
$i++; |
| 503 |
|
} |
| 504 |
|
|
| 505 |
|
// If we didn't get at least a single truthy value, return the default. |
| 506 |
|
if ($i === 0) { |
| 507 |
|
return $default; |
| 508 |
|
} |
| 509 |
|
|
| 510 |
|
// Otherwise we're just gonna overwrite the $callback and proceed as if it were an integer in the |
| 511 |
|
// first place. |
| 512 |
|
$callback = $i; |
| 513 |
|
} |
| 514 |
|
|
| 515 |
|
// At this point we need a positive integer, 1 at minimum. |
| 516 |
|
return array_slice($array, 0, -(int) (!$callback ? 1 : abs($callback))); |
|
@@ 805-824 (lines=20) @@
|
| 802 |
|
} |
| 803 |
|
|
| 804 |
|
// With a callable given, keep counting as long as the callable returns a truthy value. |
| 805 |
|
if (is_callable($callback)) { |
| 806 |
|
$i = 0; |
| 807 |
|
|
| 808 |
|
foreach ($array as $key => $value) { |
| 809 |
|
if (!call_user_func($callback, $key, $value)) { |
| 810 |
|
break; |
| 811 |
|
} |
| 812 |
|
|
| 813 |
|
$i++; |
| 814 |
|
} |
| 815 |
|
|
| 816 |
|
// If we didn't get at least a single truthy value, return the default. |
| 817 |
|
if ($i === 0) { |
| 818 |
|
return $default; |
| 819 |
|
} |
| 820 |
|
|
| 821 |
|
// Otherwise we're just gonna overwrite the $callback and proceed as if it were an integer in the |
| 822 |
|
// first place. |
| 823 |
|
$callback = $i; |
| 824 |
|
} |
| 825 |
|
|
| 826 |
|
// Return the final $callback elements. |
| 827 |
|
return array_slice($array, abs((int) $callback)); |