Skip to content
This repository was archived by the owner on May 4, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-version: ['8.0', '8.1', '8.2', '8.3', '8.4', '8.5']
php-version: ['8.1', '8.2', '8.3', '8.4', '8.5']
include:
- php-version: '8.0'
- php-version: '8.1'
composer-flags: '--prefer-stable --prefer-lowest'
steps:
- name: Check out code into the workspace
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

# 3.x

# 3.1.0 (unreleased)

* Add enum support
* Drop support for PHP 8.0

# 3.0.0

* Update to liip/metadata-parser 2.x. Most notable changes:
Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
"issues": "https://github.com/liip/serializer/issues"
},
"require": {
"php": "^8.0",
"php": "^8.1",
"ext-json": "*",
"liip/metadata-parser": "^2.0",
"liip/metadata-parser": "^2.1.0",
"pnz/json-exception": "^1.0",
"symfony/filesystem": "^4.4 || ^5.0 || ^6.0 || ^7.0 || ^8.0",
"symfony/finder": "^4.4 || ^5.0 || ^6.0 || ^7.0 || ^8.0",
Expand All @@ -25,12 +25,12 @@
},
"require-dev": {
"doctrine/collections": "^1.6",
"friendsofphp/php-cs-fixer": "^3.90.0",
"friendsofphp/php-cs-fixer": "^3.94.2",
"jms/serializer": "^1.13 || ^2 || ^3",
"phpstan/phpstan": "^1.0",
"phpstan/phpstan-phpunit": "^1.3",
"phpstan/phpstan": "^2.1.44",
"phpstan/phpstan-phpunit": "^2.0.16",
"phpunit/phpunit": "^9.6",
"rector/rector": "^1.2.1"
"rector/rector": "^2.3.9"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 2 additions & 0 deletions phpstan.tests.neon
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
parameters:
excludePaths:
- *serialize_Tests_Liip_Serializer_Fixtures_*
# Excluded until the minimum version required from jms/serializer is set to >= 3.31.0
- tests/Fixtures/ComplexUnionTyping.php
2 changes: 1 addition & 1 deletion src/Configuration/GeneratorConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function addClassToGenerate(ClassToGenerate $classToGenerate): void
}

