Debian Codegraph @

vendor/sabberworm/php-css-parser/src/Value/Size.php

raw
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Sabberworm\CSS\Value;
6
7 use Sabberworm\CSS\OutputFormat;
8 use Sabberworm\CSS\Parsing\ParserState;
9 use Sabberworm\CSS\Parsing\UnexpectedEOFException;
10 use Sabberworm\CSS\Parsing\UnexpectedTokenException;
11 use Sabberworm\CSS\ShortClassNameProvider;
12
13 use function Safe\preg_match;
14 use function Safe\preg_replace;
15
16 /**
17 * A `Size` consists of a numeric `size` value and a unit.
18 */
19 class Size extends PrimitiveValue
20 {
21 use ShortClassNameProvider;
22
23 /**
24 * vh/vw/vm(ax)/vmin/rem are absolute insofar as they don’t scale to the immediate parent (only the viewport)
25 */
26 private const ABSOLUTE_SIZE_UNITS = [
27 'px',
28 'pt',
29 'pc',
30 'cm',
31 'mm',
32 'mozmm',
33 'in',
34 'vh',
35 'dvh',
36 'svh',
37 'lvh',
38 'vw',
39 'vmin',
40 'vmax',
41 'rem',
42 ];
43
44 private const RELATIVE_SIZE_UNITS = ['%', 'em', 'ex', 'ch', 'fr'];
45
46 private const NON_SIZE_UNITS = ['deg', 'grad', 'rad', 's', 'ms', 'turn', 'Hz', 'kHz'];
47
48 /**
49 * @var array<int<1, max>, array<lowercase-string, non-empty-string>>|null
50 */
51 private static $SIZE_UNITS = null;
52
53 /**
54 * @var float
55 */
56 private $size;
57
58 /**
59 * @var string|null
60 */
61 private $unit;
62
63 /**
64 * @var bool
65 */
66 private $isColorComponent;
67
68 /**
69 * @param float|int|string $size
70 * @param int<1, max>|null $lineNumber
71 */
72 public function __construct($size, ?string $unit = null, bool $isColorComponent = false, ?int $lineNumber = null)
73 {
74 parent::__construct($lineNumber);
75 $this->size = (float) $size;
76 $this->unit = $unit;
77 $this->isColorComponent = $isColorComponent;
78 }
79
80 /**
81 * @throws UnexpectedEOFException
82 * @throws UnexpectedTokenException
83 *
84 * @internal since V8.8.0
85 */
86 public static function parse(ParserState $parserState, bool $isColorComponent = false): Size
87 {
88 $size = '';
89 if ($parserState->comes('-')) {
90 $size .= $parserState->consume('-');
91 }
92 while (\is_numeric($parserState->peek()) || $parserState->comes('.') || $parserState->comes('e', true)) {
93 if ($parserState->comes('.')) {
94 $size .= $parserState->consume('.');
95 } elseif ($parserState->comes('e', true)) {
96 $lookahead = $parserState->peek(1, 1);
97 if (\is_numeric($lookahead) || $lookahead === '+' || $lookahead === '-') {
98 $size .= $parserState->consume(2);
99 } else {
100 break; // Reached the unit part of the number like "em" or "ex"
101 }
102 } else {
103 $size .= $parserState->consume(1);
104 }
105 }
106
107 $unit = null;
108 $sizeUnits = self::getSizeUnits();
109 foreach ($sizeUnits as $length => &$values) {
110 $key = \strtolower($parserState->peek($length));
111 if (\array_key_exists($key, $values)) {
112 if (($unit = $values[$key]) !== null) {
113 $parserState->consume($length);
114 break;
115 }
116 }
117 }
118 return new Size((float) $size, $unit, $isColorComponent, $parserState->currentLine());
119 }
120
121 /**
122 * @return array<int<1, max>, array<lowercase-string, non-empty-string>>
123 */
124 private static function getSizeUnits(): array
125 {
126 if (!\is_array(self::$SIZE_UNITS)) {
127 self::$SIZE_UNITS = [];
128 $sizeUnits = \array_merge(self::ABSOLUTE_SIZE_UNITS, self::RELATIVE_SIZE_UNITS, self::NON_SIZE_UNITS);
129 foreach ($sizeUnits as $sizeUnit) {
130 $tokenLength = \strlen($sizeUnit);
131 if (!isset(self::$SIZE_UNITS[$tokenLength])) {
132 self::$SIZE_UNITS[$tokenLength] = [];
133 }
134 self::$SIZE_UNITS[$tokenLength][\strtolower($sizeUnit)] = $sizeUnit;
135 }
136
137 \krsort(self::$SIZE_UNITS, SORT_NUMERIC);
138 }
139
140 return self::$SIZE_UNITS;
141 }
142
143 public function setUnit(string $unit): void
144 {
145 $this->unit = $unit;
146 }
147
148 public function getUnit(): ?string
149 {
150 return $this->unit;
151 }
152
153 /**
154 * @param float|int|string $size
155 */
156 public function setSize($size): void
157 {
158 $this->size = (float) $size;
159 }
160
161 public function getSize(): float
162 {
163 return $this->size;
164 }
165
166 public function isColorComponent(): bool
167 {
168 return $this->isColorComponent;
169 }
170
171 /**
172 * Returns whether the number stored in this Size really represents a size (as in a length of something on screen).
173 *
174 * Returns `false` if the unit is an angle, a duration, a frequency, or the number is a component in a `Color`
175 * object.
176 */
177 public function isSize(): bool
178 {
179 if (\in_array($this->unit, self::NON_SIZE_UNITS, true)) {
180 return false;
181 }
182 return !$this->isColorComponent();
183 }
184
185 public function isRelative(): bool
186 {
187 if (\in_array($this->unit, self::RELATIVE_SIZE_UNITS, true)) {
188 return true;
189 }
190 if ($this->unit === null && $this->size !== 0.0) {
191 return true;
192 }
193 return false;
194 }
195
196 /**
197 * @return non-empty-string
198 */
199 public function render(OutputFormat $outputFormat): string
200 {
201 $locale = \localeconv();
202 $decimalPoint = \preg_quote($locale['decimal_point'], '/');
203 $size = preg_match('/[\\d\\.]+e[+-]?\\d+/i', (string) $this->size) === 1
204 ? preg_replace("/$decimalPoint?0+$/", '', \sprintf('%f', $this->size)) : (string) $this->size;
205
206 return preg_replace(["/$decimalPoint/", '/^(-?)0\\./'], ['.', '$1.'], $size) . ($this->unit ?? '');
207 }
208
209 /**
210 * @return array<string, bool|int|float|string|array<mixed>|null>
211 *
212 * @internal
213 */
214 public function getArrayRepresentation(): array
215 {
216 return [
217 'class' => $this->getShortClassName(),
218 // 'number' is the official W3C terminology (not 'size')
219 'number' => $this->size,
220 'unit' => $this->unit,
221 ];
222 }
223 }