1 | <?php |
||
27 | class Utilities |
||
28 | { |
||
29 | /** |
||
30 | * Replace / with the system directory separator |
||
31 | * |
||
32 | * @param string $path the original path |
||
33 | * |
||
34 | * @return mixed |
||
35 | */ |
||
36 | 1 | public static function normalizeDirectorySeparator($path) |
|
40 | |||
41 | /** |
||
42 | * explode an array by lines that match a regular expression |
||
43 | * |
||
44 | * @param array $array the original array, should be a non-associative array |
||
45 | * @param string $regexp the regular expression |
||
46 | * |
||
47 | * @return array an array of array pieces |
||
48 | * @throws \InvalidArgumentException |
||
49 | */ |
||
50 | 3 | public static function pregSplitArray($array, $regexp) |
|
51 | { |
||
52 | 3 | if (static::isAssociative($array)) { |
|
53 | throw new \InvalidArgumentException('pregSplitArray only accepts non-associative arrays.'); |
||
54 | } |
||
55 | 3 | $lineNumbers = array(); |
|
56 | 3 | $arrOut = array(); |
|
57 | 3 | foreach ($array as $i => $line) { |
|
58 | 3 | if (preg_match($regexp, $line)) { |
|
59 | 3 | $lineNumbers[] = $i; |
|
60 | 3 | } |
|
61 | 3 | } |
|
62 | |||
63 | 3 | foreach ($lineNumbers as $i => $lineNum) { |
|
64 | 3 | if (isset($lineNumbers[$i + 1])) { |
|
65 | 2 | $arrOut[] = array_slice($array, $lineNum, $lineNumbers[$i + 1] - $lineNum); |
|
66 | 2 | } else { |
|
67 | 3 | $arrOut[] = array_slice($array, $lineNum); |
|
68 | } |
||
69 | 3 | } |
|
70 | |||
71 | 3 | return $arrOut; |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param array $array a flat array |
||
76 | * @param string $regexp a regular expression |
||
77 | * |
||
78 | * @return array |
||
79 | */ |
||
80 | 20 | public static function pregSplitFlatArray($array, $regexp) |
|
81 | { |
||
82 | 20 | $index = 0; |
|
83 | 20 | $slices = array(); |
|
84 | 20 | $slice = array(); |
|
85 | 20 | foreach ($array as $val) { |
|
86 | 20 | if (preg_match($regexp, $val) && !empty($slice)) { |
|
87 | 14 | $slices[$index] = $slice; |
|
88 | 14 | ++$index; |
|
89 | 14 | $slice = array(); |
|
90 | 14 | } |
|
91 | 20 | $slice[] = $val; |
|
92 | 20 | } |
|
93 | 20 | if (!empty($slice)) { |
|
94 | 20 | $slices[$index] = $slice; |
|
95 | 20 | } |
|
96 | |||
97 | 20 | return $slices; |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * Tell if an array is associative |
||
102 | * |
||
103 | * @param array $arr an array |
||
104 | * |
||
105 | * @return bool |
||
106 | */ |
||
107 | 3 | public static function isAssociative($arr) |
|
111 | } |
||
112 |