1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ILess |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace ILess\Node; |
11
|
|
|
|
12
|
|
|
use ILess\Context; |
13
|
|
|
use ILess\Node; |
14
|
|
|
use ILess\Output\OutputInterface; |
15
|
|
|
use ILess\Util\UnitConversion; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Dimension unit. |
19
|
|
|
*/ |
20
|
|
|
class UnitNode extends Node implements ComparableInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Length regular expression. |
24
|
|
|
*/ |
25
|
|
|
const LENGTH_REGEXP = '/px|em|%|in|cm|mm|pc|pt|ex/'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Numerator. |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
public $numerator = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Denominator. |
36
|
|
|
* |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
public $denominator = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The backup unit to use when unit is empty. |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
public $backupUnit; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Constructor. |
50
|
|
|
* |
51
|
|
|
* @param array $numerator |
52
|
|
|
* @param array $denominator |
53
|
|
|
* @param string $backupUnit |
54
|
|
|
*/ |
55
|
|
|
public function __construct(array $numerator = [], array $denominator = [], $backupUnit = null) |
56
|
|
|
{ |
57
|
|
|
$this->numerator = $numerator; |
58
|
|
|
$this->denominator = $denominator; |
59
|
|
|
|
60
|
|
|
sort($this->numerator); |
61
|
|
|
sort($this->denominator); |
62
|
|
|
|
63
|
|
|
if ($backupUnit) { |
64
|
|
|
$this->backupUnit = $backupUnit; |
65
|
|
|
} elseif ($numerator) { |
66
|
|
|
$this->backupUnit = $numerator[0]; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Converts to string. |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function toString() |
76
|
|
|
{ |
77
|
|
|
$returnStr = implode('*', $this->numerator); |
78
|
|
|
foreach ($this->denominator as $d) { |
79
|
|
|
$returnStr .= '/' . $d; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $returnStr; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function compare(Node $other) |
89
|
|
|
{ |
90
|
|
|
// we can compare only units |
91
|
|
|
if ($other instanceof self) { |
92
|
|
|
return $this->is($other->toString()) ? 0 : null; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Take care about backup unit when cloning, see the constructor. |
98
|
|
|
*/ |
99
|
|
|
public function __clone() |
100
|
|
|
{ |
101
|
|
|
if (null === $this->backupUnit && count($this->numerator)) { |
102
|
|
|
$this->backupUnit = $this->numerator[0]; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Is the unit equal to $unitString? |
108
|
|
|
* |
109
|
|
|
* @param string $unitString |
110
|
|
|
* |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
public function is($unitString) |
114
|
|
|
{ |
115
|
|
|
return strtoupper($this->toString()) === strtoupper($unitString); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Is the unit length unit? |
120
|
|
|
* |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
|
|
public function isLength() |
124
|
|
|
{ |
125
|
|
|
$css = $this->toCSS(new Context()); |
126
|
|
|
|
127
|
|
|
return (bool) preg_match(self::LENGTH_REGEXP, $css); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Is the unit angle unit? |
132
|
|
|
* |
133
|
|
|
* @return bool |
134
|
|
|
*/ |
135
|
|
|
public function isAngle() |
136
|
|
|
{ |
137
|
|
|
$angle = UnitConversion::getGroup('angle'); |
138
|
|
|
|
139
|
|
|
return isset($angle[$this->toCSS(new Context())]); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Is the unit empty? |
144
|
|
|
* |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
|
|
public function isEmpty() |
148
|
|
|
{ |
149
|
|
|
return count($this->numerator) === 0 && count($this->denominator) === 0; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Is singular? |
154
|
|
|
* |
155
|
|
|
* @return bool |
156
|
|
|
*/ |
157
|
|
|
public function isSingular() |
158
|
|
|
{ |
159
|
|
|
return count($this->numerator) <= 1 && count($this->denominator) == 0; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function usedUnits() |
163
|
|
|
{ |
164
|
|
|
$result = []; |
165
|
|
|
foreach (UnitConversion::getGroups() as $groupName) { |
166
|
|
|
$group = UnitConversion::getGroup($groupName); |
167
|
|
|
for ($i = 0; $i < count($this->numerator); ++$i) { |
|
|
|
|
168
|
|
|
$atomicUnit = $this->numerator[$i]; |
169
|
|
|
if (isset($group[$atomicUnit]) && !isset($result[$groupName])) { |
170
|
|
|
$result[$groupName] = $atomicUnit; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
for ($i = 0; $i < count($this->denominator); ++$i) { |
|
|
|
|
174
|
|
|
$atomicUnit = $this->denominator[$i]; |
175
|
|
|
if (isset($group[$atomicUnit]) && !isset($result[$groupName])) { |
176
|
|
|
$result[$groupName] = $atomicUnit; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $result; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function cancel() |
185
|
|
|
{ |
186
|
|
|
$counter = []; |
187
|
|
|
|
188
|
|
|
for ($i = 0; $i < count($this->numerator); ++$i) { |
|
|
|
|
189
|
|
|
$atomicUnit = $this->numerator[$i]; |
190
|
|
|
$counter[$atomicUnit] = (isset($counter[$atomicUnit]) ? $counter[$atomicUnit] : 0) + 1; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
for ($i = 0; $i < count($this->denominator); ++$i) { |
|
|
|
|
194
|
|
|
$atomicUnit = $this->denominator[$i]; |
195
|
|
|
$counter[$atomicUnit] = (isset($counter[$atomicUnit]) ? $counter[$atomicUnit] : 0) - 1; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$this->numerator = []; |
199
|
|
|
$this->denominator = []; |
200
|
|
|
|
201
|
|
|
foreach ($counter as $atomicUnit => $count) { |
202
|
|
|
if ($count > 0) { |
203
|
|
|
for ($i = 0; $i < $count; ++$i) { |
204
|
|
|
$this->numerator[] = $atomicUnit; |
205
|
|
|
} |
206
|
|
|
} elseif ($count < 0) { |
207
|
|
|
for ($i = 0; $i < -$count; ++$i) { |
208
|
|
|
$this->denominator[] = $atomicUnit; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
sort($this->numerator); |
214
|
|
|
sort($this->denominator); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* {@inheritdoc} |
219
|
|
|
*/ |
220
|
|
|
public function generateCSS(Context $context, OutputInterface $output) |
221
|
|
|
{ |
222
|
|
|
if (count($this->numerator) === 1) { |
223
|
|
|
$output->add($this->numerator[0]); |
224
|
|
|
} elseif (!$context->strictUnits && $this->backupUnit) { |
225
|
|
|
$output->add($this->backupUnit); |
226
|
|
|
} elseif (!$context->strictUnits && count($this->denominator)) { |
227
|
|
|
$output->add($this->denominator[0]); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Convert to string. |
233
|
|
|
* |
234
|
|
|
* @return string |
235
|
|
|
*/ |
236
|
|
|
public function __toString() |
237
|
|
|
{ |
238
|
|
|
return $this->toString(); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: