Skip to content

Commit 7c463d7

Browse files
committed
feature #1249 [LiveComponent] Normalize "true" & "false" model values (smnandre)
This PR was merged into the 2.x branch. Discussion ---------- [LiveComponent] Normalize "true" & "false" model values | Q | A | ------------- | --- | Bug fix? | yes? | New feature? | no? | Issues | Fix #1211 | License | MIT Coerce strings "true" & "false" to their boolean equivalent (same for 0 and 1) ```php private static function coerceStringToBoolean(string $value): bool { $booleanMap = [ '0' => false, '1' => true, 'false' => false, 'true' => true, ]; return $booleanMap[$value] ?? (bool) $value; } ``` Is that what you had in mind `@weaverryan` ? Commits ------- 7fbac61 [LiveComponent] Normalize "true" & "false" model values
2 parents 3a127fe + 7fbac61 commit 7c463d7

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/LiveComponent/src/LiveComponentHydrator.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,23 @@ private static function coerceStringValue(string $value, string $type, bool $all
244244
return match ($type) {
245245
'int' => (int) $value,
246246
'float' => (float) $value,
247-
'bool' => (bool) $value,
247+
'bool' => self::coerceStringToBoolean($value),
248248
default => throw new \LogicException(sprintf('Cannot coerce value "%s" to type "%s"', $value, $type)),
249249
};
250250
}
251251

252+
private static function coerceStringToBoolean(string $value): bool
253+
{
254+
$booleanMap = [
255+
'0' => false,
256+
'1' => true,
257+
'false' => false,
258+
'true' => true,
259+
];
260+
261+
return $booleanMap[$value] ?? (bool) $value;
262+
}
263+
252264
private function calculateChecksum(array $dehydratedPropsData): ?string
253265
{
254266
// sort so it is always consistent (frontend could have re-ordered data)

src/LiveComponent/tests/Integration/LiveComponentHydratorTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,6 +1460,20 @@ public function testCanDehydrateAndHydrateComponentsWithEmptyAttributes(): void
14601460
$this->assertSame([], $actualAttributes->all());
14611461
}
14621462

1463+
/**
1464+
* @dataProvider truthyValueProvider
1465+
*/
1466+
public function testCoerceTruthyValuesForScalarTypes($prop, $value, $expected): void
1467+
{
1468+
$dehydratedProps = $this->dehydrateComponent($this->mountComponent('scalar_types'))->getProps();
1469+
1470+
$updatedProps = [$prop => $value];
1471+
$hydratedComponent = $this->getComponent('scalar_types');
1472+
$this->hydrateComponent($hydratedComponent, 'scalar_types', $dehydratedProps, $updatedProps);
1473+
1474+
$this->assertSame($expected, $hydratedComponent->$prop);
1475+
}
1476+
14631477
/**
14641478
* @dataProvider falseyValueProvider
14651479
*/
@@ -1474,6 +1488,15 @@ public function testCoerceFalseyValuesForScalarTypes($prop, $value, $expected):
14741488
$this->assertSame($expected, $hydratedComponent->$prop);
14751489
}
14761490

1491+
public static function truthyValueProvider(): iterable
1492+
{
1493+
yield ['int', '1', 1];
1494+
yield ['float', '1', 1.0];
1495+
yield ['float', 'true', 0.0];
1496+
yield ['bool', 'true', true];
1497+
yield ['bool', '1', true];
1498+
}
1499+
14771500
public static function falseyValueProvider(): iterable
14781501
{
14791502
yield ['int', '', 0];
@@ -1484,6 +1507,7 @@ public static function falseyValueProvider(): iterable
14841507
yield ['float', 'apple', 0.0];
14851508
yield ['bool', '', false];
14861509
yield ['bool', ' ', false];
1510+
yield ['bool', 'false', false];
14871511

14881512
yield ['nullableInt', '', null];
14891513
yield ['nullableInt', ' ', null];

0 commit comments

Comments
 (0)