Skip to content

Commit 7538c81

Browse files
authored
[TASK] Implement CSSString::getArrayRepresentation() (#1459)
Part of #1440.
1 parent 95263d1 commit 7538c81

2 files changed

Lines changed: 36 additions & 4 deletions

File tree

src/Value/CSSString.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
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
use function Safe\preg_match;
1415

@@ -19,6 +20,8 @@
1920
*/
2021
class CSSString extends PrimitiveValue
2122
{
23+
use ShortClassNameProvider;
24+
2225
/**
2326
* @var string
2427
*/
@@ -94,4 +97,18 @@ public function render(OutputFormat $outputFormat): string
9497
$string = \str_replace("\n", '\\A', $string);
9598
return $outputFormat->getStringQuotingType() . $string . $outputFormat->getStringQuotingType();
9699
}
100+
101+
/**
102+
* @return array<string, bool|int|float|string|list<array<string, mixed>>>
103+
*
104+
* @internal
105+
*/
106+
public function getArrayRepresentation(): array
107+
{
108+
return [
109+
'class' => $this->getShortClassName(),
110+
// We're using the term "contents" here to make the difference to the class more clear.
111+
'contents' => $this->string,
112+
];
113+
}
97114
}

tests/Unit/Value/CSSStringTest.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);
90-
9189
$subject = new CSSString('');
9290

93-
$subject->getArrayRepresentation();
91+
$result = $subject->getArrayRepresentation();
92+
93+
self::assertArrayHasKey('class', $result);
94+
self::assertSame('CSSString', $result['class']);
95+
}
96+
97+
/**
98+
* @test
99+
*/
100+
public function getArrayRepresentationIncludesContents(): void
101+
{
102+
$contents = 'What is love?';
103+
$subject = new CSSString($contents);
104+
105+
$result = $subject->getArrayRepresentation();
106+
107+
self::assertArrayHasKey('contents', $result);
108+
self::assertSame($contents, $result['contents']);
94109
}
95110
}

0 commit comments

Comments
 (0)