Skip to content
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

enableFlexibleCasting but just for scalars #524

Open
staabm opened this issue Apr 9, 2024 · 1 comment
Open

enableFlexibleCasting but just for scalars #524

staabm opened this issue Apr 9, 2024 · 1 comment

Comments

@staabm
Copy link

staabm commented Apr 9, 2024

we would want to have type juggling from e.g. numeric-strings into int; same for float from string (just like regular php cast rules).

we found the enableFlexibleCasting to achieve it,

$flexibleMapper = (new \CuyZ\Valinor\MapperBuilder())
    ->enableFlexibleCasting()
    ->mapper();

$flexibleMapper->map('int', '42');
// => 42

but it also enables some kind of list<->array juggling, which we would not want:

$flexibleMapper->map('list<int>', ['foo' => 42, 'bar' => 1337]);
// => [0 => 42, 1 => 1338]

is there a supported way to only enable type juggling for scalars? or some kind of api we should use?

our use-case: we want to convert from $_GET or $_POST which is always string and need to map it into objects which e.g. expect int.

it seems valinor is already able so we can easily define per class how data needs to be mapped to said class, but it seems to miss some kind of "general conversion" building blocks for scalar types only.

@romm
Copy link
Member

romm commented Apr 12, 2024

Hi @staabm, thanks for the report. For now it is not possible to distinguish the scalar casting from the other cases. I definitely see the value in your usecase and will be thinking about how to extend the current API.

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

No branches or pull requests

2 participants