-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Add support of PHP backed enumerations #40830
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
Merged
fabpot
merged 1 commit into
symfony:5.4
from
alexandre-daubois:feat-backed-enum-normalizer
Jul 5, 2021
Merged
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 |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
5.4 | ||
--- | ||
|
||
* Add support of PHP backed enumerations | ||
|
||
5.3 | ||
--- | ||
|
||
|
85 changes: 85 additions & 0 deletions
85
src/Symfony/Component/Serializer/Normalizer/BackedEnumNormalizer.php
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,85 @@ | ||
<?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\Serializer\Normalizer; | ||
|
||
use Symfony\Component\Serializer\Exception\InvalidArgumentException; | ||
use Symfony\Component\Serializer\Exception\NotNormalizableValueException; | ||
|
||
/** | ||
* Normalizes a {@see \BackedEnum} enumeration to a string or an integer. | ||
* | ||
* @author Alexandre Daubois <alex.daubois@gmail.com> | ||
*/ | ||
final class BackedEnumNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws InvalidArgumentException | ||
* | ||
* @return int|string | ||
*/ | ||
public function normalize($object, $format = null, array $context = []) | ||
{ | ||
if (!$object instanceof \BackedEnum) { | ||
throw new InvalidArgumentException('The data must belong to a backed enumeration.'); | ||
} | ||
|
||
return $object->value; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsNormalization($data, $format = null) | ||
{ | ||
return $data instanceof \BackedEnum; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws NotNormalizableValueException | ||
*/ | ||
public function denormalize($data, $type, $format = null, array $context = []) | ||
{ | ||
if (!is_subclass_of($type, \BackedEnum::class)) { | ||
throw new InvalidArgumentException('The data must belong to a backed enumeration.'); | ||
} | ||
|
||
if (!\is_int($data) && !\is_string($data)) { | ||
throw new NotNormalizableValueException('The data is neither an integer nor a string, you should pass an integer or a string that can be parsed as an enumeration case of type '.$type.'.'); | ||
} | ||
|
||
try { | ||
return $type::from($data); | ||
} catch (\ValueError $e) { | ||
throw new NotNormalizableValueException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsDenormalization($data, $type, $format = null) | ||
{ | ||
return is_subclass_of($type, \BackedEnum::class); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function hasCacheableSupportsMethod(): bool | ||
{ | ||
return true; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/Symfony/Component/Serializer/Tests/Fixtures/IntegerBackedEnumDummy.php
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,8 @@ | ||
<?php | ||
|
||
derrabus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
namespace Symfony\Component\Serializer\Tests\Fixtures; | ||
|
||
enum IntegerBackedEnumDummy: int | ||
{ | ||
case SUCCESS = 200; | ||
derrabus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/Symfony/Component/Serializer/Tests/Fixtures/StringBackedEnumDummy.php
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,8 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Serializer\Tests\Fixtures; | ||
|
||
enum StringBackedEnumDummy: string | ||
{ | ||
case GET = 'GET'; | ||
} |
8 changes: 8 additions & 0 deletions
8
src/Symfony/Component/Serializer/Tests/Fixtures/UnitEnumDummy.php
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,8 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Serializer\Tests\Fixtures; | ||
|
||
enum UnitEnumDummy | ||
{ | ||
case GET; | ||
} |
143 changes: 143 additions & 0 deletions
143
src/Symfony/Component/Serializer/Tests/Normalizer/BackedEnumNormalizerTest.php
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,143 @@ | ||
<?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\Serializer\Tests\Normalizer; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Serializer\Exception\InvalidArgumentException; | ||
use Symfony\Component\Serializer\Exception\NotNormalizableValueException; | ||
use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer; | ||
use Symfony\Component\Serializer\Tests\Fixtures\IntegerBackedEnumDummy; | ||
use Symfony\Component\Serializer\Tests\Fixtures\StringBackedEnumDummy; | ||
use Symfony\Component\Serializer\Tests\Fixtures\UnitEnumDummy; | ||
|
||
/** | ||
* @author Alexandre Daubois <alex.daubois@gmail.com> | ||
*/ | ||
class BackedEnumNormalizerTest extends TestCase | ||
{ | ||
/** | ||
* @var BackedEnumNormalizer | ||
*/ | ||
private $normalizer; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->normalizer = new BackedEnumNormalizer(); | ||
} | ||
|
||
/** | ||
* @requires PHP 8.1 | ||
*/ | ||
jderusse marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public function testSupportsNormalization() | ||
{ | ||
$this->assertTrue($this->normalizer->supportsNormalization(StringBackedEnumDummy::GET)); | ||
$this->assertTrue($this->normalizer->supportsNormalization(IntegerBackedEnumDummy::SUCCESS)); | ||
$this->assertFalse($this->normalizer->supportsNormalization(UnitEnumDummy::GET)); | ||
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass())); | ||
} | ||
|
||
/** | ||
* @requires PHP 8.1 | ||
*/ | ||
public function testNormalize() | ||
{ | ||
$this->assertSame('GET', $this->normalizer->normalize(StringBackedEnumDummy::GET)); | ||
$this->assertSame(200, $this->normalizer->normalize(IntegerBackedEnumDummy::SUCCESS)); | ||
} | ||
|
||
/** | ||
* @requires PHP 8.1 | ||
*/ | ||
public function testNormalizeBadObjectTypeThrowsException() | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->normalizer->normalize(new \stdClass()); | ||
} | ||
|
||
/** | ||
* @requires PHP 8.1 | ||
*/ | ||
public function testSupportsDenormalization() | ||
{ | ||
$this->assertTrue($this->normalizer->supportsDenormalization(null, StringBackedEnumDummy::class)); | ||
$this->assertTrue($this->normalizer->supportsDenormalization(null, IntegerBackedEnumDummy::class)); | ||
$this->assertFalse($this->normalizer->supportsDenormalization(null, UnitEnumDummy::class)); | ||
$this->assertFalse($this->normalizer->supportsDenormalization(null, \stdClass::class)); | ||
} | ||
|
||
/** | ||
* @requires PHP 8.1 | ||
*/ | ||
public function testDenormalize() | ||
{ | ||
$this->assertSame(StringBackedEnumDummy::GET, $this->normalizer->denormalize('GET', StringBackedEnumDummy::class)); | ||
$this->assertSame(IntegerBackedEnumDummy::SUCCESS, $this->normalizer->denormalize(200, IntegerBackedEnumDummy::class)); | ||
} | ||
|
||
/** | ||
* @requires PHP 8.1 | ||
*/ | ||
public function testDenormalizeNullValueThrowsException() | ||
{ | ||
$this->expectException(NotNormalizableValueException::class); | ||
$this->normalizer->denormalize(null, StringBackedEnumDummy::class); | ||
} | ||
|
||
/** | ||
* @requires PHP 8.1 | ||
*/ | ||
public function testDenormalizeBooleanValueThrowsException() | ||
{ | ||
$this->expectException(NotNormalizableValueException::class); | ||
$this->normalizer->denormalize(true, StringBackedEnumDummy::class); | ||
} | ||
|
||
/** | ||
* @requires PHP 8.1 | ||
*/ | ||
public function testDenormalizeObjectThrowsException() | ||
{ | ||
$this->expectException(NotNormalizableValueException::class); | ||
$this->normalizer->denormalize(new \stdClass(), StringBackedEnumDummy::class); | ||
} | ||
|
||
/** | ||
* @requires PHP 8.1 | ||
*/ | ||
public function testDenormalizeBadBackingValueThrowsException() | ||
{ | ||
$this->expectException(NotNormalizableValueException::class); | ||
$this->expectExceptionMessage('"POST" is not a valid backing value for enum "'.StringBackedEnumDummy::class.'"'); | ||
$this->normalizer->denormalize('POST', StringBackedEnumDummy::class); | ||
} | ||
|
||
public function testNormalizeShouldThrowExceptionForNonEnumObjects() | ||
{ | ||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectExceptionMessage('The data must belong to a backed enumeration.'); | ||
|
||
$this->normalizer->normalize(\stdClass::class); | ||
} | ||
|
||
public function testDenormalizeShouldThrowExceptionForNonEnumObjects() | ||
{ | ||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectExceptionMessage('The data must belong to a backed enumeration.'); | ||
|
||
$this->normalizer->denormalize('GET', \stdClass::class); | ||
} | ||
|
||
public function testSupportsNormalizationShouldFailOnAnyPHPVersionForNonEnumObjects() | ||
{ | ||
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass())); | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.