-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathListUtil.php
More file actions
128 lines (115 loc) · 3.38 KB
/
ListUtil.php
File metadata and controls
128 lines (115 loc) · 3.38 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
namespace PostCSS;
/**
* Contains helpers for safely splitting lists of CSS values, preserving parentheses and quotes.
*
* @link https://github.com/postcss/postcss/blob/master/lib/list.es6
*/
class ListUtil
{
public static function split($string, array $separators, $last = false)
{
$array = [];
$current = '';
$split = false;
$func = 0;
$quote = false;
$escape = false;
$stringLength = strlen($string);
for ($i = 0; $i < $stringLength; ++$i) {
$letter = $string[$i];
if ($quote) {
if ($escape) {
$escape = false;
} elseif ($letter === '\\') {
$escape = true;
} elseif ($letter === $quote) {
$quote = false;
}
} elseif ($letter === '"' || $letter === '\'') {
$quote = $letter;
} elseif ($letter === '(') {
$func += 1;
} elseif ($letter === ')') {
if ($func > 0) {
$func -= 1;
}
} elseif ($func === 0) {
if (in_array($letter, $separators, true)) {
$split = true;
}
}
if ($split) {
if ($current !== '') {
$array[] = trim($current);
}
$current = '';
$split = false;
} else {
$current .= $letter;
}
}
if ($last || $current !== '') {
$array[] = trim($current);
}
return $array;
}
/**
* Safely splits space-separated values (such as those for `background`, `border-radius`, and other shorthand properties).
*
* @param string $string Space-separated values
*
* @return string[] Split values
*
* @example
*
* \PostCSS\ListUtil::space('1px calc(10% + 1px)') //=> ['1px', 'calc(10% + 1px)']
*/
public static function space($string)
{
$spaces = [' ', '\n', '\t'];
return static::split($string, $spaces);
}
/**
* Safely splits comma-separated values (such as those for `transition-*` and `background` properties).
*
* @param string $string Comma-separated values
*
* @return string[] Split values
*
* @example
* \PostCSS\ListUtil::comma('black, linear-gradient(white, black)') //=> ['black', 'linear-gradient(white, black)']
*/
public static function comma($string)
{
$comma = ',';
return static::split($string, [$comma], true);
}
/**
* Check if a variable is an array whose keys are numeric.
*
* @param mixed $v
* @param bool $ifEmptyArrayReturn What to return if it's an empty array
*
* @return bool
*/
public static function isPlainArray($v, $ifEmptyArrayReturn = false)
{
$result = false;
if (is_array($v)) {
$keys = array_keys($v);
if (empty($keys)) {
$result = $ifEmptyReturn;
} else {
$result = true;
foreach ($keys as $key) {
if (!is_int($key)) {
$result = false;
break;
}
}
}
}
return $result;
}
}