Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[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
Loading
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Symfony\Component\ObjectMapper\Tests\Fixtures\TransformCollection;

use Symfony\Component\ObjectMapper\Attribute\Map;
use Symfony\Component\ObjectMapper\Transform\MapCollection;

class TransformCollectionA
{
#[Map(transform: new MapCollection())]
/** @var TransformCollectionC[] */
public array $foo;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Symfony\Component\ObjectMapper\Tests\Fixtures\TransformCollection;

class TransformCollectionB
{
/** @var TransformCollectionD[] */
public array $foo;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Symfony\Component\ObjectMapper\Tests\Fixtures\TransformCollection;

use Symfony\Component\ObjectMapper\Attribute\Map;

#[Map(target: TransformCollectionD::class)]
class TransformCollectionC
{
public function __construct(
#[Map(target: 'baz')]
public string $bar
) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Symfony\Component\ObjectMapper\Tests\Fixtures\TransformCollection;

class TransformCollectionD
{
public function __construct(
public string $baz
) {}
}
15 changes: 15 additions & 0 deletions 15 src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
use Symfony\Component\ObjectMapper\Tests\Fixtures\ServiceLocator\B as ServiceLocatorB;
use Symfony\Component\ObjectMapper\Tests\Fixtures\ServiceLocator\ConditionCallable;
use Symfony\Component\ObjectMapper\Tests\Fixtures\ServiceLocator\TransformCallable;
use Symfony\Component\ObjectMapper\Tests\Fixtures\TransformCollection\TransformCollectionA;
use Symfony\Component\ObjectMapper\Tests\Fixtures\TransformCollection\TransformCollectionB;
use Symfony\Component\ObjectMapper\Tests\Fixtures\TransformCollection\TransformCollectionC;
use Symfony\Component\ObjectMapper\Tests\Fixtures\TransformCollection\TransformCollectionD;
use Symfony\Component\PropertyAccess\PropertyAccess;

final class ObjectMapperTest extends TestCase
Expand Down Expand Up @@ -291,4 +295,15 @@ public function testMultipleTargetMapProperty()
$this->assertEquals($c->foo, 'donotmap');
$this->assertEquals($c->doesNotExistInTargetB, 'foo');
}

public function testTransformCollection()
{
$u = new TransformCollectionA();
$u->foo = [new TransformCollectionC('a'), new TransformCollectionC('b')];
$mapper = new ObjectMapper();

$transformed = $mapper->map($u, TransformCollectionB::class);

$this->assertEquals([new TransformCollectionD('a'), new TransformCollectionD('b')], $transformed->foo);
}
}
43 changes: 43 additions & 0 deletions 43 src/Symfony/Component/ObjectMapper/Transform/MapCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\ObjectMapper\Transform;

use Symfony\Component\ObjectMapper\Exception\MappingException;
use Symfony\Component\ObjectMapper\ObjectMapper;
use Symfony\Component\ObjectMapper\ObjectMapperInterface;
use Symfony\Component\ObjectMapper\TransformCallableInterface;

/**
* @template T of object
*
* @implements TransformCallableInterface<object, T>
*/
class MapCollection implements TransformCallableInterface
{
public function __construct(private ObjectMapperInterface $objectMapper = new ObjectMapper())
{
}

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.

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 also forgot but this $value may also be the object being mapped :)

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

$values = [];
foreach ($value as $v) {
$values[] = $this->objectMapper->map($v);
}

return $values;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.