Skip to content

Commit d4fd462

Browse files
bug #60491 [ObjectMapper] added earlier skip to allow if=false when using source mapping (maciekpaprocki)
This PR was merged into the 7.3 branch. Discussion ---------- [ObjectMapper] added earlier skip to allow if=false when using source mapping | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | License | MIT Currently when using object mapper from source if seems to fail if the oiginal source class doesn't have this field. Which means ```php class A { public $id = 1 } class B { #[Map(source:'id')] public $id = null #[Map(if:false)] public $additionalInfo = null } $objectMapper->map(new A(), new B()); ``` will fail because there's no property available in A. Moving the if statement up in the object mapper solves the issue if #Map(if: false). It doesn't support full "IF" logic as it's dependent on value, but I think that's all right. Don't really see why someone would need that if there's no mapping value either way. Commits ------- 5b2634f added earlier skip to allow if=false when using source mapping
2 parents baad646 + 5b2634f commit d4fd462

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/Symfony/Component/ObjectMapper/ObjectMapper.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,12 @@ public function map(object $source, object|string|null $target = null): object
122122
$sourcePropertyName = $mapping->source;
123123
}
124124

125-
$value = $this->getRawValue($source, $sourcePropertyName);
126-
if (($if = $mapping->if) && ($fn = $this->getCallable($if, $this->conditionCallableLocator)) && !$this->call($fn, $value, $source, $mappedTarget)) {
125+
if (false === $if = $mapping->if) {
127126
continue;
128127
}
129128

130-
if (false === $if) {
129+
$value = $this->getRawValue($source, $sourcePropertyName);
130+
if ($if && ($fn = $this->getCallable($if, $this->conditionCallableLocator)) && !$this->call($fn, $value, $source, $mappedTarget)) {
131131
continue;
132132
}
133133

src/Symfony/Component/ObjectMapper/Tests/Fixtures/HydrateObject/SourceOnly.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515

1616
class SourceOnly
1717
{
18-
public function __construct(#[Map(source: 'name')] public string $mappedName)
19-
{
18+
public function __construct(
19+
#[Map(source: 'name')] public string $mappedName,
20+
#[Map(if: false)] public ?string $mappedDescription = null
21+
) {
2022
}
2123
}

0 commit comments

Comments
 (0)