-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[ObjectMapper] Feat 61044/enum mapping #62392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Nayte91
wants to merge
1
commit into
symfony:8.1
Choose a base branch
from
Nayte91:feat-61044/enum-mapping
base: 8.1
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <fabien@symfony.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Symfony\Component\ObjectMapper\Transform; | ||
|
|
||
| use Symfony\Component\ObjectMapper\Exception\MappingTransformException; | ||
| use Symfony\Component\ObjectMapper\TransformCallableInterface; | ||
|
|
||
| /** | ||
| * Transforms values between BackedEnum and their scalar representation. | ||
| * | ||
| * This transformer handles bidirectional conversion: | ||
| * - BackedEnum -> int|string (extracts the backing value) | ||
| * - int|string -> BackedEnum (creates enum from scalar) | ||
| * | ||
| * @implements TransformCallableInterface<object, object> | ||
| * | ||
| * @author Julien Robic <ayte91@gmail.com> | ||
| */ | ||
| final class MapEnum implements TransformCallableInterface | ||
| { | ||
| /** | ||
| * @param string $targetType The target type: 'int', 'string', or a BackedEnum class-string | ||
| */ | ||
| public function __construct( | ||
| private readonly string $targetType, | ||
| ) { | ||
| } | ||
|
|
||
| public function __invoke(mixed $value, object $source, ?object $target): mixed | ||
| { | ||
| if (null === $value) { | ||
| return null; | ||
| } | ||
|
|
||
| // BackedEnum -> scalar | ||
| if ($value instanceof \BackedEnum && \in_array($this->targetType, ['int', 'string'], true)) { | ||
| return $this->fromBackedEnum($value); | ||
| } | ||
|
|
||
| // Pure enum -> scalar (not allowed) | ||
| if ($value instanceof \UnitEnum && !$value instanceof \BackedEnum && \in_array($this->targetType, ['int', 'string'], true)) { | ||
| throw new MappingTransformException(\sprintf('Cannot convert pure enum "%s" to scalar type "%s". Only BackedEnum can be converted to scalar values.', $value::class, $this->targetType)); | ||
| } | ||
|
|
||
| // scalar -> BackedEnum | ||
| if (is_a($this->targetType, \BackedEnum::class, true) && !\is_object($value)) { | ||
| return $this->toBackedEnum($value); | ||
| } | ||
|
|
||
| // scalar -> pure enum (not allowed) | ||
| if (is_a($this->targetType, \UnitEnum::class, true) && !is_a($this->targetType, \BackedEnum::class, true) && !\is_object($value)) { | ||
| throw new MappingTransformException(\sprintf('Cannot convert "%s" to pure enum "%s". Pure enums cannot be created from scalar values.', get_debug_type($value), $this->targetType)); | ||
| } | ||
|
|
||
| return $value; | ||
| } | ||
|
|
||
| private function fromBackedEnum(\BackedEnum $enum): int|string | ||
| { | ||
| $backingType = get_debug_type($enum->value); | ||
|
|
||
| if ($backingType !== $this->targetType) { | ||
| throw new MappingTransformException(\sprintf('Cannot convert "%s"-backed enum "%s" to "%s".', $backingType, $enum::class, $this->targetType)); | ||
| } | ||
|
|
||
| return $enum->value; | ||
| } | ||
|
|
||
| /** | ||
| * @return \BackedEnum | ||
| */ | ||
| private function toBackedEnum(int|string $value): \BackedEnum | ||
| { | ||
| $refl = new \ReflectionEnum($this->targetType); | ||
| $backingType = $refl->getBackingType(); | ||
| $expectedType = $backingType instanceof \ReflectionNamedType ? $backingType->getName() : (string) $backingType; | ||
| $actualType = get_debug_type($value); | ||
|
|
||
| if ($expectedType !== $actualType) { | ||
| throw new MappingTransformException(\sprintf('Cannot convert "%s" to "%s"-backed enum "%s".', $actualType, $expectedType, $this->targetType)); | ||
| } | ||
|
|
||
| try { | ||
| return $this->targetType::from($value); | ||
| } catch (\ValueError $e) { | ||
| throw new MappingTransformException(\sprintf('Invalid value "%s" for enum "%s": %s', $value, $this->targetType, $e->getMessage()), 0, $e); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
sdtClassas the$sourcevaluesymfony/src/Symfony/Component/ObjectMapper/ObjectMapper.php
Lines 121 to 127 in 5725a54
will produce a
$readMetadataFromthat is the same as$targetRefl.In this case,
$sourceTypeNameand$targetTypeNamewill be equal, so neither of the two conditions will be appliedMaybe for this we should do something like?