From ab925f866ad805f9e3c0b05fcbe1852cea5cff44 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Sat, 11 Apr 2026 13:28:15 +0200 Subject: [PATCH] [TASK] Implement `Declaration::getArrayRepresentation()` Part of #1440. --- src/Property/Declaration.php | 11 ++++- tests/Unit/Property/DeclarationTest.php | 61 +++++++++++++++++++++++-- 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/src/Property/Declaration.php b/src/Property/Declaration.php index 079ab48d..1c980a67 100644 --- a/src/Property/Declaration.php +++ b/src/Property/Declaration.php @@ -14,6 +14,7 @@ use Sabberworm\CSS\Parsing\UnexpectedTokenException; use Sabberworm\CSS\Position\Position; use Sabberworm\CSS\Position\Positionable; +use Sabberworm\CSS\ShortClassNameProvider; use Sabberworm\CSS\Value\RuleValueList; use Sabberworm\CSS\Value\Value; @@ -28,6 +29,7 @@ class Declaration implements Commentable, CSSElement, Positionable { use CommentContainer; use Position; + use ShortClassNameProvider; /** * @var non-empty-string @@ -226,6 +228,13 @@ public function render(OutputFormat $outputFormat): string */ public function getArrayRepresentation(): array { - throw new \BadMethodCallException('`getArrayRepresentation` is not yet implemented for `' . self::class . '`'); + return [ + 'class' => $this->getShortClassName(), + 'propertyName' => $this->propertyName, + // We're using the term "property value" here to match the wording used in the specs: + // https://www.w3.org/TR/CSS22/syndata.html#declaration + 'propertyValue' => $this->value, + 'important' => $this->isImportant, + ]; } } diff --git a/tests/Unit/Property/DeclarationTest.php b/tests/Unit/Property/DeclarationTest.php index 8344bb87..8fa0f6a6 100644 --- a/tests/Unit/Property/DeclarationTest.php +++ b/tests/Unit/Property/DeclarationTest.php @@ -74,12 +74,65 @@ static function ($component): string { /** * @test */ - public function getArrayRepresentationThrowsException(): void + public function getArrayRepresentationIncludesClassName(): void { - $this->expectException(\BadMethodCallException::class); + $subject = new Declaration('line-height'); - $subject = new Declaration('todo'); + $result = $subject->getArrayRepresentation(); - $subject->getArrayRepresentation(); + self::assertSame('Declaration', $result['class']); + } + + /** + * @test + */ + public function getArrayRepresentationIncludesPropertyName(): void + { + $propertyName = 'font-weight'; + $subject = new Declaration($propertyName); + + $result = $subject->getArrayRepresentation(); + + self::assertSame($propertyName, $result['propertyName']); + } + + /** + * @test + */ + public function getArrayRepresentationIncludesPropertyValue(): void + { + $subject = new Declaration('font-weight'); + $propertyValue = 'bold'; + $subject->setValue($propertyValue); + + $result = $subject->getArrayRepresentation(); + + self::assertSame($propertyValue, $result['propertyValue']); + } + + /** + * @return array + */ + public static function provideBooleans(): array + { + return [ + 'true' => [true], + 'false' => [false], + ]; + } + + /** + * @test + * + * @dataProvider provideBooleans + */ + public function getArrayRepresentationIncludesImportantFlag(bool $important): void + { + $subject = new Declaration('font-weight'); + $subject->setIsImportant($important); + + $result = $subject->getArrayRepresentation(); + + self::assertSame($important, $result['important']); } }