Skip to content

Commit 0a2c875

Browse files
authored
[TASK] Implement getArrayRepresentation() for ValueList (#1455)
Note that `ValueList` is an abstract class. The implementation can be reused by the concrete subclasses. In the case of `LineName` and `RuleValueList` it can be inherited as is, because they don't have any additional properties. `CSSFunction` will need to add the `name` property. For now, exception-throwing stubs are added to the subclasses, pending implementation on a case-by-case basis. Part of #1440.
1 parent 7566715 commit 0a2c875

6 files changed

Lines changed: 165 additions & 0 deletions

File tree

src/Value/CSSFunction.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,14 @@ public function render(OutputFormat $outputFormat): string
113113
$arguments = parent::render($outputFormat);
114114
return "{$this->name}({$arguments})";
115115
}
116+
117+
/**
118+
* @return array<string, bool|int|float|string|list<array<string, mixed>>>
119+
*
120+
* @internal
121+
*/
122+
public function getArrayRepresentation(): array
123+
{
124+
throw new \BadMethodCallException('`getArrayRepresentation` is not yet implemented for `' . self::class . '`');
125+
}
116126
}

src/Value/LineName.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,14 @@ public function render(OutputFormat $outputFormat): string
5656
{
5757
return '[' . parent::render(OutputFormat::createCompact()) . ']';
5858
}
59+
60+
/**
61+
* @return array<string, bool|int|float|string|list<array<string, mixed>>>
62+
*
63+
* @internal
64+
*/
65+
public function getArrayRepresentation(): array
66+
{
67+
throw new \BadMethodCallException('`getArrayRepresentation` is not yet implemented for `' . self::class . '`');
68+
}
5969
}

src/Value/RuleValueList.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,14 @@ public function __construct(string $separator = ',', ?int $lineNumber = null)
1919
{
2020
parent::__construct([], $separator, $lineNumber);
2121
}
22+
23+
/**
24+
* @return array<string, bool|int|float|string|list<array<string, mixed>>>
25+
*
26+
* @internal
27+
*/
28+
public function getArrayRepresentation(): array
29+
{
30+
throw new \BadMethodCallException('`getArrayRepresentation` is not yet implemented for `' . self::class . '`');
31+
}
2232
}

src/Value/ValueList.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Sabberworm\CSS\Value;
66

77
use Sabberworm\CSS\OutputFormat;
8+
use Sabberworm\CSS\ShortClassNameProvider;
89

910
/**
1011
* A `ValueList` represents a lists of `Value`s, separated by some separation character
@@ -14,6 +15,8 @@
1415
*/
1516
abstract class ValueList extends Value
1617
{
18+
use ShortClassNameProvider;
19+
1720
/**
1821
* @var array<Value|string>
1922
*
@@ -93,4 +96,29 @@ public function render(OutputFormat $outputFormat): string
9396
$this->components
9497
);
9598
}
99+
100+
/**
101+
* @return array<string, bool|int|float|string|list<array<string, mixed>>>
102+
*
103+
* @internal
104+
*/
105+
public function getArrayRepresentation(): array
106+
{
107+
return [
108+
'class' => $this->getShortClassName(),
109+
'components' => \array_map(
110+
/**
111+
* @parm Value|string $component
112+
*/
113+
function ($component): array {
114+
if (\is_string($component)) {
115+
return ['class' => 'string', 'value' => $component];
116+
}
117+
return $component->getArrayRepresentation();
118+
},
119+
$this->components
120+
),
121+
'separator' => $this->separator,
122+
];
123+
}
96124
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sabberworm\CSS\Tests\Unit\Value\Fixtures;
6+
7+
use Sabberworm\CSS\Value\ValueList;
8+
9+
final class ConcreteValueList extends ValueList {}

tests/Unit/Value/ValueListTest.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sabberworm\CSS\Tests\Unit\Value;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Sabberworm\CSS\Tests\Unit\Value\Fixtures\ConcreteValueList;
9+
use Sabberworm\CSS\Value\Size;
10+
11+
/**
12+
* @covers \Sabberworm\CSS\Value\ValueList
13+
*/
14+
final class ValueListTest extends TestCase
15+
{
16+
/**
17+
* @test
18+
*/
19+
public function getArrayRepresentationIncludesClassName(): void
20+
{
21+
$subject = new ConcreteValueList();
22+
23+
$result = $subject->getArrayRepresentation();
24+
25+
self::assertArrayHasKey('class', $result);
26+
self::assertSame('ConcreteValueList', $result['class']);
27+
}
28+
29+
/**
30+
* @test
31+
*/
32+
public function getArrayRepresentationIncludesStringComponent(): void
33+
{
34+
$subject = new ConcreteValueList(['Helvetica']);
35+
36+
$result = $subject->getArrayRepresentation();
37+
38+
self::assertArrayHasKey('components', $result);
39+
self::assertIsArray($result['components']);
40+
self::assertArrayHasKey(0, $result['components']);
41+
self::assertArrayHasKey('value', $result['components'][0]);
42+
self::assertSame('Helvetica', $result['components'][0]['value']);
43+
}
44+
45+
/**
46+
* @test
47+
*/
48+
public function getArrayRepresentationIncludesValueComponent(): void
49+
{
50+
$subject = new ConcreteValueList([new Size(1)]);
51+
52+
$result = $subject->getArrayRepresentation();
53+
54+
self::assertArrayHasKey('components', $result);
55+
self::assertIsArray($result['components']);
56+
self::assertArrayHasKey(0, $result['components']);
57+
self::assertArrayHasKey('class', $result['components'][0]);
58+
self::assertSame('Size', $result['components'][0]['class']);
59+
}
60+
61+
/**
62+
* @test
63+
*/
64+
public function getArrayRepresentationIncludesMultipleMixedComponents(): void
65+
{
66+
$subject = new ConcreteValueList([new Size(1), '+', new Size(2)]);
67+
68+
$result = $subject->getArrayRepresentation();
69+
70+
self::assertArrayHasKey('components', $result);
71+
self::assertIsArray($result['components']);
72+
73+
self::assertArrayHasKey(0, $result['components']);
74+
self::assertArrayHasKey('class', $result['components'][0]);
75+
self::assertSame('Size', $result['components'][0]['class']);
76+
77+
self::assertArrayHasKey(1, $result['components']);
78+
self::assertArrayHasKey('value', $result['components'][1]);
79+
self::assertSame('+', $result['components'][1]['value']);
80+
81+
self::assertArrayHasKey(2, $result['components']);
82+
self::assertArrayHasKey('class', $result['components'][2]);
83+
self::assertSame('Size', $result['components'][2]['class']);
84+
}
85+
86+
/**
87+
* @test
88+
*/
89+
public function getArrayRepresentationIncludesSeparator(): void
90+
{
91+
$subject = new ConcreteValueList();
92+
93+
$result = $subject->getArrayRepresentation();
94+
95+
self::assertArrayHasKey('separator', $result);
96+
self::assertSame(',', $result['separator']);
97+
}
98+
}

0 commit comments

Comments
 (0)