Skip to content

Commit c169f93

Browse files
committed
[TASK] Implement RuleSet::getArrayRepresentation()
Part of #1440.
1 parent 5c5b2b0 commit c169f93

2 files changed

Lines changed: 63 additions & 4 deletions

File tree

src/RuleSet/RuleSet.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Sabberworm\CSS\Position\Position;
1515
use Sabberworm\CSS\Position\Positionable;
1616
use Sabberworm\CSS\Property\Declaration;
17+
use Sabberworm\CSS\ShortClassNameProvider;
1718

1819
/**
1920
* This class is a container for individual `Declaration`s.
@@ -31,6 +32,7 @@ class RuleSet implements CSSElement, CSSListItem, Positionable, DeclarationList
3132
use CommentContainer;
3233
use LegacyDeclarationListMethods;
3334
use Position;
35+
use ShortClassNameProvider;
3436

3537
/**
3638
* the declarations in this rule set, using the property name as the key,
@@ -345,7 +347,20 @@ static function () use ($declaration, $nextLevelFormat): string {
345347
*/
346348
public function getArrayRepresentation(): array
347349
{
348-
throw new \BadMethodCallException('`getArrayRepresentation` is not yet implemented for `' . self::class . '`');
350+
$declarationsArrayRepresentation = [];
351+
foreach ($this->declarations as $propertyName => $declarationForOneProperty) {
352+
$declarationsArrayRepresentation[$propertyName] = \array_map(
353+
function (Declaration $declaration): array {
354+
return $declaration->getArrayRepresentation();
355+
},
356+
$declarationForOneProperty
357+
);
358+
}
359+
360+
return [
361+
'class' => $this->getShortClassName(),
362+
'declarations' => $declarationsArrayRepresentation,
363+
];
349364
}
350365

351366
/**

tests/Unit/RuleSet/RuleSetTest.php

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPUnit\Framework\TestCase;
88
use Sabberworm\CSS\CSSElement;
99
use Sabberworm\CSS\CSSList\CSSListItem;
10+
use Sabberworm\CSS\Property\Declaration;
1011
use Sabberworm\CSS\RuleSet\RuleSet;
1112

1213
/**
@@ -83,10 +84,53 @@ public function getLineNumberReturnsLineNumberPassedToConstructor(?int $lineNumb
8384
/**
8485
* @test
8586
*/
86-
public function getArrayRepresentationThrowsException(): void
87+
public function getArrayRepresentationIncludesClassName(): void
8788
{
88-
$this->expectException(\BadMethodCallException::class);
89+
$subject = new RuleSet();
8990

90-
$this->subject->getArrayRepresentation();
91+
$result = $subject->getArrayRepresentation();
92+
93+
self::assertSame('RuleSet', $result['class']);
94+
}
95+
96+
/**
97+
* @test
98+
*/
99+
public function getArrayRepresentationIncludesDeclarations(): void
100+
{
101+
$subject = new RuleSet();
102+
$subject->addDeclaration(new Declaration('line-height'));
103+
$subject->addDeclaration(new Declaration('line-height'));
104+
$subject->addDeclaration(new Declaration('color'));
105+
106+
$result = $subject->getArrayRepresentation();
107+
108+
self::assertSame(
109+
[
110+
'line-height' => [
111+
[
112+
'class' => 'Declaration',
113+
'propertyName' => 'line-height',
114+
'propertyValue' => null,
115+
'important' => false,
116+
],
117+
[
118+
'class' => 'Declaration',
119+
'propertyName' => 'line-height',
120+
'propertyValue' => null,
121+
'important' => false,
122+
],
123+
],
124+
'color' => [
125+
[
126+
'class' => 'Declaration',
127+
'propertyName' => 'color',
128+
'propertyValue' => null,
129+
'important' => false,
130+
],
131+
],
132+
],
133+
$result['declarations']
134+
);
91135
}
92136
}

0 commit comments

Comments
 (0)