<?php
namespace Sabberworm\CSS\Value;
> use Sabberworm\CSS\OutputFormat;
use Sabberworm\CSS\Parsing\ParserState;
> use Sabberworm\CSS\Parsing\UnexpectedEOFException;
> use Sabberworm\CSS\Parsing\UnexpectedTokenException;
< class Size extends PrimitiveValue {
> class Size extends PrimitiveValue
> {
> /**
> * vh/vw/vm(ax)/vmin/rem are absolute insofar as they don’t scale to the immediate parent (only the viewport)
> *
> * @var array<int, string>
> */
> const ABSOLUTE_SIZE_UNITS = ['px', 'cm', 'mm', 'mozmm', 'in', 'pt', 'pc', 'vh', 'vw', 'vmin', 'vmax', 'rem'];
>
> /**
> * @var array<int, string>
> */
> const RELATIVE_SIZE_UNITS = ['%', 'em', 'ex', 'ch', 'fr'];
< const ABSOLUTE_SIZE_UNITS = 'px/cm/mm/mozmm/in/pt/pc/vh/vw/vm/vmin/vmax/rem'; //vh/vw/vm(ax)/vmin/rem are absolute insofar as they don’t scale to the immediate parent (only the viewport)
< const RELATIVE_SIZE_UNITS = '%/em/ex/ch/fr';
< const NON_SIZE_UNITS = 'deg/grad/rad/s/ms/turns/Hz/kHz';
> /**
> * @var array<int, string>
> */
> const NON_SIZE_UNITS = ['deg', 'grad', 'rad', 's', 'ms', 'turns', 'Hz', 'kHz'];
> /**
private static $SIZE_UNITS = null;
> * @var array<int, array<string, string>>|null
> */
private $fSize;
> /**
private $sUnit;
> * @var float
private $bIsColorComponent;
> */
>
public function __construct($fSize, $sUnit = null, $bIsColorComponent = false, $iLineNo = 0) {
> /**
parent::__construct($iLineNo);
> * @var string|null
$this->fSize = floatval($fSize);
> */
$this->sUnit = $sUnit;
>
$this->bIsColorComponent = $bIsColorComponent;
> /**
}
> * @var bool
> */
< public function __construct($fSize, $sUnit = null, $bIsColorComponent = false, $iLineNo = 0) {
> /**
> * @param float|int|string $fSize
> * @param string|null $sUnit
> * @param bool $bIsColorComponent
> * @param int $iLineNo
> */
> public function __construct($fSize, $sUnit = null, $bIsColorComponent = false, $iLineNo = 0)
> {
< $this->fSize = floatval($fSize);
> $this->fSize = (float)$fSize;
< public static function parse(ParserState $oParserState, $bIsColorComponent = false) {
> /**
> * @param bool $bIsColorComponent
> *
> * @return Size
> *
> * @throws UnexpectedEOFException
> * @throws UnexpectedTokenException
> */
> public static function parse(ParserState $oParserState, $bIsColorComponent = false)
> {
$sSize .= $oParserState->consume('-');
}
while (is_numeric($oParserState->peek()) || $oParserState->comes('.')) {
if ($oParserState->comes('.')) {
$sSize .= $oParserState->consume('.');
} else {
$sSize .= $oParserState->consume(1);
}
}
$sUnit = null;
$aSizeUnits = self::getSizeUnits();
foreach($aSizeUnits as $iLength => &$aValues) {
$sKey = strtolower($oParserState->peek($iLength));
if(array_key_exists($sKey, $aValues)) {
if (($sUnit = $aValues[$sKey]) !== null) {
$oParserState->consume($iLength);
break;
}
}
}
< return new Size(floatval($sSize), $sUnit, $bIsColorComponent, $oParserState->currentLine());
> return new Size((float)$sSize, $sUnit, $bIsColorComponent, $oParserState->currentLine());
}
< private static function getSizeUnits() {
< if(self::$SIZE_UNITS === null) {
< self::$SIZE_UNITS = array();
< foreach (explode('/', Size::ABSOLUTE_SIZE_UNITS.'/'.Size::RELATIVE_SIZE_UNITS.'/'.Size::NON_SIZE_UNITS) as $val) {
> /**
> * @return array<int, array<string, string>>
> */
> private static function getSizeUnits()
> {
> if (!is_array(self::$SIZE_UNITS)) {
> self::$SIZE_UNITS = [];
> foreach (array_merge(self::ABSOLUTE_SIZE_UNITS, self::RELATIVE_SIZE_UNITS, self::NON_SIZE_UNITS) as $val) {
$iSize = strlen($val);
if(!isset(self::$SIZE_UNITS[$iSize])) {
< self::$SIZE_UNITS[$iSize] = array();
> self::$SIZE_UNITS[$iSize] = [];
}
self::$SIZE_UNITS[$iSize][strtolower($val)] = $val;
}
< // FIXME: Should we not order the longest units first?
< ksort(self::$SIZE_UNITS, SORT_NUMERIC);
> krsort(self::$SIZE_UNITS, SORT_NUMERIC);
}
return self::$SIZE_UNITS;
}
< public function setUnit($sUnit) {
> /**
> * @param string $sUnit
> *
> * @return void
> */
> public function setUnit($sUnit)
> {
$this->sUnit = $sUnit;
}
< public function getUnit() {
> /**
> * @return string|null
> */
> public function getUnit()
> {
return $this->sUnit;
}
< public function setSize($fSize) {
< $this->fSize = floatval($fSize);
> /**
> * @param float|int|string $fSize
> */
> public function setSize($fSize)
> {
> $this->fSize = (float)$fSize;
}
< public function getSize() {
> /**
> * @return float
> */
> public function getSize()
> {
return $this->fSize;
}
< public function isColorComponent() {
> /**
> * @return bool
> */
> public function isColorComponent()
> {
return $this->bIsColorComponent;
}
/**
* Returns whether the number stored in this Size really represents a size (as in a length of something on screen).
> *
* @return false if the unit an angle, a duration, a frequency or the number is a component in a Color object.
*/
< public function isSize() {
< if (in_array($this->sUnit, explode('/', self::NON_SIZE_UNITS))) {
> public function isSize()
> {
> if (in_array($this->sUnit, self::NON_SIZE_UNITS, true)) {
return false;
}
return !$this->isColorComponent();
}
< public function isRelative() {
< if (in_array($this->sUnit, explode('/', self::RELATIVE_SIZE_UNITS))) {
> /**
> * @return bool
> */
> public function isRelative()
> {
> if (in_array($this->sUnit, self::RELATIVE_SIZE_UNITS, true)) {
return true;
}
if ($this->sUnit === null && $this->fSize != 0) {
return true;
}
return false;
}
< public function __toString() {
< return $this->render(new \Sabberworm\CSS\OutputFormat());
> /**
> * @return string
> */
> public function __toString()
> {
> return $this->render(new OutputFormat());
}
< public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
> /**
> * @return string
> */
> public function render(OutputFormat $oOutputFormat)
> {
$l = localeconv();
$sPoint = preg_quote($l['decimal_point'], '/');
< return preg_replace(array("/$sPoint/", "/^(-?)0\./"), array('.', '$1.'), $this->fSize) . ($this->sUnit === null ? '' : $this->sUnit);
> $sSize = preg_match("/[\d\.]+e[+-]?\d+/i", (string)$this->fSize)
> ? preg_replace("/$sPoint?0+$/", "", sprintf("%f", $this->fSize)) : $this->fSize;
> return preg_replace(["/$sPoint/", "/^(-?)0\./"], ['.', '$1.'], $sSize)
> . ($this->sUnit === null ? '' : $this->sUnit);
}
<
}