Skip to content

Commit

Permalink
Merge pull request #53 from marcimat/issue_50_enums
Browse files Browse the repository at this point in the history
Feat #50 (enums for PHP 8.1)
  • Loading branch information
jrbasso authored Jul 13, 2023
2 parents ef82b51 + 63a45fa commit 31d23c1
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added
- Support of UnitEnum and BackedEnum deserialization (PHP > 8.1)
### Fixed
- Fixed deprecated with DateTimeImmutable deserialization with PHP 8.2

Expand Down
4 changes: 4 additions & 0 deletions src/JsonSerializer/JsonSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,10 @@ protected function unserializeObject($value)
return $obj;
}

if (is_subclass_of($className, \UnitEnum::class)) {
return constant("$className::{$value['name']}");
}

if (!$this->isSplList($className)) {
$ref = new ReflectionClass($className);
$obj = $ref->newInstanceWithoutConstructor();
Expand Down
82 changes: 82 additions & 0 deletions tests/JsonSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,88 @@ public function testUnserializeObjects()
$this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportClasses\EmptyClass', $array['instance']);
}


/**
* Test serialization of Enums
*
* @return void
*/
public function testSerializeEnums()
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped("Enums are only available since PHP 8.1");
}

$unitEnum = SupportEnums\MyUnitEnum::Hearts;
$expected = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyUnitEnum","name":"Hearts"}';
$this->assertSame($expected, $this->serializer->serialize($unitEnum));

$backedEnum = SupportEnums\MyBackedEnum::Hearts;
$expected = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyBackedEnum","name":"Hearts","value":"H"}';
$this->assertSame($expected, $this->serializer->serialize($backedEnum));
}

/**
* Test unserialization of Enums
*
* @return void
*/
public function testUnserializeEnums()
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped("Enums are only available since PHP 8.1");
}

$serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyUnitEnum","name":"Hearts"}';
$obj = $this->serializer->unserialize($serialized);
$this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportEnums\MyUnitEnum', $obj);
$this->assertSame(SupportEnums\MyUnitEnum::Hearts, $obj);

$serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyBackedEnum","name":"Hearts","value":"H"}';
$obj = $this->serializer->unserialize($serialized);
$this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportEnums\MyBackedEnum', $obj);
$this->assertSame(SupportEnums\MyBackedEnum::Hearts, $obj);

// wrong value of BackedEnum is ignored
$serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyBackedEnum","name":"Hearts","value":"S"}';
$obj = $this->serializer->unserialize($serialized);
$this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportEnums\MyBackedEnum', $obj);
$this->assertSame(SupportEnums\MyBackedEnum::Hearts, $obj);
$this->assertSame(SupportEnums\MyBackedEnum::Hearts->value, $obj->value);
}

/**
* Test unserialization of wrong UnitEnum
*
* @return void
*/
public function testUnserializeWrongUnitEnum() {
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped("Enums are only available since PHP 8.1");
}

// bad case generate Error
$serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyUnitEnum","name":"Circles"}';
$this->expectException(\Error::class);
$this->serializer->unserialize($serialized);
}

/**
* Test unserialization of wrong BackedEnum
*
* @return void
*/
public function testUnserializeWrongBackedEnum() {
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped("Enums are only available since PHP 8.1");
}

// bad case generate Error
$serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyBackedEnum","name":"Circles","value":"C"}';
$this->expectException(\Error::class);
$this->serializer->unserialize($serialized);
}

/**
* Test serialization of objects using the custom serializers
*
Expand Down
11 changes: 11 additions & 0 deletions tests/SupportEnums/MyBackedEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Zumba\JsonSerializer\Test\SupportEnums;

enum MyBackedEnum: string
{
case Hearts = 'H';
case Diamonds = 'D';
case Clubs = 'C';
case Spades = 'S';
}
11 changes: 11 additions & 0 deletions tests/SupportEnums/MyUnitEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Zumba\JsonSerializer\Test\SupportEnums;

enum MyUnitEnum
{
case Hearts;
case Diamonds;
case Clubs;
case Spades;
}

0 comments on commit 31d23c1

Please sign in to comment.