Skip to content

[ObjectMapper] embed collection transformer #60442

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

Open
wants to merge 3 commits into
base: 7.4
Choose a base branch
from

Conversation

soyuka
Copy link
Contributor

@soyuka soyuka commented May 17, 2025

Q A
Branch? 7.3
Bug fix? no
New feature? yes
Deprecations? no
Issues Related to #60432
License MIT

This is another approach to embeded collection mapping that avoids changing the ObjectMapper. Even if this doesn't land in the component it'd be a good thing to document. Let me know your thoughts.

throw new MappingException(sprintf('The MapCollection transform expects an iterable, "%s" given.', get_debug_type($value)));
}

foreach ($value as &$v) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be best to create a new array this is probably why psalm is not happy :P

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. Plus, if the iterator is not rewindable, the actual implementation can cause troubles.

{
}

public function __invoke(mixed $value, object $source, ?object $target): mixed
Copy link

@makomweb makomweb Jul 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name "$value" does not say anything about what this parameter means from a semantical POV.
"$sourceProperty" might be a better name. Calling a field/property of a class a "variable" is very much confusing to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree but the convention is to name the variable the same as its defined inside the interface, here its the value being mapped.

Copy link

@makomweb makomweb Jul 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uff. Then this should definitely be changed in the interface. A field/property is not a variable ☝️🤓

interface TransformCallableInterface
{
    /**
     * @param mixed $propertyValue The property being mapped
     * @param T     $source The object we're working on
     * @param T2|null $target The target we're mapping to
     */
    public function __invoke(mixed $propertyValue, object $source, ?object $target): mixed;
}
```

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the value of an object's key, in PHP it's not necessary a property though its nitpicking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants