Skip to content

Commit bbda5b2

Browse files
committed
[ObjectMapper] perfs
1 parent f810760 commit bbda5b2

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/Symfony/Component/ObjectMapper/ObjectMapper.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,19 @@ public function withObjectMapper(ObjectMapperInterface $objectMapper): static
390390
return $clone;
391391
}
392392

393+
private function needsEnumConversion(mixed $value, string $targetTypeName): bool
394+
{
395+
if ($value instanceof \UnitEnum) {
396+
return true;
397+
}
398+
399+
if (enum_exists($targetTypeName)) {
400+
return true;
401+
}
402+
403+
return false;
404+
}
405+
393406
private function convertValueForProperty(mixed $value, \ReflectionClass $targetRefl, string $propertyName): mixed
394407
{
395408
if (!$targetRefl->hasProperty($propertyName)) {
@@ -405,17 +418,18 @@ private function convertValueForProperty(mixed $value, \ReflectionClass $targetR
405418

406419
$targetTypeName = $targetType->getName();
407420

408-
// Enum → Scalaire
421+
if (!$this->needsEnumConversion($value, $targetTypeName)) {
422+
return $value;
423+
}
424+
409425
if ($value instanceof \BackedEnum && \in_array($targetTypeName, ['string', 'int'], true)) {
410426
return $this->convertFromBackedEnum($value, $targetTypeName);
411427
}
412428

413-
// Pure enum → Scalaire (erreur)
414429
if ($value instanceof \UnitEnum && !\is_a($targetTypeName, \UnitEnum::class, true)) {
415430
throw new MappingException(\sprintf('Cannot convert pure enum "%s" to non-enum type "%s".', $value::class, $targetTypeName));
416431
}
417432

418-
// Scalaire → Enum
419433
if (\is_a($targetTypeName, \BackedEnum::class, true) && !\is_object($value)) {
420434
return $this->convertToBackedEnum($value, $targetTypeName);
421435
}

0 commit comments

Comments
 (0)