Skip to content

Commit 0173f83

Browse files
Neutron-ProNeutron-Pro
authored andcommitted
Initialize project for symfony serializer
0 parents commit 0173f83

File tree

5 files changed

+112
-0
lines changed

5 files changed

+112
-0
lines changed

.gitignore

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

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "neutronstars/symfony-normalizer-enum-php",
3+
"description": "Added an enumeration system as close as possible to PHP 8.1 for earlier versions.",
4+
"type": "library",
5+
"license": [ "Apache-2.0" ],
6+
"authors": [
7+
{
8+
"name": "NeutronStars",
9+
"email": "pro@neutronstars.fr"
10+
}
11+
],
12+
"minimum-stability": "stable",
13+
"require": {
14+
"php": "^7.1 | ^8.0",
15+
"neutronstars/enum": "^1.4.0",
16+
"symfony/serializer": "^4.0 | ^5.0"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"NeutronStars\\Symfony\\Enum\\Normalizer\\": "src/"
21+
}
22+
}
23+
}

src/AbstractEnumNormalizer.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace NeutronStars\Symfony\Enum\Normalizer;
6+
7+
use NeutronStars\Enum\Enum;
8+
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
9+
10+
class AbstractEnumNormalizer
11+
{
12+
/** @var string|Enum $className */
13+
protected $className;
14+
15+
/**
16+
* @param string|null $className
17+
* @throws NotNormalizableValueException
18+
*/
19+
public function __construct(?string $className = null)
20+
{
21+
$this->className = $className ?? Enum::class;
22+
if ($this->className !== Enum::class
23+
&& !is_subclass_of($this->className, Enum::class, true)) {
24+
throw new NotNormalizableValueException('Class ' . $this->className . ' is not supported !');
25+
}
26+
}
27+
}

src/EnumDenormalizer.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace NeutronStars\Symfony\Enum\Normalizer;
6+
7+
use NeutronStars\Enum\Enum;
8+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
9+
10+
class EnumDenormalizer extends AbstractEnumNormalizer implements DenormalizerInterface
11+
{
12+
public function supportsDenormalization($data, string $type, string $format = null): bool
13+
{
14+
return is_a($type, Enum::class, true);
15+
}
16+
17+
/**
18+
* @param mixed $data
19+
* @param string $type
20+
* @param string|null $format
21+
* @param array $context
22+
* @return object
23+
* @throws \ReflectionException
24+
*/
25+
public function denormalize($data, string $type, string $format = null, array $context = [])
26+
{
27+
return $this->className::from($data);
28+
}
29+
}

src/EnumNormalizer.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace NeutronStars\Symfony\Enum\Normalizer;
6+
7+
use NeutronStars\Enum\Enum;
8+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
9+
10+
class EnumNormalizer extends AbstractEnumNormalizer implements NormalizerInterface
11+
{
12+
public function supportsNormalization($data, string $format = null): bool
13+
{
14+
return is_a($data, Enum::class, true);
15+
}
16+
17+
/**
18+
* @param mixed $object
19+
* @param string|null $format
20+
* @param array $context
21+
* @return string|null
22+
*/
23+
public function normalize($object, string $format = null, array $context = []): ?string
24+
{
25+
if ($object instanceof Enum) {
26+
return (string) $object;
27+
}
28+
return null;
29+
}
30+
}

0 commit comments

Comments
 (0)