Skip to content

Commit 7d1f175

Browse files
committed
[TASK] Implement getArrayRepresentation() in CSSFunction
Also include a demo of its usage. Part of #1440.
1 parent 09adec3 commit 7d1f175

5 files changed

Lines changed: 63 additions & 16 deletions

File tree

src/Value/CSSFunction.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ public function render(OutputFormat $outputFormat): string
121121
*/
122122
public function getArrayRepresentation(): array
123123
{
124-
throw new \BadMethodCallException('`getArrayRepresentation` is not yet implemented for `' . self::class . '`');
124+
$result = parent::getArrayRepresentation();
125+
126+
$result['name'] = $this->name;
127+
128+
return $result;
125129
}
126130
}

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

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,43 @@ public function parsesArithmeticInFunctions(string $operator): void
6060
self::DEFAULT_DELIMITERS
6161
);
6262

63+
$result = $subject->getArrayRepresentation();
64+
self::assertSame(
65+
[
66+
'class' => 'CSSFunction',
67+
'components' => [
68+
[
69+
'class' => 'Size',
70+
'number' => 300.0,
71+
'unit' => 'px',
72+
],
73+
[
74+
'class' => 'RuleValueList',
75+
'components' => [
76+
[
77+
'class' => 'Size',
78+
'number' => 50.0,
79+
'unit' => 'vh',
80+
],
81+
[
82+
'class' => 'string',
83+
'value' => $operator,
84+
],
85+
[
86+
'class' => 'Size',
87+
'number' => 10.0,
88+
'unit' => 'px',
89+
],
90+
],
91+
'separator' => ' ',
92+
],
93+
],
94+
'separator' => ',',
95+
'name' => 'max',
96+
],
97+
$result
98+
);
99+
63100
self::assertInstanceOf(CSSFunction::class, $subject);
64101
self::assertSame('max(300px,50vh ' . $operator . ' 10px)', $subject->render(OutputFormat::createCompact()));
65102
}

0 commit comments

Comments
 (0)