-
Notifications
You must be signed in to change notification settings - Fork 8
/
UuidDenormalizer.php
46 lines (39 loc) · 1.04 KB
/
UuidDenormalizer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
namespace GBProd\UuidNormalizer;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
/**
* Normalizer for Uuid
*
* @author gbprod <contact@gb-prod.fr>
*/
class UuidDenormalizer implements DenormalizerInterface
{
/**
* @inheritdoc
*/
public function denormalize($data, $class, $format = null, array $context = array())
{
if (!$this->isValid($data)) {
throw new UnexpectedValueException('Expected a valid Uuid.');
}
if (null === $data) {
return null;
}
return Uuid::fromString($data);
}
/**
* @inheritdoc
*/
public function supportsDenormalization($data, $type, $format = null)
{
return (Uuid::class === $type || UuidInterface::class === $type);
}
private function isValid($data)
{
return $data === null
|| (is_string($data) && Uuid::isValid($data));
}
}