Skip to content

Commit 8ad8565

Browse files
authored
[TASK] Implement (At)RuleSet::getArrayRepresentation() (#1580)
Part of #1440.
1 parent dcc0d20 commit 8ad8565

4 files changed

Lines changed: 143 additions & 7 deletions

File tree

src/RuleSet/AtRuleSet.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,18 @@ public function render(OutputFormat $outputFormat): string
6565
$result .= '}';
6666
return $result;
6767
}
68+
69+
/**
70+
* @return array<string, bool|int|float|string|array<mixed>|null>
71+
*
72+
* @internal
73+
*/
74+
public function getArrayRepresentation(): array
75+
{
76+
$arrayRepresentation = parent::getArrayRepresentation();
77+
$arrayRepresentation['atRuleName'] = $this->type;
78+
$arrayRepresentation['arguments'] = $this->arguments;
79+
80+
return $arrayRepresentation;
81+
}
6882
}

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,
@@ -344,7 +346,20 @@ static function () use ($declaration, $nextLevelFormat): string {
344346
*/
345347
public function getArrayRepresentation(): array
346348
{
347-
throw new \BadMethodCallException('`getArrayRepresentation` is not yet implemented for `' . self::class . '`');
349+
$declarationsArrayRepresentation = [];
350+
foreach ($this->declarations as $propertyName => $declarationsForOneProperty) {
351+
$declarationsArrayRepresentation[$propertyName] = \array_map(
352+
function (Declaration $declaration): array {
353+
return $declaration->getArrayRepresentation();
354+
},
355+
$declarationsForOneProperty
356+
);
357+
}
358+
359+
return [
360+
'class' => $this->getShortClassName(),
361+
'declarations' => $declarationsArrayRepresentation,
362+
];
348363
}
349364

350365
/**

tests/Unit/RuleSet/AtRuleSetTest.php

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PHPUnit\Framework\TestCase;
88
use Sabberworm\CSS\CSSList\CSSListItem;
9+
use Sabberworm\CSS\Property\Declaration;
910
use Sabberworm\CSS\RuleSet\AtRuleSet;
1011

1112
/**
@@ -34,10 +35,72 @@ public function implementsCSSListItem(): void
3435
/**
3536
* @test
3637
*/
37-
public function getArrayRepresentationThrowsException(): void
38+
public function getArrayRepresentationIncludesClassName(): void
3839
{
39-
$this->expectException(\BadMethodCallException::class);
40+
$subject = new AtRuleSet('supports');
4041

41-
$this->subject->getArrayRepresentation();
42+
$result = $subject->getArrayRepresentation();
43+
44+
self::assertSame('AtRuleSet', $result['class']);
45+
}
46+
47+
/**
48+
* @test
49+
*/
50+
public function getArrayRepresentationIncludesDeclarations(): void
51+
{
52+
$subject = new AtRuleSet('supports');
53+
$subject->addDeclaration(new Declaration('display'));
54+
$subject->addDeclaration(new Declaration('transform-origin'));
55+
56+
$result = $subject->getArrayRepresentation();
57+
58+
self::assertSame(
59+
[
60+
'display' => [
61+
[
62+
'class' => 'Declaration',
63+
'propertyName' => 'display',
64+
'propertyValue' => null,
65+
'important' => false,
66+
],
67+
],
68+
'transform-origin' => [
69+
[
70+
'class' => 'Declaration',
71+
'propertyName' => 'transform-origin',
72+
'propertyValue' => null,
73+
'important' => false,
74+
],
75+
],
76+
],
77+
$result['declarations']
78+
);
79+
}
80+
81+
/**
82+
* @test
83+
*/
84+
public function getArrayRepresentationIncludesAtRuleName(): void
85+
{
86+
$atRuleName = 'supports';
87+
$subject = new AtRuleSet($atRuleName);
88+
89+
$result = $subject->getArrayRepresentation();
90+
91+
self::assertSame($atRuleName, $result['atRuleName']);
92+
}
93+
94+
/**
95+
* @test
96+
*/
97+
public function getArrayRepresentationIncludesArguments(): void
98+
{
99+
$arguments = 'foo';
100+
$subject = new AtRuleSet('supports', $arguments);
101+
102+
$result = $subject->getArrayRepresentation();
103+
104+
self::assertSame($arguments, $result['arguments']);
42105
}
43106
}

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)