Skip to content

Commit cd42aac

Browse files
committed
feature #60442 [ObjectMapper] embed collection transformer (soyuka)
This PR was squashed before being merged into the 7.4 branch. Discussion ---------- [ObjectMapper] embed collection transformer | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | Related to #60432 also fixes #61116 | 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. Commits ------- 0d7d139 [ObjectMapper] embed collection transformer
2 parents 01b49c0 + 0d7d139 commit cd42aac

File tree

7 files changed

+108
-0
lines changed

7 files changed

+108
-0
lines changed

src/Symfony/Component/ObjectMapper/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* The component is not marked as `@experimental` anymore
88
* Add `ObjectMapperAwareInterface` to set the owning object mapper instance
9+
* Add a `MapCollection` transform that calls the Mapper over iterable properties
910

1011
7.3
1112
---
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Symfony\Component\ObjectMapper\Tests\Fixtures\TransformCollection;
4+
5+
use Symfony\Component\ObjectMapper\Attribute\Map;
6+
use Symfony\Component\ObjectMapper\Transform\MapCollection;
7+
8+
class TransformCollectionA
9+
{
10+
#[Map(transform: new MapCollection())]
11+
/** @var TransformCollectionC[] */
12+
public array $foo;
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Symfony\Component\ObjectMapper\Tests\Fixtures\TransformCollection;
4+
5+
class TransformCollectionB
6+
{
7+
/** @var TransformCollectionD[] */
8+
public array $foo;
9+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Symfony\Component\ObjectMapper\Tests\Fixtures\TransformCollection;
4+
5+
use Symfony\Component\ObjectMapper\Attribute\Map;
6+
7+
#[Map(target: TransformCollectionD::class)]
8+
class TransformCollectionC
9+
{
10+
public function __construct(
11+
#[Map(target: 'baz')]
12+
public string $bar,
13+
) {
14+
}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Symfony\Component\ObjectMapper\Tests\Fixtures\TransformCollection;
4+
5+
class TransformCollectionD
6+
{
7+
public function __construct(
8+
public string $baz,
9+
) {
10+
}
11+
}

src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@
7272
use Symfony\Component\ObjectMapper\Tests\Fixtures\ServiceLocator\TransformCallable;
7373
use Symfony\Component\ObjectMapper\Tests\Fixtures\TargetTransform\SourceEntity;
7474
use Symfony\Component\ObjectMapper\Tests\Fixtures\TargetTransform\TargetDto as TargetTransformTargetDto;
75+
use Symfony\Component\ObjectMapper\Tests\Fixtures\TransformCollection\TransformCollectionA;
76+
use Symfony\Component\ObjectMapper\Tests\Fixtures\TransformCollection\TransformCollectionB;
77+
use Symfony\Component\ObjectMapper\Tests\Fixtures\TransformCollection\TransformCollectionC;
78+
use Symfony\Component\ObjectMapper\Tests\Fixtures\TransformCollection\TransformCollectionD;
7579
use Symfony\Component\PropertyAccess\PropertyAccess;
7680

7781
final class ObjectMapperTest extends TestCase
@@ -516,4 +520,15 @@ public function testMapWithSourceTransform()
516520
$this->assertTrue($target->transformed);
517521
$this->assertSame('test', $target->name);
518522
}
523+
524+
public function testTransformCollection()
525+
{
526+
$u = new TransformCollectionA();
527+
$u->foo = [new TransformCollectionC('a'), new TransformCollectionC('b')];
528+
$mapper = new ObjectMapper();
529+
530+
$transformed = $mapper->map($u, TransformCollectionB::class);
531+
532+
$this->assertEquals([new TransformCollectionD('a'), new TransformCollectionD('b')], $transformed->foo);
533+
}
519534
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\ObjectMapper\Transform;
13+
14+
use Symfony\Component\ObjectMapper\Exception\MappingException;
15+
use Symfony\Component\ObjectMapper\ObjectMapper;
16+
use Symfony\Component\ObjectMapper\ObjectMapperInterface;
17+
use Symfony\Component\ObjectMapper\TransformCallableInterface;
18+
19+
/**
20+
* @template T of object
21+
*
22+
* @implements TransformCallableInterface<object, T>
23+
*/
24+
class MapCollection implements TransformCallableInterface
25+
{
26+
public function __construct(
27+
private ObjectMapperInterface $objectMapper = new ObjectMapper(),
28+
) {
29+
}
30+
31+
public function __invoke(mixed $value, object $source, ?object $target): mixed
32+
{
33+
if (!is_iterable($value)) {
34+
throw new MappingException(\sprintf('The MapCollection transform expects an iterable, "%s" given.', get_debug_type($value)));
35+
}
36+
37+
$values = [];
38+
foreach ($value as $k => $v) {
39+
$values[$k] = $this->objectMapper->map($v);
40+
}
41+
42+
return $values;
43+
}
44+
}

0 commit comments

Comments
 (0)