Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitca88a41

Browse files
committed
[ObjectMapper] condition on the target class name
1 parentf1e169e commitca88a41

File tree

7 files changed

+87
-4
lines changed

7 files changed

+87
-4
lines changed

‎src/Symfony/Component/ObjectMapper/Attribute/Map.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
readonlyclass Map
2323
{
2424
/**
25-
* @param string|class-string|null$source The property or the class to map from
26-
* @param string|class-string|null$target The property or the class to map to
27-
* @param string|bool|callable(mixed, object): bool|null $if A boolean, a service id or a callable that instructs whether to map
25+
* @param string|class-string|null $source The property or the class to map from
26+
* @param string|class-string|null $target The property or the class to map to
27+
* @paramclass-string|string|bool|callable(mixed, object): bool|null $if A boolean, a service id or a callable that instructs whether to map
2828
* @param (string|callable(mixed, object): mixed)|(string|callable(mixed, object): mixed)[]|null $transform A service id or a callable that transforms the value during mapping
2929
*/
3030
publicfunction__construct(

‎src/Symfony/Component/ObjectMapper/Metadata/Mapping.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
/**
2424
* @param string|class-string|null $source The property or the class to map from
2525
* @param string|class-string|null $target The property or the class to map to
26-
* @param string|bool|callable(mixed, object): bool|null $if A boolean, Symfony service name or a callable that instructs whether to map
26+
* @paramclass-string|string|bool|callable(mixed, object): bool|null $if A boolean, Symfony service name or a callable that instructs whether to map
2727
* @param (string|callable(mixed, object): mixed)|(string|callable(mixed, object): mixed)[]|null $transform A service id or a callable that transform the value during mapping
2828
*/
2929
publicfunction__construct(

‎src/Symfony/Component/ObjectMapper/ObjectMapper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ public function map(object $source, object|string|null $target = null): object
117117
$propertyName =$property->getName();
118118
$mappings =$this->metadataFactory->create($readMetadataFrom,$propertyName);
119119
foreach ($mappingsas$mapping) {
120+
if (\is_string($mapping->if) && !$this->conditionCallableLocator?->has($mapping->if) && !is_a($mapping->if,$targetRefl->getName(),true)) {
121+
continue;
122+
}
123+
120124
$sourcePropertyName =$propertyName;
121125
if ($mapping->source && (!$refl->hasProperty($propertyName) || !isset($source->$propertyName))) {
122126
$sourcePropertyName =$mapping->source;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
namespaceSymfony\Component\ObjectMapper\Tests\Fixtures\MultipleTargetProperty;
13+
14+
useSymfony\Component\ObjectMapper\Attribute\Map;
15+
16+
#[Map(target: B::class)]
17+
#[Map(target: C::class)]
18+
class A
19+
{
20+
#[Map(target:'foo', transform:'strtoupper', if: B::class)]
21+
#[Map(target:'bar')]
22+
publicstring$something ='test';
23+
24+
publicstring$doesNotExistInTargetB ='foo';
25+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
namespaceSymfony\Component\ObjectMapper\Tests\Fixtures\MultipleTargetProperty;
13+
14+
class B
15+
{
16+
publicstring$foo;
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
namespaceSymfony\Component\ObjectMapper\Tests\Fixtures\MultipleTargetProperty;
13+
14+
class C
15+
{
16+
publicstring$foo ='donotmap';
17+
publicstring$bar;
18+
publicstring$doesNotExistInTargetB;
19+
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
useSymfony\Component\ObjectMapper\Tests\Fixtures\MapStruct\MapStructMapperMetadataFactory;
3939
useSymfony\Component\ObjectMapper\Tests\Fixtures\MapStruct\Source;
4040
useSymfony\Component\ObjectMapper\Tests\Fixtures\MapStruct\Target;
41+
useSymfony\Component\ObjectMapper\Tests\Fixtures\MultipleTargetProperty\AasMultipleTargetPropertyA;
42+
useSymfony\Component\ObjectMapper\Tests\Fixtures\MultipleTargetProperty\BasMultipleTargetPropertyB;
43+
useSymfony\Component\ObjectMapper\Tests\Fixtures\MultipleTargetProperty\CasMultipleTargetPropertyC;
4144
useSymfony\Component\ObjectMapper\Tests\Fixtures\MultipleTargets\AasMultipleTargetsA;
4245
useSymfony\Component\ObjectMapper\Tests\Fixtures\MultipleTargets\CasMultipleTargetsC;
4346
useSymfony\Component\ObjectMapper\Tests\Fixtures\Recursion\AB;
@@ -262,4 +265,19 @@ public function testTransformToWrongObject()
262265
$mapper =newObjectMapper($metadata);
263266
$mapper->map($u);
264267
}
268+
269+
publicfunctiontestMultipleTargetMapProperty()
270+
{
271+
$u =newMultipleTargetPropertyA();
272+
273+
$mapper =newObjectMapper();
274+
$b =$mapper->map($u, MultipleTargetPropertyB::class);
275+
$this->assertInstanceOf(MultipleTargetPropertyB::class,$b);
276+
$this->assertEquals($b->foo,'TEST');
277+
$c =$mapper->map($u, MultipleTargetPropertyC::class);
278+
$this->assertInstanceOf(MultipleTargetPropertyC::class,$c);
279+
$this->assertEquals($c->bar,'test');
280+
$this->assertEquals($c->foo,'donotmap');
281+
$this->assertEquals($c->doesNotExistInTargetB,'foo');
282+
}
265283
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp