-
Notifications
You must be signed in to change notification settings - Fork 17
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
Handle array value casting #195
Comments
Hey @nlemoine and thanks for reaching us about your issue ! I added your case in my local test suite and reproduced it. I do think this is not even a behavior we do "on purpose", I think this is something we have because we don't add the Also, at the moment we do not add it, but since #180 you can have it in your mappers (it's a configuration that is
Since this is a configuration I would like to flip from I would say this is not something we want to support (array value casting) since we don't even support the "normal" casting and it's just a coincidence it is working at the moment. Baptiste |
Hi everyone, I did have a thought about this issue earlier but didn't find the time to work on implementation. I think in theory we can extract array shape from doc block and create anonymous function to cast array values. It would look something like this: $cast = static fn (float $value) => $value;
$values = [];
foreach ($value['age'] ?? [] as $key => $value_1) {
$values[] = $cast($value_1);
} And its works with either strictTypes setting, if we have it disabled value get casted, if it's enabled we get an error and we know something wrong with the source. Also example where it would behavior the same: class DtoArray
{
/**
* @var array<float>
*/
public array $age;
}
$source = ['age' => [10]];
$target = AutoMapper::create(new Configuration(strictTypes: true))->map($source, DtoArray::class);
assert($target->age === [10.0]); // It's allowed to cast int to float with declare(strict_types=1) |
Salut @Korbeil ! Thanks for taking the time to provide such detailed feedback 🙂 Having the ability to cast weakly typed values would be a nice addition (maybe opt in, with a Feel free to close the issue if this is an unwanted feature. |
Hello,
This is more a question than a bug report.
Are array shapes supposed to be casted? To be more accurate:
Would it be possible to handle array docblock types?
The text was updated successfully, but these errors were encountered: