Skip to content

Commit d23f08a

Browse files
authored
Merge pull request MyIntervals#253 from oliverklee/cleanup/sizes
Simplify the size handling
2 parents 4b8d3d6 + df220b9 commit d23f08a

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

src/Value/Size.php

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,26 @@
77
class Size extends PrimitiveValue
88
{
99

10-
// vh/vw/vm(ax)/vmin/rem are absolute insofar as they don’t scale to the immediate parent (only the viewport)
11-
const ABSOLUTE_SIZE_UNITS = 'px/cm/mm/mozmm/in/pt/pc/vh/vw/vmin/vmax/rem';
12-
const RELATIVE_SIZE_UNITS = '%/em/ex/ch/fr';
13-
const NON_SIZE_UNITS = 'deg/grad/rad/s/ms/turns/Hz/kHz';
10+
/**
11+
* vh/vw/vm(ax)/vmin/rem are absolute insofar as they don’t scale to the immediate parent (only the viewport)
12+
*
13+
* @var array<int, string>
14+
*/
15+
const ABSOLUTE_SIZE_UNITS = ['px', 'cm', 'mm', 'mozmm', 'in', 'pt', 'pc', 'vh', 'vw', 'vmin', 'vmax', 'rem'];
1416

17+
/**
18+
* @var array<int, string>
19+
*/
20+
const RELATIVE_SIZE_UNITS = ['%', 'em', 'ex', 'ch', 'fr'];
21+
22+
/**
23+
* @var array<int, string>
24+
*/
25+
const NON_SIZE_UNITS = ['deg', 'grad', 'rad', 's', 'ms', 'turns', 'Hz', 'kHz'];
26+
27+
/**
28+
* @var array<int, array<string, string>>|null
29+
*/
1530
private static $SIZE_UNITS = null;
1631

1732
private $fSize;
@@ -56,14 +71,14 @@ public static function parse(ParserState $oParserState, $bIsColorComponent = fal
5671
return new Size((float)$sSize, $sUnit, $bIsColorComponent, $oParserState->currentLine());
5772
}
5873

74+
/**
75+
* @return array<int, array<string, string>>
76+
*/
5977
private static function getSizeUnits()
6078
{
61-
if (self::$SIZE_UNITS === null) {
79+
if (!is_array(self::$SIZE_UNITS)) {
6280
self::$SIZE_UNITS = [];
63-
foreach (
64-
explode('/', Size::ABSOLUTE_SIZE_UNITS . '/' . Size::RELATIVE_SIZE_UNITS . '/' . Size::NON_SIZE_UNITS)
65-
as $val
66-
) {
81+
foreach (array_merge(self::ABSOLUTE_SIZE_UNITS, self::RELATIVE_SIZE_UNITS, self::NON_SIZE_UNITS) as $val) {
6782
$iSize = strlen($val);
6883
if (!isset(self::$SIZE_UNITS[$iSize])) {
6984
self::$SIZE_UNITS[$iSize] = [];
@@ -109,15 +124,15 @@ public function isColorComponent()
109124
*/
110125
public function isSize()
111126
{
112-
if (in_array($this->sUnit, explode('/', self::NON_SIZE_UNITS))) {
127+
if (in_array($this->sUnit, self::NON_SIZE_UNITS, true)) {
113128
return false;
114129
}
115130
return !$this->isColorComponent();
116131
}
117132

118133
public function isRelative()
119134
{
120-
if (in_array($this->sUnit, explode('/', self::RELATIVE_SIZE_UNITS))) {
135+
if (in_array($this->sUnit, self::RELATIVE_SIZE_UNITS, true)) {
121136
return true;
122137
}
123138
if ($this->sUnit === null && $this->fSize != 0) {

0 commit comments

Comments
 (0)