blob: 9283e304b60dff054c126e2922ea89cd7bf5f8b1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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));
}
}
|