Skip to content

[Live] coerce falsey strings to scalar types #532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 7, 2022

Conversation

kbond
Copy link
Member

@kbond kbond commented Nov 6, 2022

Q A
Bug fix? no
New feature? yes
Tickets Fix #526
License MIT

I chose to allow weird values to be cast to the proper type instead of throwing an exception (ie 0 === (int) 'apple').

@@ -148,6 +148,8 @@ public function hydrate(object $component, array $data, string $componentName):
if ($method = $liveProp->hydrateMethod()) {
// TODO: Error checking
$value = $component->$method($value);
} elseif (\is_string($value) && $type && \in_array($type->getName(), ['int', 'float', 'bool'], true)) {
$value = self::coerceScalarValue($value, $type);
} elseif (!$value && $type && $type->allowsNull() && is_a($type->getName(), \BackedEnum::class, true) && !\in_array($value, array_map(fn (\BackedEnum $e) => $e->value, $type->getName()::cases()))) {
$value = null;
} elseif (null !== $value && $type && !$type->isBuiltin()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This area is increasingly feeling "ad-hoc", but it's well-covered in tests. And if there is some broader solution for handling these edge cases, it'll become obvious eventually 👍

@weaverryan
Copy link
Member

Thanks Kevin!

@weaverryan weaverryan merged commit 240028f into symfony:2.x Nov 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Live] scalar type coercion when hydrating
2 participants