Skip to content

Commit d86b160

Browse files
authored
[TASK] Implement Declaration::getArrayRepresentation() (#1564)
Part of #1440.
1 parent 97f2d1f commit d86b160

2 files changed

Lines changed: 67 additions & 5 deletions

File tree

src/Property/Declaration.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
1515
use Sabberworm\CSS\Position\Position;
1616
use Sabberworm\CSS\Position\Positionable;
17+
use Sabberworm\CSS\ShortClassNameProvider;
1718
use Sabberworm\CSS\Value\RuleValueList;
1819
use Sabberworm\CSS\Value\Value;
1920

@@ -28,6 +29,7 @@ class Declaration implements Commentable, CSSElement, Positionable
2829
{
2930
use CommentContainer;
3031
use Position;
32+
use ShortClassNameProvider;
3133

3234
/**
3335
* @var non-empty-string
@@ -226,6 +228,13 @@ public function render(OutputFormat $outputFormat): string
226228
*/
227229
public function getArrayRepresentation(): array
228230
{
229-
throw new \BadMethodCallException('`getArrayRepresentation` is not yet implemented for `' . self::class . '`');
231+
return [
232+
'class' => $this->getShortClassName(),
233+
'propertyName' => $this->propertyName,
234+
// We're using the term "property value" here to match the wording used in the specs:
235+
// https://www.w3.org/TR/CSS22/syndata.html#declaration
236+
'propertyValue' => $this->value,
237+
'important' => $this->isImportant,
238+
];
230239
}
231240
}

tests/Unit/Property/DeclarationTest.php

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,65 @@ static function ($component): string {
7474
/**
7575
* @test
7676
*/
77-
public function getArrayRepresentationThrowsException(): void
77+
public function getArrayRepresentationIncludesClassName(): void
7878
{
79-
$this->expectException(\BadMethodCallException::class);
79+
$subject = new Declaration('line-height');
8080

81-
$subject = new Declaration('todo');
81+
$result = $subject->getArrayRepresentation();
8282

83-
$subject->getArrayRepresentation();
83+
self::assertSame('Declaration', $result['class']);
84+
}
85+
86+
/**
87+
* @test
88+
*/
89+
public function getArrayRepresentationIncludesPropertyName(): void
90+
{
91+
$propertyName = 'font-weight';
92+
$subject = new Declaration($propertyName);
93+
94+
$result = $subject->getArrayRepresentation();
95+
96+
self::assertSame($propertyName, $result['propertyName']);
97+
}
98+
99+
/**
100+
* @test
101+
*/
102+
public function getArrayRepresentationIncludesPropertyValue(): void
103+
{
104+
$subject = new Declaration('font-weight');
105+
$propertyValue = 'bold';
106+
$subject->setValue($propertyValue);
107+
108+
$result = $subject->getArrayRepresentation();
109+
110+
self::assertSame($propertyValue, $result['propertyValue']);
111+
}
112+
113+
/**
114+
* @return array<non-empty-string, array{0: bool}>
115+
*/
116+
public static function provideBooleans(): array
117+
{
118+
return [
119+
'true' => [true],
120+
'false' => [false],
121+
];
122+
}
123+
124+
/**
125+
* @test
126+
*
127+
* @dataProvider provideBooleans
128+
*/
129+
public function getArrayRepresentationIncludesImportantFlag(bool $important): void
130+
{
131+
$subject = new Declaration('font-weight');
132+
$subject->setIsImportant($important);
133+
134+
$result = $subject->getArrayRepresentation();
135+
136+
self::assertSame($important, $result['important']);
84137
}
85138
}

0 commit comments

Comments
 (0)