diff options
Diffstat (limited to 'vendor/jfcherng/php-diff/src/Utility/Arr.php')
-rw-r--r-- | vendor/jfcherng/php-diff/src/Utility/Arr.php | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/vendor/jfcherng/php-diff/src/Utility/Arr.php b/vendor/jfcherng/php-diff/src/Utility/Arr.php new file mode 100644 index 0000000..9283e30 --- /dev/null +++ b/vendor/jfcherng/php-diff/src/Utility/Arr.php @@ -0,0 +1,47 @@ +<?php + +declare(strict_types=1); + +namespace Jfcherng\Diff\Utility; + +final class Arr +{ + /** + * Get a partial array slice with start/end indexes. + * + * @param array $array the array + * @param int $start the starting index (negative = count from backward) + * @param null|int $end the ending index (negative = count from backward) + * if is null, it returns a slice from $start to the end + * + * @return array array of all of the lines between the specified range + */ + public static function getPartialByIndex(array $array, int $start = 0, ?int $end = null): array + { + $count = \count($array); + + // make $end set + $end = $end ?? $count; + + // make $start non-negative + if ($start < 0) { + $start += $count; + + if ($start < 0) { + $start = 0; + } + } + + // make $end non-negative + if ($end < 0) { + $end += $count; + + if ($end < 0) { + $end = 0; + } + } + + // make the length non-negative + return \array_slice($array, $start, \max(0, $end - $start)); + } +} |