Passed
Push — master ( dd03d9...cef246 )
by Kévin
04:20 queued 30s
created

UuidNormalizer::denormalize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 4
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Bridge\RamseyUuid\Identifier\Normalizer;
15
16
use ApiPlatform\Core\Exception\InvalidIdentifierException;
17
use Ramsey\Uuid\Exception\InvalidUuidStringException;
18
use Ramsey\Uuid\Uuid;
19
use Ramsey\Uuid\UuidInterface;
20
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
21
22
/**
23
 * Denormalizes an UUID string to an instance of Ramsey\Uuid.
24
 *
25
 * @author Antoine Bluchet <[email protected]>
26
 */
27
final class UuidNormalizer implements DenormalizerInterface
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function denormalize($data, $class, $format = null, array $context = [])
33
    {
34
        try {
35
            return Uuid::fromString($data);
36
        } catch (InvalidUuidStringException $e) {
37
            throw new InvalidIdentifierException($e->getMessage());
38
        }
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function supportsDenormalization($data, $type, $format = null)
45
    {
46
        return is_a($type, UuidInterface::class, true);
47
    }
48
}
49