Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
Signed-off-by: RJ Garcia <[email protected]>
  • Loading branch information
ragboyjr committed Oct 21, 2018
0 parents commit 886f5d4
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/composer.lock
/vendor/
29 changes: 29 additions & 0 deletions README.md
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],
];
```
31 changes: 31 additions & 0 deletions composer.json
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 src/Bridge/Symfony/DependencyInjection/EnumNormalizerExtension.php
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');
}
}
10 changes: 10 additions & 0 deletions src/Bridge/Symfony/EnumNormalizerBundle.php
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
{

}
17 changes: 17 additions & 0 deletions src/EnumDenormalizer.php
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);
}
}

0 comments on commit 886f5d4

Please sign in to comment.