Skip to content

Commit

Permalink
Merge pull request #1561 from bobvandevijver/generic-enum-interface
Browse files Browse the repository at this point in the history
Do not use single quotes in enum type definition
  • Loading branch information
scyzoryck authored Oct 16, 2024
2 parents 745aeb7 + 5997eae commit 25b5ebe
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 10 deletions.
1 change: 1 addition & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ If you are on version `1.x`, it is suggested to migrate directly to `3.0.0` (sin
- `JsonSerializationVisitor::hasData` will be removed
- `VisitorInterface` is internal, use `SerializationVisitorInterface` and `DeserializationVisitorInterface` instead
- `GraphNavigator` is internal, use `GraphNavigatorInterface` instead
- `enum<'Type'>` and similar are deprecated, use `enum<Type>` instead

**Other**
- Elements (as classes, interfaces, methods, properties...)
Expand Down
8 changes: 4 additions & 4 deletions doc/reference/annotations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -454,19 +454,19 @@ Available Types:
| | Examples: array<string, string>, |
| | array<string, MyNamespace\MyObject>, etc. |
+------------------------------------------------------------+--------------------------------------------------+
| enum<'Color'> | Enum of type Color, use its case values |
| enum<T> | Enum of type Color, use its case values |
| | for serialization and deserialization |
| | if the enum is a backed enum, |
| | use its case names if it is not a backed enum. |
+------------------------------------------------------------+--------------------------------------------------+
| enum<'Color', 'name'> | Enum of type Color, use its case names |
| enum<T, 'name'> | Enum of type Color, use its case names |
| | (as string) for serialization |
| | and deserialization. |
+------------------------------------------------------------+--------------------------------------------------+
| enum<'Color', 'value'> | Backed Enum of type Color, use its case value |
| enum<T, 'value'> | Backed Enum of type Color, use its case value |
| | for serialization and deserialization. |
+------------------------------------------------------------+--------------------------------------------------+
| enum<'Color', 'value', 'integer'> | Backed Enum of type Color, use its case value |
| enum<T, 'value', 'integer'> | Backed Enum of type Color, use its case value |
| | (forced as integer) for serialization |
| | and deserialization. |
+------------------------------------------------------------+--------------------------------------------------+
Expand Down
6 changes: 6 additions & 0 deletions src/Handler/EnumHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ public function serializeEnum(
public function deserializeEnum(DeserializationVisitorInterface $visitor, $data, array $type): ?\UnitEnum
{
$enumType = $type['params'][0];
if (isset($enumType['name'])) {
$enumType = $enumType['name'];
} else {
trigger_deprecation('jms/serializer', '3.31', "Using enum<'Type'> or similar is deprecated, use enum<Type> instead.");
}

$caseValue = (string) $data;

$ref = new \ReflectionEnum($enumType);
Expand Down
8 changes: 7 additions & 1 deletion src/Metadata/Driver/EnumPropertiesDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ public function loadMetadataForClass(ReflectionClass $class): ?ClassMetadata
try {
$propertyReflection = $this->getReflection($propertyMetadata);
if ($enum = $this->getEnumReflection($propertyReflection)) {
$serializerType = ['name' => 'enum', 'params' => [$enum->getName(), $enum->isBacked() ? 'value' : 'name']];
$serializerType = [
'name' => 'enum',
'params' => [
['name' => $enum->getName(), 'params' => []],
$enum->isBacked() ? 'value' : 'name',
],
];
$propertyMetadata->setType($serializerType);
}
} catch (ReflectionException $e) {
Expand Down
12 changes: 7 additions & 5 deletions tests/Fixtures/ObjectWithEnums.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,34 @@
class ObjectWithEnums
{
/**
* @Serializer\Type("enum<'JMS\Serializer\Tests\Fixtures\Enum\Suit', 'name'>")
* @Serializer\Type("enum<JMS\Serializer\Tests\Fixtures\Enum\Suit, 'name'>")
*/
public Suit $ordinary;

/**
* @Serializer\Type("enum<'JMS\Serializer\Tests\Fixtures\Enum\BackedSuit', 'value'>")
* @Serializer\Type("enum<JMS\Serializer\Tests\Fixtures\Enum\BackedSuit, 'value'>")
*/
public BackedSuit $backedValue;

/**
* Deprecated, remove single quote around type with 4.0.
*
* @Serializer\Type("enum<'JMS\Serializer\Tests\Fixtures\Enum\BackedSuit'>")
*/
public BackedSuit $backedWithoutParam;

/**
* @Serializer\Type("array<enum<'JMS\Serializer\Tests\Fixtures\Enum\Suit'>>")
* @Serializer\Type("array<enum<JMS\Serializer\Tests\Fixtures\Enum\Suit>>")
*/
public array $ordinaryArray;

/**
* @Serializer\Type("array<enum<'JMS\Serializer\Tests\Fixtures\Enum\BackedSuit', 'value'>>")
* @Serializer\Type("array<enum<JMS\Serializer\Tests\Fixtures\Enum\BackedSuit, 'value'>>")
*/
public array $backedArray;

/**
* @Serializer\Type("array<enum<'JMS\Serializer\Tests\Fixtures\Enum\BackedSuit'>>")
* @Serializer\Type("array<enum<JMS\Serializer\Tests\Fixtures\Enum\BackedSuit>>")
*/
public array $backedArrayWithoutParam;

Expand Down

0 comments on commit 25b5ebe

Please sign in to comment.