-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[ObjectMapper] handle non existing property errors #60856
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
base: 7.3
Are you sure you want to change the base?
Conversation
c6a7f6d
to
88a4a59
Compare
return $this->propertyAccessor->getValue($source, $propertyName); | ||
} | ||
|
||
if (!property_exists($source, $propertyName) && !(method_exists($source, '__isset') && true === $source->__isset($propertyName))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a huge fan of testing for the __isset
magic method but if we don't add this its hard to support magic getters properly, maybe we should not support them in the first place?
Comments welcome.
Hmm. I'm not sure why this is a non-existing property error. The DTO has the property $extra, with a default of null (or something else). The source object is a simple StdClass that doesn't have the property. It's not setting a non-existing property of the target object, but rather set a default on the target object when the source object is missing the field. I think it's a pretty common case -- the source and target have some common fields, create the target object and map with the property name is the same or the property has a #[Map()] attribute. |
the "isset" was just something I tried because it seemed like logic that would work, but didn't. Could be a different issue. |
In your example: $mapper = new ObjectMapper();
$target = 'App\Dto\MyClass';
$obj = (object) ['id'=>'abc'];
$entity = $mapper->map($obj, $target); The $this->assertEquals('abc', $b->id);
$this->assertEquals(null, $b->optional);
Isset would be a weird use case but NotNull would be quite useful. |
Thanks. My use case is that I'm getting data from csv and JSON, and I want to map the data to a DTO. But the field names and often type are wrong. So
And I want a DTO of a class with properties $firstName, $lastName,$countryCode,$langCode So I want to import from $row['First Name'] to $dto->firstName and transform the country and language names to codes them with a service. ObjectMapper seems like the perfect tool for that. So this step is to go from the stdClass to a DTO. What approach do you suggest? I've been using league/csv-reader for some parts, (which will map to a DTO), but the problem is that I don't have a DTO yet. |
The property accessor allows to ignore exceptions when a property does not exist. Without the property accessor we now throw a proper exception when the property does not exist (as opposed to the PHP Warning it'd trigger otherwise). I think its better to thrown then to hide the error in that particular case.
This fixes #60848 providing a working solution and a better DX, especially that in PHP the behavior of dynamic properties is to avoid them as much as possible.