Skip to content

Navigation Menu

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.3
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 A
{
#[Map(transform: new MapCollection())]
/** @var C[] */
public array $foo;
soyuka marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

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

class B
{
/** @var D[] */
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: D::class)]
class C
{
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 D
{
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\A as TransformCollectionA;
use Symfony\Component\ObjectMapper\Tests\Fixtures\TransformCollection\B as TransformCollectionB;
use Symfony\Component\ObjectMapper\Tests\Fixtures\TransformCollection\C as TransformCollectionC;
use Symfony\Component\ObjectMapper\Tests\Fixtures\TransformCollection\D as TransformCollectionD;
Comment on lines +54 to +57
Copy link
Member

Choose a reason for hiding this comment

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

I would make the classe names longer instead of using an alias. It's simpler to call the class by its name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

its nitpicking as these are test fixtures I don't see the benefits

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);
}
}
42 changes: 42 additions & 0 deletions 42 src/Symfony/Component/ObjectMapper/Transform/MapCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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
{
if (!is_iterable($value)) {
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.

$v = $this->objectMapper->map($v);

Check failure on line 37 in src/Symfony/Component/ObjectMapper/Transform/MapCollection.php

View workflow job for this annotation

GitHub Actions / Psalm

NoValue

src/Symfony/Component/ObjectMapper/Transform/MapCollection.php:37:13: NoValue: All possible types for this assignment were invalidated - This may be dead code (see https://psalm.dev/179)

Check failure on line 37 in src/Symfony/Component/ObjectMapper/Transform/MapCollection.php

View workflow job for this annotation

GitHub Actions / Psalm

NoValue

src/Symfony/Component/ObjectMapper/Transform/MapCollection.php:37:13: NoValue: All possible types for this assignment were invalidated - This may be dead code (see https://psalm.dev/179)
}

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