Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.6k
[ObjectMapper] Condition to target a specific class#60028
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Uh oh!
There was an error while loading.Please reload this page.
I extracted the CS fixes into to keep this PR clean |
564b44b
to81039a8
Compare81039a8
tof803dc1
Compare95783e6
to2b46190
Compare2b46190
to377a6a6
Compare377a6a6
to388f5ab
CompareUh oh!
There was an error while loading.Please reload this page.
a60b582
toca88a41
CompareGromNaN commentedMar 31, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
The feature is relevant, but I find the syntax of the attribute is not explicit. I would find it clearer if the target contained - #[Map(target: 'foo', transform: 'strtoupper', if: B::class)]+ #[Map(target: [B::class, 'foo'], transform: 'strtoupper'] |
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
This was my first attempt but its weird to use a Closure that's not interpreted as a Closure no? Also the |
Then, can we add a |
soyuka commentedMar 31, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
using the class name is just a shortcut for that. Although to make this work we would also need to add the To re-center the debate I think symfony should allow some use cases with a as straightforward as possible DX, when users want more behavior it's quite feasible using the MetadataFactory. |
GromNaN commentedMar 31, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Adding the The if condition can be an invokable object that implements readonlyclass TargetClassimplements ConditionCallableInterface {publicfunction__construct(privatestring$targetClass) {}publicfunction__invoke(mixed$value,object$source,object$target):bool {returnis_a($this->targetClass,$target,true); }} Usage: #[Map(target: B::class)]class A{ #[Map(target:'foo', transform:'strtoupper', if:newTargetClass(B::class))]publicstring$something ='test';} That way, it's explicit, it doesn't require the PHP 8.5 closures in attributes and keep a good DX. Later you can combine more conditions by nesting objects. Experimentation:soyuka#1 |
c826e6d
to53b56d6
Compare53b56d6
to1910509
CompareI'm being late on this, but having this check on the It's due to the fact, that those kind of condition (evaluating a target class) can be easily checked during metadata extraction. But if there is future condition that induce runtime check it will be impossible to extrapolate this check during metadata extraction (since it's too generic) so we will have to do it each time |
Maybe something like this would be better : #[Map(target: B::class)]#[Map(target: C::class)]class A{ #[Map(to:'foo', target: B::class, transform:'strtoupper')] #[Map(to:'bar')]publicstring$something ='test';publicstring$doesNotExistInTargetB ='foo';} It keep target consistent with the attribute being on either a property or the class, However this is a hard change in current api (but since it's not released i think it's ok ?) |
It would be no problem yes |
@joelwurtz I think it's too confusing to have |
joelwurtz commentedApr 2, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Personally i find this confusing to have the same property on the same attribute that does not mean the same thing given the position of the attribute :/ (as an user i would expect a property having the same meaning)
Not sure how it would work since they may be check that combine multiple checks ? I just find this complex given the use case EDIT : Also this could be : #[Map(property:'foo', target: B::class, transform:'strtoupper')] (Could be also rename ? or any other name) So no to / from, work both ways |
soyuka commentedApr 3, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I'm not sure we need to anticipate these use cases quite yet as we're still a long way to go before adding performance optimizations. I suggest that we leave this like this: the
Sure but then they would be executed at runtime, just like transformations. Let's say we have
You mean having the |
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.
With minor comments
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
src/Symfony/Component/ObjectMapper/Tests/Fixtures/ServiceLocator/TransformCallable.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Yes that was i mean, i think consistent wording is important across a component, specially when this parameter can be considered as a "configuration" parameter having different meaning for the same word in the same component will induce a lot of friction IMO |
Although the potential confusion seems real, making that setting context-dependent (i.e. expecting a property if used on a property vs a FQCN if used on a class) looks sensible to me. Throwing meaningful errors on such mistake should be good enough, do we have that? |
IMHO its outside of the current scope of this PR we can always open a new issue to discuss this (or add this to the RFC issue) |
Thank you@soyuka. |
7379bfb
intosymfony:7.3Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
We want to be able to choose which property to map according to the target. Here
foo
is mapped toB
only when the target isB
. IfC
has afoo
property it won't be mapped as we only map tobar
.This is a good alternative to groups as we can have one class that has multiple representation.