Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/RuleSet/AtRuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,18 @@ public function render(OutputFormat $outputFormat): string
$result .= '}';
return $result;
}

/**
* @return array<string, bool|int|float|string|array<mixed>|null>
*
* @internal
*/
public function getArrayRepresentation(): array
{
$arrayRepresentation = parent::getArrayRepresentation();
$arrayRepresentation['atRuleName'] = $this->type;
$arrayRepresentation['arguments'] = $this->arguments;

return $arrayRepresentation;
}
}
17 changes: 16 additions & 1 deletion src/RuleSet/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Sabberworm\CSS\Position\Position;
use Sabberworm\CSS\Position\Positionable;
use Sabberworm\CSS\Property\Declaration;
use Sabberworm\CSS\ShortClassNameProvider;

/**
* This class is a container for individual `Declaration`s.
Expand All @@ -31,6 +32,7 @@ class RuleSet implements CSSElement, CSSListItem, Positionable, DeclarationList
use CommentContainer;
use LegacyDeclarationListMethods;
use Position;
use ShortClassNameProvider;

/**
* the declarations in this rule set, using the property name as the key,
Expand Down Expand Up @@ -344,7 +346,20 @@ static function () use ($declaration, $nextLevelFormat): string {
*/
public function getArrayRepresentation(): array
{
throw new \BadMethodCallException('`getArrayRepresentation` is not yet implemented for `' . self::class . '`');
$declarationsArrayRepresentation = [];
foreach ($this->declarations as $propertyName => $declarationsForOneProperty) {
$declarationsArrayRepresentation[$propertyName] = \array_map(
function (Declaration $declaration): array {
return $declaration->getArrayRepresentation();
},
$declarationsForOneProperty
);
}

return [
'class' => $this->getShortClassName(),
'declarations' => $declarationsArrayRepresentation,
];
}

/**
Expand Down
69 changes: 66 additions & 3 deletions tests/Unit/RuleSet/AtRuleSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PHPUnit\Framework\TestCase;
use Sabberworm\CSS\CSSList\CSSListItem;
use Sabberworm\CSS\Property\Declaration;
use Sabberworm\CSS\RuleSet\AtRuleSet;

/**
Expand Down Expand Up @@ -34,10 +35,72 @@ public function implementsCSSListItem(): void
/**
* @test
*/
public function getArrayRepresentationThrowsException(): void
public function getArrayRepresentationIncludesClassName(): void
{
$this->expectException(\BadMethodCallException::class);
$subject = new AtRuleSet('supports');

$this->subject->getArrayRepresentation();
$result = $subject->getArrayRepresentation();

self::assertSame('AtRuleSet', $result['class']);
}

/**
* @test
*/
public function getArrayRepresentationIncludesDeclarations(): void
{
$subject = new AtRuleSet('supports');
$subject->addDeclaration(new Declaration('display'));
$subject->addDeclaration(new Declaration('transform-origin'));

$result = $subject->getArrayRepresentation();

self::assertSame(
[
'display' => [
[
'class' => 'Declaration',
'propertyName' => 'display',
'propertyValue' => null,
'important' => false,
],
],
'transform-origin' => [
[
'class' => 'Declaration',
'propertyName' => 'transform-origin',
'propertyValue' => null,
'important' => false,
],
],
],
$result['declarations']
);
}

/**
* @test
*/
public function getArrayRepresentationIncludesAtRuleName(): void
{
$atRuleName = 'supports';
$subject = new AtRuleSet($atRuleName);

$result = $subject->getArrayRepresentation();

self::assertSame($atRuleName, $result['atRuleName']);
}

/**
* @test
*/
public function getArrayRepresentationIncludesArguments(): void
{
$arguments = 'foo';
$subject = new AtRuleSet('supports', $arguments);

$result = $subject->getArrayRepresentation();

self::assertSame($arguments, $result['arguments']);
}
}
50 changes: 47 additions & 3 deletions tests/Unit/RuleSet/RuleSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPUnit\Framework\TestCase;
use Sabberworm\CSS\CSSElement;
use Sabberworm\CSS\CSSList\CSSListItem;
use Sabberworm\CSS\Property\Declaration;
use Sabberworm\CSS\RuleSet\RuleSet;

/**
Expand Down Expand Up @@ -83,10 +84,53 @@ public function getLineNumberReturnsLineNumberPassedToConstructor(?int $lineNumb
/**
* @test
*/
public function getArrayRepresentationThrowsException(): void
public function getArrayRepresentationIncludesClassName(): void
{
$this->expectException(\BadMethodCallException::class);
$subject = new RuleSet();

$this->subject->getArrayRepresentation();
$result = $subject->getArrayRepresentation();

self::assertSame('RuleSet', $result['class']);
}

/**
* @test
*/
public function getArrayRepresentationIncludesDeclarations(): void
{
$subject = new RuleSet();
$subject->addDeclaration(new Declaration('line-height'));
$subject->addDeclaration(new Declaration('line-height'));
$subject->addDeclaration(new Declaration('color'));

$result = $subject->getArrayRepresentation();

self::assertSame(
[
'line-height' => [
[
'class' => 'Declaration',
'propertyName' => 'line-height',
'propertyValue' => null,
'important' => false,
],
[
'class' => 'Declaration',
'propertyName' => 'line-height',
'propertyValue' => null,
'important' => false,
],
],
'color' => [
[
'class' => 'Declaration',
'propertyName' => 'color',
'propertyValue' => null,
'important' => false,
],
],
],
$result['declarations']
);
}
}