From 740520b2b32233adf7a49388e3cc6c2b5b74535d Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 11 Jan 2021 00:03:10 +0100 Subject: [PATCH] added Arrays::first(), last() & contains() --- src/Utils/Arrays.php | 30 ++++++++++++++++++++++++++++++ tests/Utils/Arrays.contains().phpt | 16 ++++++++++++++++ tests/Utils/Arrays.first().phpt | 21 +++++++++++++++++++++ tests/Utils/Arrays.last().phpt | 20 ++++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 tests/Utils/Arrays.contains().phpt create mode 100644 tests/Utils/Arrays.first().phpt create mode 100644 tests/Utils/Arrays.last().phpt diff --git a/src/Utils/Arrays.php b/src/Utils/Arrays.php index 8f78fcc39..a93fe7ddf 100644 --- a/src/Utils/Arrays.php +++ b/src/Utils/Arrays.php @@ -99,6 +99,36 @@ public static function searchKey(array $array, $key): ?int } + /** + * Tests an array for the presence of value. + * @param mixed $value + */ + public static function contains(array $array, $value): bool + { + return in_array($value, $array, true); + } + + + /** + * Returns the first item from the array or null. + * @return mixed + */ + public static function first(array $array) + { + return count($array) ? reset($array) : null; + } + + + /** + * Returns the last item from the array or null. + * @return mixed + */ + public static function last(array $array) + { + return count($array) ? end($array) : null; + } + + /** * Inserts the contents of the $inserted array into the $array immediately after the $key. * If $key is null (or does not exist), it is inserted at the beginning. diff --git a/tests/Utils/Arrays.contains().phpt b/tests/Utils/Arrays.contains().phpt new file mode 100644 index 000000000..5bc808768 --- /dev/null +++ b/tests/Utils/Arrays.contains().phpt @@ -0,0 +1,16 @@ +