CSSFunction.php
3 years ago
CSSString.php
3 years ago
CalcFunction.php
3 years ago
CalcRuleValueList.php
3 years ago
Color.php
3 years ago
LineName.php
3 years ago
PrimitiveValue.php
3 years ago
RuleValueList.php
3 years ago
Size.php
3 years ago
URL.php
3 years ago
Value.php
3 years ago
ValueList.php
3 years ago
Size.php
186 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Sabberworm\CSS\Value; |
| 4 | |
| 5 | use IAWP\Sabberworm\CSS\OutputFormat; |
| 6 | use IAWP\Sabberworm\CSS\Parsing\ParserState; |
| 7 | use IAWP\Sabberworm\CSS\Parsing\UnexpectedEOFException; |
| 8 | use IAWP\Sabberworm\CSS\Parsing\UnexpectedTokenException; |
| 9 | class Size extends PrimitiveValue |
| 10 | { |
| 11 | /** |
| 12 | * vh/vw/vm(ax)/vmin/rem are absolute insofar as they don’t scale to the immediate parent (only the viewport) |
| 13 | * |
| 14 | * @var array<int, string> |
| 15 | */ |
| 16 | const ABSOLUTE_SIZE_UNITS = ['px', 'cm', 'mm', 'mozmm', 'in', 'pt', 'pc', 'vh', 'vw', 'vmin', 'vmax', 'rem']; |
| 17 | /** |
| 18 | * @var array<int, string> |
| 19 | */ |
| 20 | const RELATIVE_SIZE_UNITS = ['%', 'em', 'ex', 'ch', 'fr']; |
| 21 | /** |
| 22 | * @var array<int, string> |
| 23 | */ |
| 24 | const NON_SIZE_UNITS = ['deg', 'grad', 'rad', 's', 'ms', 'turns', 'Hz', 'kHz']; |
| 25 | /** |
| 26 | * @var array<int, array<string, string>>|null |
| 27 | */ |
| 28 | private static $SIZE_UNITS = null; |
| 29 | /** |
| 30 | * @var float |
| 31 | */ |
| 32 | private $fSize; |
| 33 | /** |
| 34 | * @var string|null |
| 35 | */ |
| 36 | private $sUnit; |
| 37 | /** |
| 38 | * @var bool |
| 39 | */ |
| 40 | private $bIsColorComponent; |
| 41 | /** |
| 42 | * @param float|int|string $fSize |
| 43 | * @param string|null $sUnit |
| 44 | * @param bool $bIsColorComponent |
| 45 | * @param int $iLineNo |
| 46 | */ |
| 47 | public function __construct($fSize, $sUnit = null, $bIsColorComponent = \false, $iLineNo = 0) |
| 48 | { |
| 49 | parent::__construct($iLineNo); |
| 50 | $this->fSize = (float) $fSize; |
| 51 | $this->sUnit = $sUnit; |
| 52 | $this->bIsColorComponent = $bIsColorComponent; |
| 53 | } |
| 54 | /** |
| 55 | * @param bool $bIsColorComponent |
| 56 | * |
| 57 | * @return Size |
| 58 | * |
| 59 | * @throws UnexpectedEOFException |
| 60 | * @throws UnexpectedTokenException |
| 61 | */ |
| 62 | public static function parse(ParserState $oParserState, $bIsColorComponent = \false) |
| 63 | { |
| 64 | $sSize = ''; |
| 65 | if ($oParserState->comes('-')) { |
| 66 | $sSize .= $oParserState->consume('-'); |
| 67 | } |
| 68 | while (\is_numeric($oParserState->peek()) || $oParserState->comes('.')) { |
| 69 | if ($oParserState->comes('.')) { |
| 70 | $sSize .= $oParserState->consume('.'); |
| 71 | } else { |
| 72 | $sSize .= $oParserState->consume(1); |
| 73 | } |
| 74 | } |
| 75 | $sUnit = null; |
| 76 | $aSizeUnits = self::getSizeUnits(); |
| 77 | foreach ($aSizeUnits as $iLength => &$aValues) { |
| 78 | $sKey = \strtolower($oParserState->peek($iLength)); |
| 79 | if (\array_key_exists($sKey, $aValues)) { |
| 80 | if (($sUnit = $aValues[$sKey]) !== null) { |
| 81 | $oParserState->consume($iLength); |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | return new Size((float) $sSize, $sUnit, $bIsColorComponent, $oParserState->currentLine()); |
| 87 | } |
| 88 | /** |
| 89 | * @return array<int, array<string, string>> |
| 90 | */ |
| 91 | private static function getSizeUnits() |
| 92 | { |
| 93 | if (!\is_array(self::$SIZE_UNITS)) { |
| 94 | self::$SIZE_UNITS = []; |
| 95 | foreach (\array_merge(self::ABSOLUTE_SIZE_UNITS, self::RELATIVE_SIZE_UNITS, self::NON_SIZE_UNITS) as $val) { |
| 96 | $iSize = \strlen($val); |
| 97 | if (!isset(self::$SIZE_UNITS[$iSize])) { |
| 98 | self::$SIZE_UNITS[$iSize] = []; |
| 99 | } |
| 100 | self::$SIZE_UNITS[$iSize][\strtolower($val)] = $val; |
| 101 | } |
| 102 | \krsort(self::$SIZE_UNITS, \SORT_NUMERIC); |
| 103 | } |
| 104 | return self::$SIZE_UNITS; |
| 105 | } |
| 106 | /** |
| 107 | * @param string $sUnit |
| 108 | * |
| 109 | * @return void |
| 110 | */ |
| 111 | public function setUnit($sUnit) |
| 112 | { |
| 113 | $this->sUnit = $sUnit; |
| 114 | } |
| 115 | /** |
| 116 | * @return string|null |
| 117 | */ |
| 118 | public function getUnit() |
| 119 | { |
| 120 | return $this->sUnit; |
| 121 | } |
| 122 | /** |
| 123 | * @param float|int|string $fSize |
| 124 | */ |
| 125 | public function setSize($fSize) |
| 126 | { |
| 127 | $this->fSize = (float) $fSize; |
| 128 | } |
| 129 | /** |
| 130 | * @return float |
| 131 | */ |
| 132 | public function getSize() |
| 133 | { |
| 134 | return $this->fSize; |
| 135 | } |
| 136 | /** |
| 137 | * @return bool |
| 138 | */ |
| 139 | public function isColorComponent() |
| 140 | { |
| 141 | return $this->bIsColorComponent; |
| 142 | } |
| 143 | /** |
| 144 | * Returns whether the number stored in this Size really represents a size (as in a length of something on screen). |
| 145 | * |
| 146 | * @return false if the unit an angle, a duration, a frequency or the number is a component in a Color object. |
| 147 | */ |
| 148 | public function isSize() |
| 149 | { |
| 150 | if (\in_array($this->sUnit, self::NON_SIZE_UNITS, \true)) { |
| 151 | return \false; |
| 152 | } |
| 153 | return !$this->isColorComponent(); |
| 154 | } |
| 155 | /** |
| 156 | * @return bool |
| 157 | */ |
| 158 | public function isRelative() |
| 159 | { |
| 160 | if (\in_array($this->sUnit, self::RELATIVE_SIZE_UNITS, \true)) { |
| 161 | return \true; |
| 162 | } |
| 163 | if ($this->sUnit === null && $this->fSize != 0) { |
| 164 | return \true; |
| 165 | } |
| 166 | return \false; |
| 167 | } |
| 168 | /** |
| 169 | * @return string |
| 170 | */ |
| 171 | public function __toString() |
| 172 | { |
| 173 | return $this->render(new OutputFormat()); |
| 174 | } |
| 175 | /** |
| 176 | * @return string |
| 177 | */ |
| 178 | public function render(OutputFormat $oOutputFormat) |
| 179 | { |
| 180 | $l = \localeconv(); |
| 181 | $sPoint = \preg_quote($l['decimal_point'], '/'); |
| 182 | $sSize = \preg_match("/[\\d\\.]+e[+-]?\\d+/i", (string) $this->fSize) ? \preg_replace("/{$sPoint}?0+\$/", "", \sprintf("%f", $this->fSize)) : $this->fSize; |
| 183 | return \preg_replace(["/{$sPoint}/", "/^(-?)0\\./"], ['.', '$1.'], $sSize) . ($this->sUnit === null ? '' : $this->sUnit); |
| 184 | } |
| 185 | } |
| 186 |