/**
* @return string[]
* @return list<string>
*/
public function getDefaultVersions(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Configuration/GroupCombination.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct(
}

/**
* @return string[]
* @return list<string>
*/
public function getGroups(): array
{
Expand Down
6 changes: 3 additions & 3 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class Context
private ?string $version = null;

/**
* @var string[]
* @var list<string>
*/
private array $groups = [];

Expand All @@ -21,7 +21,7 @@ public function __construct()
}

/**
* @return string[]
* @return list<string>
*/
public function getGroups(): array
{
Expand All @@ -33,7 +33,7 @@ public function getGroups(): array
*/
public function setGroups(array $groups): self
{
$this->groups = array_unique($groups);
$this->groups = array_values(array_unique($groups));
sort($this->groups);

return $this;
Expand Down
22 changes: 21 additions & 1 deletion src/DeserializerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Liip\MetadataParser\Metadata\PropertyType;
use Liip\MetadataParser\Metadata\PropertyTypeClass;
use Liip\MetadataParser\Metadata\PropertyTypeDateTime;
use Liip\MetadataParser\Metadata\PropertyTypeEnum;
use Liip\MetadataParser\Metadata\PropertyTypeIterable;
use Liip\MetadataParser\Metadata\PropertyTypePrimitive;
use Liip\MetadataParser\Metadata\PropertyTypeUnion;
Expand Down Expand Up @@ -242,7 +243,7 @@ private function generateInnerCodeForFieldType(
return $this->generateCodeForArray($type, $arrayPath, $modelPropertyPath, $stack);

case $type instanceof PropertyTypeDateTime:
$formats = $type->getDeserializeFormats() ?: (\is_string($type->getFormat()) ? [$type->getFormat()] : $type->getFormat());
$formats = $type->getDeserializeFormats() ?: (null !== $type->getFormat() ? [$type->getFormat()] : null);
if (null !== $formats) {
return $this->templating->renderAssignDateTimeFromFormat($type->isImmutable(), (string) $modelPropertyPath, (string) $arrayPath, $formats, $type->getZone());
}
Expand All @@ -256,6 +257,9 @@ private function generateInnerCodeForFieldType(
case $type instanceof PropertyTypeUnknown:
return $this->templating->renderAssignJsonDataToField((string) $modelPropertyPath, (string) $arrayPath);

case $type instanceof PropertyTypeEnum:
return $this->generateCodeForEnumField($type, $modelPropertyPath, $arrayPath);

case $type instanceof PropertyTypeClass:
return $this->generateCodeForClass($type->getClassMetadata(), $arrayPath, $modelPropertyPath, $stack);

Expand Down Expand Up @@ -346,6 +350,10 @@ private function generateCodeForArray(
$innerCode = $this->generateCodeForArray($subType, $arrayPropertyPath, $modelPropertyPath, $stack);
break;

case $subType instanceof PropertyTypeEnum:
$innerCode = $this->generateCodeForEnumField($subType, $modelPropertyPath, $arrayPropertyPath);
break;

case $subType instanceof PropertyTypeClass:
$innerCode = $this->generateCodeForClass($subType->getClassMetadata(), $arrayPropertyPath, $modelPropertyPath, $stack);
break;
Expand Down Expand Up @@ -387,6 +395,18 @@ private function generateCodeForArrayCollection(
return $innerCode.$this->templating->renderArrayCollection((string) $modelPath, (string) $tmpVariable);
}

private function generateCodeForEnumField(
PropertyTypeEnum $type,
ModelPath $modelPath,
ArrayPath $arrayPath,
): string {
if ($type->shouldSerializeAsValue()) {
return $this->templating->renderAssignBackedEnum($type->getClassName(), (string) $modelPath, (string) $arrayPath);
}

return $this->templating->renderAssignUnitEnum($type->getClassName(), (string) $modelPath, (string) $arrayPath);
}

/**
* @param list<class-string> $classesToGenerate
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Recursion.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private static function getClassNameFromProperty(PropertyMetadata $propertyMetad
$type = $type->getLeafType();
}

if (!($type instanceof PropertyTypeClass)) {
if (!$type instanceof PropertyTypeClass) {
return null;
}

Expand Down
10 changes: 10 additions & 0 deletions src/SerializerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Liip\MetadataParser\Metadata\PropertyType;
use Liip\MetadataParser\Metadata\PropertyTypeClass;
use Liip\MetadataParser\Metadata\PropertyTypeDateTime;
use Liip\MetadataParser\Metadata\PropertyTypeEnum;
use Liip\MetadataParser\Metadata\PropertyTypeIterable;
use Liip\MetadataParser\Metadata\PropertyTypePrimitive;
use Liip\MetadataParser\Metadata\PropertyTypeUnion;
Expand Down Expand Up @@ -229,6 +230,11 @@ private function generateCodeForFieldType(
// for arrays of scalars, copy the field even when its an empty array
return $this->templating->renderAssign($fieldPath, $modelPropertyPath);

case $type instanceof PropertyTypeEnum:
$valueAccess = $type->shouldSerializeAsValue() ? '->value' : '->name';

return $this->templating->renderAssign($fieldPath, $modelPropertyPath.$valueAccess);

case $type instanceof PropertyTypeClass:
return $this->generateCodeForClass($type->getClassMetadata(), $apiVersion, $serializerGroups, $fieldPath, $modelPropertyPath, $stack);

Expand Down Expand Up @@ -268,6 +274,10 @@ private function generateCodeForArray(
$innerCode = $this->generateCodeForArray($subType, $apiVersion, $serializerGroups, $arrayPath.'['.$index.']', $modelPath.'['.$index.']', $stack);
break;

case $subType instanceof PropertyTypeEnum:
$innerCode = $this->generateCodeForFieldType($subType, $apiVersion, $serializerGroups, $arrayPath.'['.$index.']', $modelPath.'['.$index.']', $stack);
break;

case $subType instanceof PropertyTypeClass:
$innerCode = $this->generateCodeForClass($subType->getClassMetadata(), $apiVersion, $serializerGroups, $arrayPath.'['.$index.']', $modelPath.'['.$index.']', $stack);
break;
Expand Down
35 changes: 35 additions & 0 deletions src/Template/Deserialization.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,23 @@ function {{functionName}}(array {{jsonPath}}): {{className}}
private const TMPL_UNSET = <<<'EOT'
unset({{variableNames|join(', ')}});

EOT;

private const TMPL_ASSIGN_BACKED_ENUM = <<<'EOT'
{{modelPath}} = {{enumClass}}::from({{jsonPath}});

EOT;

private const TMPL_ASSIGN_UNIT_ENUM = <<<'EOT'
{{modelPath}} = (static function (string $n): {{enumClass}} {
foreach ({{enumClass}}::cases() as $case) {
if ($case->name === $n) {
return $case;
}
}
throw new \ValueError("'$n' is not a valid name for enum {{enumClass}}");
})({{jsonPath}});

EOT;

private const TMPL_EXTRACT = '{{jsonPath}} ?? {{default}}';
Expand Down Expand Up @@ -315,6 +332,24 @@ public function renderAssignDateTimeFromFormat(bool $immutable, string $modelPat
]);
}

public function renderAssignBackedEnum(string $enumClass, string $modelPath, string $jsonPath): string
{
return $this->render(self::TMPL_ASSIGN_BACKED_ENUM, [
'enumClass' => $enumClass,
'modelPath' => $modelPath,
'jsonPath' => $jsonPath,
]);
}

public function renderAssignUnitEnum(string $enumClass, string $modelPath, string $jsonPath): string
{
return $this->render(self::TMPL_ASSIGN_UNIT_ENUM, [
'enumClass' => $enumClass,
'modelPath' => $modelPath,
'jsonPath' => $jsonPath,
]);
}

public function renderExtract(string $jsonPath, string $default = 'null'): string
{
return $this->render(self::TMPL_EXTRACT, [
Expand Down
11 changes: 11 additions & 0 deletions tests/Fixtures/BackedIntEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Tests\Liip\Serializer\Fixtures;

enum BackedIntEnum: int
{
case Low = 1;
case High = 2;
}
11 changes: 11 additions & 0 deletions tests/Fixtures/BackedStringEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Tests\Liip\Serializer\Fixtures;

enum BackedStringEnum: string
{
case Hearts = 'H';
case Diamonds = 'D';
}
27 changes: 27 additions & 0 deletions tests/Fixtures/EnumModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Tests\Liip\Serializer\Fixtures;

use JMS\Serializer\Annotation as Serializer;

class EnumModel
{
#[Serializer\Type('enum<'.BackedStringEnum::class.'>')]
public ?BackedStringEnum $backedString = null;

public ?BackedStringEnum $backedStringWithoutAttribute = null;

#[Serializer\Type('enum<'.BackedStringEnum::class.", 'name'>")]
public ?BackedStringEnum $backedStringAsName = null;

#[Serializer\Type('enum<'.BackedIntEnum::class.'>')]
public ?BackedIntEnum $backedInt = null;

#[Serializer\Type('enum<'.UnitEnum::class.'>')]
public ?UnitEnum $unit = null;

#[Serializer\Type('array<enum<'.BackedStringEnum::class.'>>')]
public ?array $backedStringArray = null;
}
11 changes: 11 additions & 0 deletions tests/Fixtures/UnitEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Tests\Liip\Serializer\Fixtures;

enum UnitEnum
{
case North;
case South;
}
Loading
Loading