Skip to content

Commit 1563641

Browse files
committed
[TASK] Implement getArrayRepresentation() for Size
And also in the abstract base class `Value`, with exception-throwing implementations added for all other descendent classes. Part of #1440.
1 parent 6604f35 commit 1563641

10 files changed

Lines changed: 162 additions & 17 deletions
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/Size.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,19 @@ public function render(OutputFormat $outputFormat): string
202202

203203
return preg_replace(["/$decimalPoint/", '/^(-?)0\\./'], ['.', '$1.'], $size) . ($this->unit ?? '');
204204
}
205+
206+
/**
207+
* @return array<string, bool|int|float|string|list<array<string, mixed>>>
208+
*
209+
* @internal
210+
*/
211+
public function getArrayRepresentation(): array
212+
{
213+
$result = parent::getArrayRepresentation();
214+
215+
$result['number'] = $this->size;
216+
$result['unit'] = $this->unit;
217+
218+
return $result;
219+
}
205220
}

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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,14 @@ public function render(OutputFormat $outputFormat): string
9393
$this->components
9494
);
9595
}
96+
97+
/**
98+
* @return array<string, bool|int|float|string|list<array<string, mixed>>>
99+
*
100+
* @internal
101+
*/
102+
public function getArrayRepresentation(): array
103+
{
104+
throw new \BadMethodCallException('`getArrayRepresentation` is not yet implemented for `' . self::class . '`');
105+
}
96106
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sabberworm\CSS\Tests\Unit;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Sabberworm\CSS\Tests\Unit\Fixtures\ConcreteArrayRepresentationClassProvider;
9+
10+
/**
11+
* @covers \Sabberworm\CSS\ArrayRepresentationClassProvider
12+
*/
13+
final class ArrayRepresentationClassProviderTest extends TestCase
14+
{
15+
/**
16+
* @test
17+
*/
18+
public function getArrayRepresentationIncludesClassName(): void
19+
{
20+
$subject = new ConcreteArrayRepresentationClassProvider();
21+
22+
$result = $subject->getArrayRepresentation();
23+
24+
self::assertArrayHasKey('class', $result);
25+
self::assertSame('ConcreteArrayRepresentationClassProvider', $result['class']);
26+
}
27+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sabberworm\CSS\Tests\Unit\Fixtures;
6+
7+
use Sabberworm\CSS\ArrayRepresentationClassProvider;
8+
use Sabberworm\CSS\OutputFormat;
9+
use Sabberworm\CSS\Renderable;
10+
11+
final class ConcreteArrayRepresentationClassProvider implements Renderable
12+
{
13+
use ArrayRepresentationClassProvider;
14+
15+
/**
16+
* @return never
17+
*/
18+
public function render(OutputFormat $outputFormat): string
19+
{
20+
throw new \BadMethodCallException('Nothing to see here :/', 1767308395);
21+
}
22+
}

tests/Unit/Value/SizeTest.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,39 @@ public function parsesUnit(string $unit): void
102102
/**
103103
* @test
104104
*/
105-
public function getArrayRepresentationThrowsException(): void
105+
public function getArrayRepresentationIncludesClassName(): void
106106
{
107-
$this->expectException(\BadMethodCallException::class);
107+
$subject = new Size(1);
108+
109+
$result = $subject->getArrayRepresentation();
108110

111+
self::assertArrayHasKey('class', $result);
112+
self::assertSame('Size', $result['class']);
113+
}
114+
115+
/**
116+
* @test
117+
*/
118+
public function getArrayRepresentationIncludesNumber(): void
119+
{
109120
$subject = new Size(1);
110121

111-
$subject->getArrayRepresentation();
122+
$result = $subject->getArrayRepresentation();
123+
124+
self::assertArrayHasKey('number', $result);
125+
self::assertSame(1.0, $result['number']);
126+
}
127+
128+
/**
129+
* @test
130+
*/
131+
public function getArrayRepresentationIncludesUnit(): void
132+
{
133+
$subject = new Size(1, 'px');
134+
135+
$result = $subject->getArrayRepresentation();
136+
137+
self::assertArrayHasKey('unit', $result);
138+
self::assertSame('px', $result['unit']);
112139
}
113140
}

tests/Unit/Value/ValueTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,13 @@ public function parsesArithmeticWithMalformedOperandsInFunctions(string $leftOpe
145145
/**
146146
* @test
147147
*/
148-
public function getArrayRepresentationThrowsException(): void
148+
public function getArrayRepresentationIncludesClassName(): void
149149
{
150-
$this->expectException(\BadMethodCallException::class);
151-
152150
$subject = new ConcreteValue();
153151

154-
$subject->getArrayRepresentation();
152+
$result = $subject->getArrayRepresentation();
153+
154+
self::assertArrayHasKey('class', $result);
155+
self::assertSame('ConcreteValue', $result['class']);
155156
}
156157
}

0 commit comments

Comments
 (0)