-
-
Notifications
You must be signed in to change notification settings - Fork 9.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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
I extracted the CS fixes into to keep this PR clean |
564b44b
to
81039a8
Compare
81039a8
to
f803dc1
Compare
95783e6
to
2b46190
Compare
2b46190
to
377a6a6
Compare
377a6a6
to
388f5ab
Compare
a60b582
to
ca88a41
Compare
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'] |
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 |
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. |
Adding the The if condition can be an invokable object that implements readonly class TargetClass implements ConditionCallableInterface {
public function __construct(private string $targetClass) {}
public function __invoke(mixed $value, object $source, object $target): bool {
return is_a($this->targetClass, $target, true);
}
} Usage: #[Map(target: B::class)]
class A
{
#[Map(target: 'foo', transform: 'strtoupper', if: new TargetClass(B::class))]
public string $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
to
53b56d6
Compare
53b56d6
to
1910509
Compare
I'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')]
public string $something = 'test';
public string $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 |
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 |
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
src/Symfony/Component/ObjectMapper/Tests/Fixtures/ServiceLocator/TransformCallable.php
Outdated
Show resolved
Hide resolved
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) |
fd55ff0
to
a5698aa
Compare
Thank you @soyuka. |
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.