Skip to content

Commit 80cb4c4

Browse files
authored
[TASK] Implement getArrayRepresentation() for Size (#1452)
Part of #1440.
1 parent ecde963 commit 80cb4c4

2 files changed

Lines changed: 48 additions & 3 deletions

File tree

src/Value/Size.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Sabberworm\CSS\Parsing\ParserState;
99
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
1010
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
11+
use Sabberworm\CSS\ShortClassNameProvider;
1112

1213
use function Safe\preg_match;
1314
use function Safe\preg_replace;
@@ -17,6 +18,8 @@
1718
*/
1819
class Size extends PrimitiveValue
1920
{
21+
use ShortClassNameProvider;
22+
2023
/**
2124
* vh/vw/vm(ax)/vmin/rem are absolute insofar as they don’t scale to the immediate parent (only the viewport)
2225
*/
@@ -202,4 +205,19 @@ public function render(OutputFormat $outputFormat): string
202205

203206
return preg_replace(["/$decimalPoint/", '/^(-?)0\\./'], ['.', '$1.'], $size) . ($this->unit ?? '');
204207
}
208+
209+
/**
210+
* @return array<string, bool|int|float|string|list<array<string, mixed>>>
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+
}
205223
}

tests/Unit/Value/SizeTest.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,39 @@ public function parsesUnit(string $unit): void
102102
/**
103103
* @test
104104
*/
105-
public function getArrayRepresentationThrowsException(): void
105+
public function getArrayRepresentationIncludesClassName(): void
106106
{
107-
$this->expectException(\BadMethodCallException::class);
107+
$subject = new Size(1);
108+
109+
$result = $subject->getArrayRepresentation();
108110

111+
self::assertArrayHasKey('class', $result);
112+
self::assertSame('Size', $result['class']);
113+
}
114+
115+
/**
116+
* @test
117+
*/
118+
public function getArrayRepresentationIncludesNumber(): void
119+
{
109120
$subject = new Size(1);
110121

111-
$subject->getArrayRepresentation();
122+
$result = $subject->getArrayRepresentation();
123+
124+
self::assertArrayHasKey('number', $result);
125+
self::assertSame(1.0, $result['number']);
126+
}
127+
128+
/**
129+
* @test
130+
*/
131+
public function getArrayRepresentationIncludesUnit(): void
132+
{
133+
$subject = new Size(1, 'px');
134+
135+
$result = $subject->getArrayRepresentation();
136+
137+
self::assertArrayHasKey('unit', $result);
138+
self::assertSame('px', $result['unit']);
112139
}
113140
}

0 commit comments

Comments
 (0)