From b3aba8f9fdce88f0892ae56e93f822a0b77463e7 Mon Sep 17 00:00:00 2001 From: Jake Hotson Date: Sun, 4 Jan 2026 00:15:46 +0000 Subject: [PATCH] [TASK] Implement `getArrayRepresentation()` for `ValueList` Note that `ValueList` is an abstract class. The implementation can be reused by the concrete subclasses. In the case of `LineName` and `RuleValueList` it can be inherited as is, because they don't have any additional properties. `CSSFunction` will need to add the `name` property. For now, exception-throwing stubs are added to the subclasses, pending implementation on a case-by-case basis. Part of #1440. --- src/Value/CSSFunction.php | 10 ++ src/Value/LineName.php | 10 ++ src/Value/RuleValueList.php | 10 ++ src/Value/ValueList.php | 28 ++++++ .../Unit/Value/Fixtures/ConcreteValueList.php | 9 ++ tests/Unit/Value/ValueListTest.php | 98 +++++++++++++++++++ 6 files changed, 165 insertions(+) create mode 100644 tests/Unit/Value/Fixtures/ConcreteValueList.php create mode 100644 tests/Unit/Value/ValueListTest.php diff --git a/src/Value/CSSFunction.php b/src/Value/CSSFunction.php index 86b56d9b1..a8b8ead73 100644 --- a/src/Value/CSSFunction.php +++ b/src/Value/CSSFunction.php @@ -113,4 +113,14 @@ public function render(OutputFormat $outputFormat): string $arguments = parent::render($outputFormat); return "{$this->name}({$arguments})"; } + + /** + * @return array>> + * + * @internal + */ + public function getArrayRepresentation(): array + { + throw new \BadMethodCallException('`getArrayRepresentation` is not yet implemented for `' . self::class . '`'); + } } diff --git a/src/Value/LineName.php b/src/Value/LineName.php index 763cc48ea..d16c60dbf 100644 --- a/src/Value/LineName.php +++ b/src/Value/LineName.php @@ -56,4 +56,14 @@ public function render(OutputFormat $outputFormat): string { return '[' . parent::render(OutputFormat::createCompact()) . ']'; } + + /** + * @return array>> + * + * @internal + */ + public function getArrayRepresentation(): array + { + throw new \BadMethodCallException('`getArrayRepresentation` is not yet implemented for `' . self::class . '`'); + } } diff --git a/src/Value/RuleValueList.php b/src/Value/RuleValueList.php index 37aa7cd66..604e13003 100644 --- a/src/Value/RuleValueList.php +++ b/src/Value/RuleValueList.php @@ -19,4 +19,14 @@ public function __construct(string $separator = ',', ?int $lineNumber = null) { parent::__construct([], $separator, $lineNumber); } + + /** + * @return array>> + * + * @internal + */ + public function getArrayRepresentation(): array + { + throw new \BadMethodCallException('`getArrayRepresentation` is not yet implemented for `' . self::class . '`'); + } } diff --git a/src/Value/ValueList.php b/src/Value/ValueList.php index 6f85f895e..b39449cfa 100644 --- a/src/Value/ValueList.php +++ b/src/Value/ValueList.php @@ -5,6 +5,7 @@ namespace Sabberworm\CSS\Value; use Sabberworm\CSS\OutputFormat; +use Sabberworm\CSS\ShortClassNameProvider; /** * A `ValueList` represents a lists of `Value`s, separated by some separation character @@ -14,6 +15,8 @@ */ abstract class ValueList extends Value { + use ShortClassNameProvider; + /** * @var array * @@ -93,4 +96,29 @@ public function render(OutputFormat $outputFormat): string $this->components ); } + + /** + * @return array>> + * + * @internal + */ + public function getArrayRepresentation(): array + { + return [ + 'class' => $this->getShortClassName(), + 'components' => \array_map( + /** + * @parm Value|string $component + */ + function ($component): array { + if (\is_string($component)) { + return ['class' => 'string', 'value' => $component]; + } + return $component->getArrayRepresentation(); + }, + $this->components + ), + 'separator' => $this->separator, + ]; + } } diff --git a/tests/Unit/Value/Fixtures/ConcreteValueList.php b/tests/Unit/Value/Fixtures/ConcreteValueList.php new file mode 100644 index 000000000..a9659d30c --- /dev/null +++ b/tests/Unit/Value/Fixtures/ConcreteValueList.php @@ -0,0 +1,9 @@ +getArrayRepresentation(); + + self::assertArrayHasKey('class', $result); + self::assertSame('ConcreteValueList', $result['class']); + } + + /** + * @test + */ + public function getArrayRepresentationIncludesStringComponent(): void + { + $subject = new ConcreteValueList(['Helvetica']); + + $result = $subject->getArrayRepresentation(); + + self::assertArrayHasKey('components', $result); + self::assertIsArray($result['components']); + self::assertArrayHasKey(0, $result['components']); + self::assertArrayHasKey('value', $result['components'][0]); + self::assertSame('Helvetica', $result['components'][0]['value']); + } + + /** + * @test + */ + public function getArrayRepresentationIncludesValueComponent(): void + { + $subject = new ConcreteValueList([new Size(1)]); + + $result = $subject->getArrayRepresentation(); + + self::assertArrayHasKey('components', $result); + self::assertIsArray($result['components']); + self::assertArrayHasKey(0, $result['components']); + self::assertArrayHasKey('class', $result['components'][0]); + self::assertSame('Size', $result['components'][0]['class']); + } + + /** + * @test + */ + public function getArrayRepresentationIncludesMultipleMixedComponents(): void + { + $subject = new ConcreteValueList([new Size(1), '+', new Size(2)]); + + $result = $subject->getArrayRepresentation(); + + self::assertArrayHasKey('components', $result); + self::assertIsArray($result['components']); + + self::assertArrayHasKey(0, $result['components']); + self::assertArrayHasKey('class', $result['components'][0]); + self::assertSame('Size', $result['components'][0]['class']); + + self::assertArrayHasKey(1, $result['components']); + self::assertArrayHasKey('value', $result['components'][1]); + self::assertSame('+', $result['components'][1]['value']); + + self::assertArrayHasKey(2, $result['components']); + self::assertArrayHasKey('class', $result['components'][2]); + self::assertSame('Size', $result['components'][2]['class']); + } + + /** + * @test + */ + public function getArrayRepresentationIncludesSeparator(): void + { + $subject = new ConcreteValueList(); + + $result = $subject->getArrayRepresentation(); + + self::assertArrayHasKey('separator', $result); + self::assertSame(',', $result['separator']); + } +}