File tree 6 files changed +95
-0
lines changed
src/Symfony/Component/ObjectMapper
Fixtures/TransformCollection
6 files changed +95
-0
lines changed Original file line number Diff line number Diff line change
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 A
9
+ {
10
+ #[Map(transform: new MapCollection ())]
11
+ /** @var C[] */
12
+ public array $ foo ;
13
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Symfony \Component \ObjectMapper \Tests \Fixtures \TransformCollection ;
4
+
5
+ class B
6
+ {
7
+ /** @var D[] */
8
+ public array $ foo ;
9
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Symfony \Component \ObjectMapper \Tests \Fixtures \TransformCollection ;
4
+
5
+ use Symfony \Component \ObjectMapper \Attribute \Map ;
6
+
7
+ #[Map(target: D::class)]
8
+ class C
9
+ {
10
+ public function __construct (
11
+ #[Map(target: 'baz ' )]
12
+ public string $ bar
13
+ ) {}
14
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Symfony \Component \ObjectMapper \Tests \Fixtures \TransformCollection ;
4
+
5
+ class D
6
+ {
7
+ public function __construct (
8
+ public string $ baz
9
+ ) {}
10
+ }
Original file line number Diff line number Diff line change 51
51
use Symfony \Component \ObjectMapper \Tests \Fixtures \ServiceLocator \B as ServiceLocatorB ;
52
52
use Symfony \Component \ObjectMapper \Tests \Fixtures \ServiceLocator \ConditionCallable ;
53
53
use Symfony \Component \ObjectMapper \Tests \Fixtures \ServiceLocator \TransformCallable ;
54
+ use Symfony \Component \ObjectMapper \Tests \Fixtures \TransformCollection \A as TransformCollectionA ;
55
+ use Symfony \Component \ObjectMapper \Tests \Fixtures \TransformCollection \B as TransformCollectionB ;
56
+ use Symfony \Component \ObjectMapper \Tests \Fixtures \TransformCollection \C as TransformCollectionC ;
57
+ use Symfony \Component \ObjectMapper \Tests \Fixtures \TransformCollection \D as TransformCollectionD ;
54
58
use Symfony \Component \PropertyAccess \PropertyAccess ;
55
59
56
60
final class ObjectMapperTest extends TestCase
@@ -291,4 +295,15 @@ public function testMultipleTargetMapProperty()
291
295
$ this ->assertEquals ($ c ->foo , 'donotmap ' );
292
296
$ this ->assertEquals ($ c ->doesNotExistInTargetB , 'foo ' );
293
297
}
298
+
299
+ public function testTransformCollection ()
300
+ {
301
+ $ u = new TransformCollectionA ();
302
+ $ u ->foo = [new TransformCollectionC ('a ' ), new TransformCollectionC ('b ' )];
303
+ $ mapper = new ObjectMapper ();
304
+
305
+ $ transformed = $ mapper ->map ($ u , TransformCollectionB::class);
306
+
307
+ $ this ->assertEquals ($ transformed ->foo , [new TransformCollectionD ('a ' ), new TransformCollectionD ('b ' )]);
308
+ }
294
309
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Symfony \Component \ObjectMapper \Transform ;
4
+
5
+ use Symfony \Component \ObjectMapper \Exception \MappingException ;
6
+ use Symfony \Component \ObjectMapper \ObjectMapper ;
7
+ use Symfony \Component \ObjectMapper \ObjectMapperInterface ;
8
+ use Symfony \Component \ObjectMapper \TransformCallableInterface ;
9
+
10
+ /**
11
+ * @template T of object
12
+ *
13
+ * @implements TransformCallableInterface<object, T>
14
+ */
15
+ class MapCollection implements TransformCallableInterface
16
+ {
17
+ public function __construct (private ?ObjectMapperInterface $ objectMapper = null )
18
+ {
19
+ $ this ->objectMapper ??= new ObjectMapper ();
20
+ }
21
+
22
+ public function __invoke (mixed $ value , object $ source , ?object $ target ): mixed
23
+ {
24
+ if (!is_iterable ($ value )) {
25
+ throw new MappingException (sprintf ('The MapCollection transform expects an iterable, "%s" given. ' , get_debug_type ($ value )));
26
+ }
27
+
28
+ foreach ($ value as &$ v ) {
29
+ $ v = $ this ->objectMapper ->map ($ v );
30
+ }
31
+
32
+ return $ value ;
33
+ }
34
+ }
You can’t perform that action at this time.
0 commit comments