-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
base: 7.4
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
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; | ||
} |
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 | ||
) {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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($transformed->foo, [new TransformCollectionD('a'), new TransformCollectionD('b')]); | ||
soyuka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
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()) | ||
soyuka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
} | ||
|
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||
} | ||
|
||
return $value; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.