-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[ObjectMapper] Add component #20347
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?
[ObjectMapper] Add component #20347
Conversation
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.
Antoine, thanks for contributing the docs for this new component.
I left some minor comments.
object-mapper.rst
Outdated
#[Map(target: C::class, if: [Source::class, 'shouldMapToC'])] | ||
class Source | ||
{ | ||
public static function shouldMapToB(mixed $value, object $object): bool |
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.
Which are the mixed $value, object $object
arguments passed to this method?
object-mapper.rst
Outdated
use Symfony\Component\ObjectMapper\Attributes\Map; | ||
|
||
class Source { | ||
#[Map(target: 'fullName', transform: TransformNameCallable::class)] |
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.
Can I use a transform
for the entire object instead of for each of its properties?
CI should be green after a rebase on |
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.
A rebase on 7.3 branch would be helpful @soyuka, thanks
I've rebased, this also documents symfony/symfony#60028 as it's quite mandatory for many real world use cases. Interfaces are now matching the merged code. At the beginning I've used real word examples @94noni could you let me know your thoughts and if you think I should apply the same logic accross the whole documentation? |
de11d09
to
f4c8610
Compare
The new version needs symfony/symfony#60028 |
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.
The doc is already very clear.
Just a few small questions and remarks here and there.
Also, you could maybe add a short section on how to debug mappings.
But overall it looks good to me 👍🏼.
|
||
Symfony provides a mapper component to transform a given object into another one, | ||
simplifying tasks like converting DTOs (Data Transfer Objects) to entities or | ||
vice-versa. |
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.
We could add "This component is especially useful when decoupling API input/output from internal models."
class name we use ``newInstanceWithoutConstructor``) but *before* properties | ||
are mapped. Its responsibility is often to return a correctly initialized | ||
instance, potentially replacing the one created by the mapper. | ||
The returned value *must* be an object of the target type. |
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.
What If it is not an object? An exception will be thrown?
Sometimes, it's more convenient to define the mapping on the *target* class, | ||
specifying where its properties should come from in the *source* class. This is | ||
done using the ``source`` parameter within the ``#[Map]`` attribute on the target's | ||
properties. |
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.
What if both the source and the target classes define #[Map] attributes? Which one takes precedence?
Documents the object mapper component from