Skip to content

Commit 4ce090b

Browse files
committed
fix(twig): bc break
1 parent cd3c550 commit 4ce090b

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/TwigComponent/src/ComponentAttributes.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public function __toString(): string
3939
function (string $carry, string $key) {
4040
$value = $this->attributes[$key];
4141

42+
if (null === $value) {
43+
trigger_deprecation('symfony/ux-twig-component', '2.8.0', 'Passing "null" as an attribute value is deprecated and will throw an exception in 3.0.');
44+
$value = true;
45+
}
46+
4247
return match ($value) {
4348
true => "{$carry} {$key}",
4449
false => $carry,
@@ -170,16 +175,11 @@ private function ensureNormalized(): self
170175
private static function normalize(array &$attributes): void
171176
{
172177
foreach ($attributes as $key => &$value) {
173-
if (null === $value) {
174-
trigger_deprecation('symfony/ux-twig-component', '2.8.0', 'Passing "null" as an attribute value is deprecated and will throw an exception in 3.0.');
175-
$value = true;
176-
}
177-
178178
if (\is_array($value) && array_is_list($value)) {
179179
$value = implode(' ', array_filter($value, static fn ($v) => \is_string($v) && '' !== $v));
180180
}
181181

182-
if (!\is_scalar($value)) {
182+
if (!\is_scalar($value) && null !== $value) {
183183
throw new \LogicException(sprintf('A "%s" prop was passed when creating the component. No matching "%s" property or mount() argument was found, so we attempted to use this as an HTML attribute. But, the value is not a scalar (it\'s a %s). Did you mean to pass this to your component or is there a typo on its name?', $key, $key, get_debug_type($value)));
184184
}
185185
}

src/TwigComponent/tests/Unit/ComponentAttributesTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public function testNullBehaviour(): void
188188
{
189189
$attributes = new ComponentAttributes(['disabled' => null]);
190190

191-
$this->assertSame(['disabled' => true], $attributes->all());
191+
$this->assertSame(['disabled' => null], $attributes->all());
192192
$this->assertSame(' disabled', (string) $attributes);
193193
}
194194

@@ -203,9 +203,18 @@ public function testListAttributeValues(): void
203203
{
204204
$attributes = new ComponentAttributes([
205205
'style' => ['foo', null, false, true, '', new \stdClass(), 'bar'],
206+
'class' => ['baz', ''],
207+
]);
208+
209+
$this->assertSame(' style="foo bar" class="baz"', (string) $attributes);
210+
$this->assertSame(['style' => 'foo bar', 'class' => 'baz'], $attributes->all());
211+
212+
$attributes = $attributes->defaults([
213+
'style' => ['baz', false],
214+
'class' => ['', 'qux'],
206215
]);
207216

208-
$this->assertSame(' style="foo bar"', (string) $attributes);
209-
$this->assertSame(['style' => 'foo bar'], $attributes->all());
217+
$this->assertSame(' style="foo bar" class="qux baz"', (string) $attributes);
218+
$this->assertSame(['style' => 'foo bar', 'class' => 'qux baz'], $attributes->all());
210219
}
211220
}

0 commit comments

Comments
 (0)