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
10 changes: 10 additions & 0 deletions src/Value/CSSFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,14 @@ public function render(OutputFormat $outputFormat): string
$arguments = parent::render($outputFormat);
return "{$this->name}({$arguments})";
}

/**
* @return array<string, bool|int|float|string|list<array<string, mixed>>>
*
* @internal
*/
public function getArrayRepresentation(): array
{
throw new \BadMethodCallException('`getArrayRepresentation` is not yet implemented for `' . self::class . '`');
}
}
10 changes: 10 additions & 0 deletions src/Value/LineName.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,14 @@ public function render(OutputFormat $outputFormat): string
{
return '[' . parent::render(OutputFormat::createCompact()) . ']';
}

/**
* @return array<string, bool|int|float|string|list<array<string, mixed>>>
*
* @internal
*/
public function getArrayRepresentation(): array
{
throw new \BadMethodCallException('`getArrayRepresentation` is not yet implemented for `' . self::class . '`');
}
}
10 changes: 10 additions & 0 deletions src/Value/RuleValueList.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,14 @@ public function __construct(string $separator = ',', ?int $lineNumber = null)
{
parent::__construct([], $separator, $lineNumber);
}

/**
* @return array<string, bool|int|float|string|list<array<string, mixed>>>
*
* @internal
*/
public function getArrayRepresentation(): array
{
throw new \BadMethodCallException('`getArrayRepresentation` is not yet implemented for `' . self::class . '`');
}
}
28 changes: 28 additions & 0 deletions src/Value/ValueList.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Sabberworm\CSS\Value;

use Sabberworm\CSS\OutputFormat;
use Sabberworm\CSS\ShortClassNameProvider;

/**
* A `ValueList` represents a lists of `Value`s, separated by some separation character
Expand All @@ -14,6 +15,8 @@
*/
abstract class ValueList extends Value
{
use ShortClassNameProvider;

/**
* @var array<Value|string>
*
Expand Down Expand Up @@ -93,4 +96,29 @@ public function render(OutputFormat $outputFormat): string
$this->components
);
}

/**
* @return array<string, bool|int|float|string|list<array<string, mixed>>>
*
* @internal
*/
public function getArrayRepresentation(): array
{
return [
'class' => $this->getShortClassName(),
'components' => \array_map(
/**
* @parm Value|string $component
*/
function ($component): array {
if (\is_string($component)) {
return ['class' => 'string', 'value' => $component];
}
return $component->getArrayRepresentation();
},
$this->components
),
'separator' => $this->separator,
];
}
}
9 changes: 9 additions & 0 deletions tests/Unit/Value/Fixtures/ConcreteValueList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Sabberworm\CSS\Tests\Unit\Value\Fixtures;

use Sabberworm\CSS\Value\ValueList;

final class ConcreteValueList extends ValueList {}
98 changes: 98 additions & 0 deletions tests/Unit/Value/ValueListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);

namespace Sabberworm\CSS\Tests\Unit\Value;

use PHPUnit\Framework\TestCase;
use Sabberworm\CSS\Tests\Unit\Value\Fixtures\ConcreteValueList;
use Sabberworm\CSS\Value\Size;

/**
* @covers \Sabberworm\CSS\Value\ValueList
*/
final class ValueListTest extends TestCase
{
/**
* @test
*/
public function getArrayRepresentationIncludesClassName(): void
{
$subject = new ConcreteValueList();

$result = $subject->getArrayRepresentation();

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

/**
* @test
*/
public function getArrayRepresentationIncludesStringComponent(): void
{
$subject = new ConcreteValueList(['Helvetica']);

$result = $subject->getArrayRepresentation();

self::assertArrayHasKey('components', $result);
self::assertIsArray($result['components']);
self::assertArrayHasKey(0, $result['components']);
self::assertArrayHasKey('value', $result['components'][0]);
self::assertSame('Helvetica', $result['components'][0]['value']);
}

/**
* @test
*/
public function getArrayRepresentationIncludesValueComponent(): void
{
$subject = new ConcreteValueList([new Size(1)]);

$result = $subject->getArrayRepresentation();

self::assertArrayHasKey('components', $result);
self::assertIsArray($result['components']);
self::assertArrayHasKey(0, $result['components']);
self::assertArrayHasKey('class', $result['components'][0]);
self::assertSame('Size', $result['components'][0]['class']);
}

/**
* @test
*/
public function getArrayRepresentationIncludesMultipleMixedComponents(): void
{
$subject = new ConcreteValueList([new Size(1), '+', new Size(2)]);

$result = $subject->getArrayRepresentation();

self::assertArrayHasKey('components', $result);
self::assertIsArray($result['components']);

self::assertArrayHasKey(0, $result['components']);
self::assertArrayHasKey('class', $result['components'][0]);
self::assertSame('Size', $result['components'][0]['class']);

self::assertArrayHasKey(1, $result['components']);
self::assertArrayHasKey('value', $result['components'][1]);
self::assertSame('+', $result['components'][1]['value']);

self::assertArrayHasKey(2, $result['components']);
self::assertArrayHasKey('class', $result['components'][2]);
self::assertSame('Size', $result['components'][2]['class']);
}

/**
* @test
*/
public function getArrayRepresentationIncludesSeparator(): void
{
$subject = new ConcreteValueList();

$result = $subject->getArrayRepresentation();

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