Skip to content

Commit 886f5d4

Browse files
committed
Initial Commit
Signed-off-by: RJ Garcia <rj@bighead.net>
0 parents  commit 886f5d4

File tree

6 files changed

+108
-0
lines changed

6 files changed

+108
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/composer.lock
2+
/vendor/

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Enum Normalizer
2+
3+
This contains a Symfony Denormalizer for the `myclabs/php-enum` `MyCLabs\Enum\Enum` class.
4+
5+
## Installation
6+
7+
Install with composer at `krak/enum-normalizer`.
8+
9+
## Usage
10+
11+
## EnumDenormalizer
12+
13+
```php
14+
$denormalizer = new Krak\EnumNormalizer\EnumDenormalizer();
15+
$enum = $denormalizer->denormalize('value', AcmeEnum::class);
16+
```
17+
18+
## Symfony Integration
19+
20+
Register the EnumNormalizerBundle in your kernel in `config/bundles.php`:
21+
22+
```php
23+
<?php
24+
25+
return [
26+
//...
27+
Krak\EnumNormalizer\Bridge\Symfony\EnumNormalizerBundle::class => ['all' => true],
28+
];
29+
```

composer.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "krak/enum-normalizer",
3+
"description": "Symfony Normalizer/Denormalizer for myclabs/enum",
4+
"type": "library",
5+
"homepage": "https://github.com/krakphp/enum-normalizer",
6+
"keywords": [
7+
"enum",
8+
"enum-normalizer",
9+
"symfony",
10+
"symfony4"
11+
],
12+
"license": "MIT",
13+
"authors": [
14+
{
15+
"name": "RJ Garcia",
16+
"email": "rj@bighead.net"
17+
}
18+
],
19+
"autoload": {
20+
"psr-4": {
21+
"Krak\\EnumNormalizer\\": "src"
22+
}
23+
},
24+
"require": {
25+
"symfony/serializer": "^3.4|^4.1"
26+
},
27+
"require-dev": {
28+
"symfony/dependency-injection": "^4.0",
29+
"symfony/http-kernel": "^4.0"
30+
}
31+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Krak\EnumNormalizer\Bridge\Symfony\DependencyInjection;
4+
5+
use Krak\EnumNormalizer\EnumDenormalizer;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
use Symfony\Component\DependencyInjection\Extension\Extension;
8+
9+
class EnumNormalizerExtension extends Extension
10+
{
11+
/**
12+
* Loads a specific configuration.
13+
*
14+
* @throws \InvalidArgumentException When provided tag is not defined in this extension
15+
*/
16+
public function load(array $configs, ContainerBuilder $container) {
17+
$container->register(EnumDenormalizer::class)->addTag('serializer.normalizer');
18+
}
19+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Krak\EnumNormalizer\Bridge\Symfony;
4+
5+
use Symfony\Component\HttpKernel\Bundle\Bundle;
6+
7+
class EnumNormalizerBundle extends Bundle
8+
{
9+
10+
}

src/EnumDenormalizer.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Krak\EnumNormalizer;
4+
5+
use MyCLabs\Enum\Enum;
6+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
7+
8+
class EnumDenormalizer implements DenormalizerInterface
9+
{
10+
public function denormalize($data, $class, $format = null, array $context = array()) {
11+
return new $class($data);
12+
}
13+
14+
public function supportsDenormalization($data, $type, $format = null) {
15+
return \is_subclass_of($type, Enum::class);
16+
}
17+
}

0 commit comments

Comments
 (0)