Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Value/Size.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Sabberworm\CSS\Parsing\ParserState;
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
use Sabberworm\CSS\ShortClassNameProvider;

use function Safe\preg_match;
use function Safe\preg_replace;
Expand All @@ -17,6 +18,8 @@
*/
class Size extends PrimitiveValue
{
use ShortClassNameProvider;

/**
* vh/vw/vm(ax)/vmin/rem are absolute insofar as they don’t scale to the immediate parent (only the viewport)
*/
Expand Down Expand Up @@ -202,4 +205,19 @@ public function render(OutputFormat $outputFormat): string

return preg_replace(["/$decimalPoint/", '/^(-?)0\\./'], ['.', '$1.'], $size) . ($this->unit ?? '');
}

/**
* @return array<string, bool|int|float|string|list<array<string, mixed>>>
*
* @internal
*/
public function getArrayRepresentation(): array
{
return [
'class' => $this->getShortClassName(),
// 'number' is the official W3C terminology (not 'size')
'number' => $this->size,
'unit' => $this->unit,
];
}
}
33 changes: 30 additions & 3 deletions tests/Unit/Value/SizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,39 @@ public function parsesUnit(string $unit): void
/**
* @test
*/
public function getArrayRepresentationThrowsException(): void
public function getArrayRepresentationIncludesClassName(): void
{
$this->expectException(\BadMethodCallException::class);
$subject = new Size(1);

$result = $subject->getArrayRepresentation();

self::assertArrayHasKey('class', $result);
self::assertSame('Size', $result['class']);
}

/**
* @test
*/
public function getArrayRepresentationIncludesNumber(): void
{
$subject = new Size(1);

$subject->getArrayRepresentation();
$result = $subject->getArrayRepresentation();

self::assertArrayHasKey('number', $result);
self::assertSame(1.0, $result['number']);
}

/**
* @test
*/
public function getArrayRepresentationIncludesUnit(): void
{
$subject = new Size(1, 'px');

$result = $subject->getArrayRepresentation();

self::assertArrayHasKey('unit', $result);
self::assertSame('px', $result['unit']);
}
}