Skip to content

Commit 5ca221a

Browse files
committed
[TASK] Implement getArrayRepresentation() in Value and ValueList
Also include a demo of its usage. Part of #1440.
1 parent 6604f35 commit 5ca221a

File tree

13 files changed

+142
-68
lines changed

13 files changed

+142
-68
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sabberworm\CSS;
6+
7+
/**
8+
* Provides a reusable implementation of `getArrayRepresentation()` which will populate the `class` element with the
9+
* short name of the actual class.
10+
* It is expected that this will be `use`d by (possibly-abstract) base classes, with extended classes calling on to the
11+
* parent implementation then further populating the array.
12+
* If a base class using this trait needs to further populate the array, the trait method can be `use`d `as`.
13+
*
14+
* @internal
15+
*
16+
* @phpstan-require-implements Renderable
17+
*/
18+
trait ArrayRepresentationClassProvider
19+
{
20+
/**
21+
* @return array<string, bool|int|float|string|list<array<string, mixed>>>
22+
*
23+
* @internal
24+
*/
25+
public function getArrayRepresentation(): array
26+
{
27+
$reflect = new \ReflectionClass($this);
28+
29+
return ['class' => $reflect->getShortName()];
30+
}
31+
}

src/Value/CSSString.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,14 @@ public function render(OutputFormat $outputFormat): string
9494
$string = \str_replace("\n", '\\A', $string);
9595
return $outputFormat->getStringQuotingType() . $string . $outputFormat->getStringQuotingType();
9696
}
97+
98+
/**
99+
* @return array<string, bool|int|float|string|list<array<string, mixed>>>
100+
*
101+
* @internal
102+
*/
103+
public function getArrayRepresentation(): array
104+
{
105+
throw new \BadMethodCallException('`getArrayRepresentation` is not yet implemented for `' . self::class . '`');
106+
}
97107
}

src/Value/CalcFunction.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,14 @@ public static function parse(ParserState $parserState, bool $ignoreCase = false)
9696
}
9797
return new CalcFunction($function, $list, ',', $parserState->currentLine());
9898
}
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+
throw new \BadMethodCallException('`getArrayRepresentation` is not yet implemented for `' . self::class . '`');
108+
}
99109
}

src/Value/Color.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,16 @@ public function render(OutputFormat $outputFormat): string
239239
return parent::render($outputFormat);
240240
}
241241

242+
/**
243+
* @return array<string, bool|int|float|string|list<array<string, mixed>>>
244+
*
245+
* @internal
246+
*/
247+
public function getArrayRepresentation(): array
248+
{
249+
throw new \BadMethodCallException('`getArrayRepresentation` is not yet implemented for `' . self::class . '`');
250+
}
251+
242252
private function shouldRenderAsHex(OutputFormat $outputFormat): bool
243253
{
244254
return

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/URL.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,14 @@ public function render(OutputFormat $outputFormat): string
8080
{
8181
return "url({$this->url->render($outputFormat)})";
8282
}
83+
84+
/**
85+
* @return array<string, bool|int|float|string|list<array<string, mixed>>>
86+
*
87+
* @internal
88+
*/
89+
public function getArrayRepresentation(): array
90+
{
91+
throw new \BadMethodCallException('`getArrayRepresentation` is not yet implemented for `' . self::class . '`');
92+
}
8393
}

src/Value/Value.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Sabberworm\CSS\Value;
66

7+
use Sabberworm\CSS\ArrayRepresentationClassProvider;
78
use Sabberworm\CSS\CSSElement;
89
use Sabberworm\CSS\Parsing\ParserState;
910
use Sabberworm\CSS\Parsing\SourceException;
@@ -21,6 +22,7 @@
2122
abstract class Value implements CSSElement, Positionable
2223
{
2324
use Position;
25+
use ArrayRepresentationClassProvider;
2426

2527
/**
2628
* @param int<1, max>|null $lineNumber
@@ -181,16 +183,6 @@ public static function parsePrimitiveValue(ParserState $parserState)
181183
return $value;
182184
}
183185

184-
/**
185-
* @return array<string, bool|int|float|string|list<array<string, mixed>>>
186-
*
187-
* @internal
188-
*/
189-
public function getArrayRepresentation(): array
190-
{
191-
throw new \BadMethodCallException('`getArrayRepresentation` is not yet implemented for `' . self::class . '`');
192-
}
193-
194186
/**
195187
* @throws UnexpectedEOFException
196188
* @throws UnexpectedTokenException

src/Value/ValueList.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,31 @@ public function render(OutputFormat $outputFormat): string
9393
$this->components
9494
);
9595
}
96+
97+
/**
98+
* @internal
99+
*
100+
* @return array<string, bool|int|float|string|list<array<string, mixed>>>
101+
*/
102+
public function getArrayRepresentation(): array
103+
{
104+
$result = parent::getArrayRepresentation();
105+
106+
$result['components'] = \array_map(
107+
/**
108+
* @parm Value|string $component
109+
*
110+
* @return array|string
111+
*/
112+
function ($component) {
113+
if (\is_string($component)) {
114+
return $component;
115+
}
116+
return $component->getArrayRepresentation();
117+
},
118+
$this->components
119+
);
120+
121+
return $result;
122+
}
96123
}

tests/Unit/Value/CSSFunctionTest.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,10 @@
55
namespace Sabberworm\CSS\Tests\Unit\Value;
66

77
use PHPUnit\Framework\TestCase;
8-
use Sabberworm\CSS\Value\CSSFunction;
98

109
/**
1110
* @covers \Sabberworm\CSS\Value\CSSFunction
1211
* @covers \Sabberworm\CSS\Value\Value
1312
* @covers \Sabberworm\CSS\Value\ValueList
1413
*/
15-
final class CSSFunctionTest extends TestCase
16-
{
17-
/**
18-
* @test
19-
*/
20-
public function getArrayRepresentationThrowsException(): void
21-
{
22-
$this->expectException(\BadMethodCallException::class);
23-
24-
$subject = new CSSFunction('filter', []);
25-
26-
$subject->getArrayRepresentation();
27-
}
28-
}
14+
final class CSSFunctionTest extends TestCase {}

tests/Unit/Value/CalcRuleValueListTest.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,4 @@ public function separatorAlwaysIsComma(): void
5656

5757
self::assertSame(',', $subject->getListSeparator());
5858
}
59-
60-
/**
61-
* @test
62-
*/
63-
public function getArrayRepresentationThrowsException(): void
64-
{
65-
$this->expectException(\BadMethodCallException::class);
66-
67-
$subject = new CalcRuleValueList();
68-
69-
$subject->getArrayRepresentation();
70-
}
7159
}

0 commit comments

Comments
 (0)