-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLength.php
More file actions
139 lines (112 loc) · 3.75 KB
/
Length.php
File metadata and controls
139 lines (112 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
* @see https://developer.mozilla.org/en-US/docs/Web/CSS/length
* @see https://www.w3.org/TR/css-length-3
* @see https://www.w3.org/TR/css-syntax-3
*/
namespace RowBloom\CssLength;
use BadMethodCallException;
use Stringable;
/**
* @method float toPxFloat()
* @method float toMmFloat()
* @method float toCmFloat()
* @method float toInFloat()
* @method float toPtFloat()
* @method float toPcFloat()
* @method string toPxString()
* @method string toMmString()
* @method string toCmString()
* @method string toInString()
* @method string toPtString()
* @method string toPcString()
*/
class Length implements Stringable
{
public static function fromDimension(string $value): static
{
$value = trim((string) $value);
$parsed = static::parseLengthDimension($value);
return Length::fromNumberUnit($parsed['value'], LengthUnit::from($parsed['unit']));
}
public static function fromNumberUnit(float|int $value, LengthUnit $unit): static
{
return new static($value, $unit);
}
final protected function __construct(
public readonly float|int $value,
public readonly LengthUnit $unit,
public ?LengthUnit $readUnit = null,
) {
$this->readUnit ??= $this->unit;
}
// TODO: setReference for relative units
// TODO: support functional notation. calc(50% - 2em)
/**
* @return array{value: float, unit: string}
*
* @see https://www.w3.org/TR/css-values-3/#lengths
*/
protected static function parseLengthDimension(string $value): array
{
$value = trim($value);
if ($value === '0') {
return ['value' => 0, 'unit' => 'px'];
}
$units = implode('|', LengthUnit::absoluteUnits());
$regex = "/^(?<value>\d+(\.\d+)?)(?<unit>{$units})$/";
preg_match($regex, $value, $parsed) ?:
throw new CssLengthException("{Invalid CSS dimension: '{$value}' (must be in <number><unit> format).");
/** @phpstan-ignore-next-line */
return $parsed;
}
public function setReadUnit(LengthUnit $readUnit): static
{
$this->readUnit = $readUnit;
return $this;
}
public function convert(LengthUnit $readUnit): static
{
return (clone $this)->setReadUnit($readUnit);
}
public function value(?LengthUnit $readUnit = null): float
{
if ($this->unit === $this->readUnit & is_null($readUnit)) {
return $this->value;
}
return LengthUnit::absoluteUnitsEquivalence(
$this->unit,
$readUnit ?? $this->readUnit,
$this->value
);
}
public function toFloat(?LengthUnit $readUnit = null): float
{
return $this->value($readUnit);
}
public function toString(?LengthUnit $readUnit = null): string
{
return $this->value($readUnit).($readUnit?->value ?? $this->readUnit->value);
}
public function __toString(): string
{
return $this->value().$this->readUnit->value;
}
/** @phpstan-ignore-next-line */
public function __call($name, $arguments): float|string
{
if (preg_match('/^to(?<unit>..)Float$/', $name, $matchedUnit) === 1) {
$readUnit = LengthUnit::tryFrom(strtolower($matchedUnit['unit']));
if (! is_null($readUnit)) {
return $this->toFloat($readUnit);
}
}
if (preg_match('/^to(?<unit>..)String$/', $name, $matchedUnit) === 1) {
$readUnit = LengthUnit::tryFrom(strtolower($matchedUnit['unit']));
if (! is_null($readUnit)) {
return $this->toString($readUnit);
}
}
throw new BadMethodCallException('Call to undefined method '.static::class.'::'.$name.'().');
}
}