diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:38:04 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:38:04 +0000 |
commit | 1ff5c35de5dbd70a782875a91dd2232fd01b002b (patch) | |
tree | 77d9ce5e1bf78b3e6ef79f8f6e7861e2ced3c09b /vendor/ipl/stdlib/src/Seq.php | |
parent | Initial commit. (diff) | |
download | icinga-php-library-upstream/0.10.1.tar.xz icinga-php-library-upstream/0.10.1.zip |
Adding upstream version 0.10.1.upstream/0.10.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/ipl/stdlib/src/Seq.php')
-rw-r--r-- | vendor/ipl/stdlib/src/Seq.php | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/vendor/ipl/stdlib/src/Seq.php b/vendor/ipl/stdlib/src/Seq.php new file mode 100644 index 0000000..0f40c93 --- /dev/null +++ b/vendor/ipl/stdlib/src/Seq.php @@ -0,0 +1,108 @@ +<?php + +namespace ipl\Stdlib; + +/** + * Collection of utilities for traversables + */ +class Seq +{ + /** + * Check if the traversable contains the given needle + * + * @param array|iterable $traversable + * @param mixed $needle Might also be a closure + * @param bool $caseSensitive Whether strings should be compared case-sensitive + * + * @return bool + */ + public static function contains($traversable, $needle, $caseSensitive = true) + { + return self::find($traversable, $needle, $caseSensitive)[0] !== null; + } + + /** + * Search in the traversable for the given needle and return its key and value + * + * @param array|iterable $traversable + * @param mixed $needle Might also be a closure + * @param bool $caseSensitive Whether strings should be compared case-sensitive + * + * @return array An array with two entries, the first is the key, then the value. Both are null if nothing is found. + */ + public static function find($traversable, $needle, $caseSensitive = true) + { + $usesCallback = is_callable($needle); + if (! $usesCallback && $caseSensitive && is_array($traversable)) { + return [array_search($needle, $traversable, true), $needle]; + } + + if (! $caseSensitive && is_string($needle) && ! $usesCallback) { + $needle = strtolower($needle); + } + + foreach ($traversable as $key => $item) { + $originalItem = $item; + if (! $caseSensitive && is_string($item)) { + $item = strtolower($item); + } + + if ($usesCallback && $needle($item)) { + return [$key, $originalItem]; + } elseif ($item === $needle) { + return [$key, $originalItem]; + } + } + + return [null, null]; + } + + /** + * Search in the traversable for the given needle and return its key + * + * @param array|iterable $traversable + * @param mixed $needle Might also be a closure + * @param bool $caseSensitive Whether strings should be compared case-sensitive + * + * @return mixed|null Null if nothing is found + */ + public static function findKey($traversable, $needle, $caseSensitive = true) + { + return self::find($traversable, $needle, $caseSensitive)[0]; + } + + /** + * Search in the traversable for the given needle and return its value + * + * @param array|iterable $traversable + * @param mixed $needle Might also be a closure + * @param bool $caseSensitive Whether strings should be compared case-sensitive + * + * @return mixed|null Null if nothing is found + */ + public static function findValue($traversable, $needle, $caseSensitive = true) + { + $usesCallback = is_callable($needle); + if (! $usesCallback && $caseSensitive && is_array($traversable)) { + return isset($traversable[$needle]) ? $traversable[$needle] : null; + } + + if (! $caseSensitive && is_string($needle) && ! $usesCallback) { + $needle = strtolower($needle); + } + + foreach ($traversable as $key => $item) { + if (! $caseSensitive && is_string($key)) { + $key = strtolower($key); + } + + if ($usesCallback && $needle($key)) { + return $item; + } elseif ($key === $needle) { + return $item; + } + } + + return null; + } +} |