-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,2 @@ | ||
/composer.lock | ||
/vendor/ |
This file contains 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,29 @@ | ||
# Enum Normalizer | ||
|
||
This contains a Symfony Denormalizer for the `myclabs/php-enum` `MyCLabs\Enum\Enum` class. | ||
|
||
## Installation | ||
|
||
Install with composer at `krak/enum-normalizer`. | ||
|
||
## Usage | ||
|
||
## EnumDenormalizer | ||
|
||
```php | ||
$denormalizer = new Krak\EnumNormalizer\EnumDenormalizer(); | ||
$enum = $denormalizer->denormalize('value', AcmeEnum::class); | ||
``` | ||
|
||
## Symfony Integration | ||
|
||
Register the EnumNormalizerBundle in your kernel in `config/bundles.php`: | ||
|
||
```php | ||
<?php | ||
|
||
return [ | ||
//... | ||
Krak\EnumNormalizer\Bridge\Symfony\EnumNormalizerBundle::class => ['all' => true], | ||
]; | ||
``` |
This file contains 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,31 @@ | ||
{ | ||
"name": "krak/enum-normalizer", | ||
"description": "Symfony Normalizer/Denormalizer for myclabs/enum", | ||
"type": "library", | ||
"homepage": "https://github.com/krakphp/enum-normalizer", | ||
"keywords": [ | ||
"enum", | ||
"enum-normalizer", | ||
"symfony", | ||
"symfony4" | ||
], | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "RJ Garcia", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"Krak\\EnumNormalizer\\": "src" | ||
} | ||
}, | ||
"require": { | ||
"symfony/serializer": "^3.4|^4.1" | ||
}, | ||
"require-dev": { | ||
"symfony/dependency-injection": "^4.0", | ||
"symfony/http-kernel": "^4.0" | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Bridge/Symfony/DependencyInjection/EnumNormalizerExtension.php
This file contains 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,19 @@ | ||
<?php | ||
|
||
namespace Krak\EnumNormalizer\Bridge\Symfony\DependencyInjection; | ||
|
||
use Krak\EnumNormalizer\EnumDenormalizer; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Extension\Extension; | ||
|
||
class EnumNormalizerExtension extends Extension | ||
{ | ||
/** | ||
* Loads a specific configuration. | ||
* | ||
* @throws \InvalidArgumentException When provided tag is not defined in this extension | ||
*/ | ||
public function load(array $configs, ContainerBuilder $container) { | ||
$container->register(EnumDenormalizer::class)->addTag('serializer.normalizer'); | ||
} | ||
} |
This file contains 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,10 @@ | ||
<?php | ||
|
||
namespace Krak\EnumNormalizer\Bridge\Symfony; | ||
|
||
use Symfony\Component\HttpKernel\Bundle\Bundle; | ||
|
||
class EnumNormalizerBundle extends Bundle | ||
{ | ||
|
||
} |
This file contains 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,17 @@ | ||
<?php | ||
|
||
namespace Krak\EnumNormalizer; | ||
|
||
use MyCLabs\Enum\Enum; | ||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; | ||
|
||
class EnumDenormalizer implements DenormalizerInterface | ||
{ | ||
public function denormalize($data, $class, $format = null, array $context = array()) { | ||
return new $class($data); | ||
} | ||
|
||
public function supportsDenormalization($data, $type, $format = null) { | ||
return \is_subclass_of($type, Enum::class); | ||
} | ||
} |