@@ 338-365 (lines=28) @@ | ||
335 | * @throws \InvalidArgumentException When $elements is neither a valid integer nor a callable. |
|
336 | * @return mixed |
|
337 | */ |
|
338 | public static function first(array $array, $elements = null, $default = null) |
|
339 | { |
|
340 | if (empty($array)) { |
|
341 | return $default; |
|
342 | } |
|
343 | ||
344 | // Most common use case - simply return the first value of the array. |
|
345 | if (!isset($elements) || $elements === 1) { |
|
346 | return reset($array); |
|
347 | } |
|
348 | ||
349 | // With a integer given, return a slice containing the first $callback elements. |
|
350 | if (is_int($elements)) { |
|
351 | ||
352 | if ($elements < 1) { |
|
353 | throw new \InvalidArgumentException("At least 1 element must be requested, while [$elements] were requested."); |
|
354 | } |
|
355 | ||
356 | return array_slice($array, 0, $elements); |
|
357 | } |
|
358 | ||
359 | // With a callable given, return the first value which passes the given truth test. |
|
360 | if (is_callable($elements)) { |
|
361 | return static::find($array, $elements, $default); |
|
362 | } |
|
363 | ||
364 | throw new \InvalidArgumentException('Expected $callback to be a positive integer or a callable, got ['.diagnostics\Debug::getTypeName($elements).'] instead.'); |
|
365 | } |
|
366 | ||
367 | /** |
|
368 | * Flattens a multi-dimensional array. |
|
@@ 555-582 (lines=28) @@ | ||
552 | * @throws \InvalidArgumentException When $elements is neither a valid integer nor a callable. |
|
553 | * @return mixed |
|
554 | */ |
|
555 | public static function last(array $array, $elements = null, $default = null) |
|
556 | { |
|
557 | if (empty($array)) { |
|
558 | return $default; |
|
559 | } |
|
560 | ||
561 | // Most common use case - simply return the last value of the array. |
|
562 | if (!isset($elements) || $elements === 1) { |
|
563 | return end($array); |
|
564 | } |
|
565 | ||
566 | // With a integer given, return a slice containing the last $elements elements. |
|
567 | if (is_int($elements)) { |
|
568 | ||
569 | if ($elements < 1) { |
|
570 | throw new \InvalidArgumentException("At least 1 element must be requested, while [$elements] were requested."); |
|
571 | } |
|
572 | ||
573 | return array_slice($array, -$elements); |
|
574 | } |
|
575 | ||
576 | // With a callable given, return the last value which passes the given truth test. |
|
577 | if (is_callable($elements)) { |
|
578 | return static::find(array_reverse($array), $elements, $default); |
|
579 | } |
|
580 | ||
581 | throw new \InvalidArgumentException('Expected $elements to be a positive integer or a callable, got ['.diagnostics\Debug::getTypeName($elements).'] instead.'); |
|
582 | } |
|
583 | ||
584 | /** |
|
585 | * Returns the biggest value from the given array. |