Skip to content

Commit 25b5ebe

Browse files
authored
Merge pull request #1561 from bobvandevijver/generic-enum-interface
Do not use single quotes in enum type definition
2 parents 745aeb7 + 5997eae commit 25b5ebe

File tree

5 files changed

+25
-10
lines changed

5 files changed

+25
-10
lines changed

UPGRADING.md

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ If you are on version `1.x`, it is suggested to migrate directly to `3.0.0` (sin
104104
- `JsonSerializationVisitor::hasData` will be removed
105105
- `VisitorInterface` is internal, use `SerializationVisitorInterface` and `DeserializationVisitorInterface` instead
106106
- `GraphNavigator` is internal, use `GraphNavigatorInterface` instead
107+
- `enum<'Type'>` and similar are deprecated, use `enum<Type>` instead
107108

108109
**Other**
109110
- Elements (as classes, interfaces, methods, properties...)

doc/reference/annotations.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -454,19 +454,19 @@ Available Types:
454454
| | Examples: array<string, string>, |
455455
| | array<string, MyNamespace\MyObject>, etc. |
456456
+------------------------------------------------------------+--------------------------------------------------+
457-
| enum<'Color'> | Enum of type Color, use its case values |
457+
| enum<T> | Enum of type Color, use its case values |
458458
| | for serialization and deserialization |
459459
| | if the enum is a backed enum, |
460460
| | use its case names if it is not a backed enum. |
461461
+------------------------------------------------------------+--------------------------------------------------+
462-
| enum<'Color', 'name'> | Enum of type Color, use its case names |
462+
| enum<T, 'name'> | Enum of type Color, use its case names |
463463
| | (as string) for serialization |
464464
| | and deserialization. |
465465
+------------------------------------------------------------+--------------------------------------------------+
466-
| enum<'Color', 'value'> | Backed Enum of type Color, use its case value |
466+
| enum<T, 'value'> | Backed Enum of type Color, use its case value |
467467
| | for serialization and deserialization. |
468468
+------------------------------------------------------------+--------------------------------------------------+
469-
| enum<'Color', 'value', 'integer'> | Backed Enum of type Color, use its case value |
469+
| enum<T, 'value', 'integer'> | Backed Enum of type Color, use its case value |
470470
| | (forced as integer) for serialization |
471471
| | and deserialization. |
472472
+------------------------------------------------------------+--------------------------------------------------+

src/Handler/EnumHandler.php

+6
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ public function serializeEnum(
6464
public function deserializeEnum(DeserializationVisitorInterface $visitor, $data, array $type): ?\UnitEnum
6565
{
6666
$enumType = $type['params'][0];
67+
if (isset($enumType['name'])) {
68+
$enumType = $enumType['name'];
69+
} else {
70+
trigger_deprecation('jms/serializer', '3.31', "Using enum<'Type'> or similar is deprecated, use enum<Type> instead.");
71+
}
72+
6773
$caseValue = (string) $data;
6874

6975
$ref = new \ReflectionEnum($enumType);

src/Metadata/Driver/EnumPropertiesDriver.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ public function loadMetadataForClass(ReflectionClass $class): ?ClassMetadata
4848
try {
4949
$propertyReflection = $this->getReflection($propertyMetadata);
5050
if ($enum = $this->getEnumReflection($propertyReflection)) {
51-
$serializerType = ['name' => 'enum', 'params' => [$enum->getName(), $enum->isBacked() ? 'value' : 'name']];
51+
$serializerType = [
52+
'name' => 'enum',
53+
'params' => [
54+
['name' => $enum->getName(), 'params' => []],
55+
$enum->isBacked() ? 'value' : 'name',
56+
],
57+
];
5258
$propertyMetadata->setType($serializerType);
5359
}
5460
} catch (ReflectionException $e) {

tests/Fixtures/ObjectWithEnums.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,34 @@
1212
class ObjectWithEnums
1313
{
1414
/**
15-
* @Serializer\Type("enum<'JMS\Serializer\Tests\Fixtures\Enum\Suit', 'name'>")
15+
* @Serializer\Type("enum<JMS\Serializer\Tests\Fixtures\Enum\Suit, 'name'>")
1616
*/
1717
public Suit $ordinary;
1818

1919
/**
20-
* @Serializer\Type("enum<'JMS\Serializer\Tests\Fixtures\Enum\BackedSuit', 'value'>")
20+
* @Serializer\Type("enum<JMS\Serializer\Tests\Fixtures\Enum\BackedSuit, 'value'>")
2121
*/
2222
public BackedSuit $backedValue;
2323

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

2931
/**
30-
* @Serializer\Type("array<enum<'JMS\Serializer\Tests\Fixtures\Enum\Suit'>>")
32+
* @Serializer\Type("array<enum<JMS\Serializer\Tests\Fixtures\Enum\Suit>>")
3133
*/
3234
public array $ordinaryArray;
3335

3436
/**
35-
* @Serializer\Type("array<enum<'JMS\Serializer\Tests\Fixtures\Enum\BackedSuit', 'value'>>")
37+
* @Serializer\Type("array<enum<JMS\Serializer\Tests\Fixtures\Enum\BackedSuit, 'value'>>")
3638
*/
3739
public array $backedArray;
3840

3941
/**
40-
* @Serializer\Type("array<enum<'JMS\Serializer\Tests\Fixtures\Enum\BackedSuit'>>")
42+
* @Serializer\Type("array<enum<JMS\Serializer\Tests\Fixtures\Enum\BackedSuit>>")
4143
*/
4244
public array $backedArrayWithoutParam;
4345

0 commit comments

Comments
 (0)