Skip to content

Commit 2b61cd5

Browse files
authored
[TASK] Implement URL::getArrayRepresentation() (#1462)
Part of #1440.
1 parent 3c27af3 commit 2b61cd5

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/Value/URL.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99
use Sabberworm\CSS\Parsing\SourceException;
1010
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
1111
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
12+
use Sabberworm\CSS\ShortClassNameProvider;
1213

1314
/**
1415
* This class represents URLs in CSS. `URL`s always output in `URL("")` notation.
1516
*/
1617
class URL extends PrimitiveValue
1718
{
19+
use ShortClassNameProvider;
20+
1821
/**
1922
* @var CSSString
2023
*/
@@ -80,4 +83,19 @@ public function render(OutputFormat $outputFormat): string
8083
{
8184
return "url({$this->url->render($outputFormat)})";
8285
}
86+
87+
/**
88+
* @return array<string, bool|int|float|string|array<mixed>|null>
89+
*
90+
* @internal
91+
*/
92+
public function getArrayRepresentation(): array
93+
{
94+
return [
95+
'class' => $this->getShortClassName(),
96+
// We're using the term "uri" here to match the wording used in the specs:
97+
// https://www.w3.org/TR/CSS22/syndata.html#uri
98+
'uri' => $this->url->getArrayRepresentation(),
99+
];
100+
}
83101
}

tests/Unit/Value/URLTest.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,27 @@ public function getLineNumberReturnsLineNumberProvidedToConstructor(): void
8484
/**
8585
* @test
8686
*/
87-
public function getArrayRepresentationThrowsException(): void
87+
public function getArrayRepresentationIncludesClassName(): void
8888
{
89-
$this->expectException(\BadMethodCallException::class);
89+
$subject = new URL(new CSSString('https://example.com'));
9090

91-
$subject = new URL(new CSSString('http://example.com'));
91+
$result = $subject->getArrayRepresentation();
92+
93+
self::assertArrayHasKey('class', $result);
94+
self::assertSame('URL', $result['class']);
95+
}
96+
97+
/**
98+
* @test
99+
*/
100+
public function getArrayRepresentationIncludesUri(): void
101+
{
102+
$uri = 'https://example.com';
103+
$subject = new URL(new CSSString($uri));
104+
105+
$result = $subject->getArrayRepresentation();
92106

93-
$subject->getArrayRepresentation();
107+
self::assertArrayHasKey('uri', $result);
108+
self::assertSame(['class' => 'CSSString', 'contents' => $uri], $result['uri']);
94109
}
95110
}

0 commit comments

Comments
 (0)