supportsNormalization()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Bvon;
4
5
use BaseValueObject\Scalar\ValueObject;
6
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
7
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
8
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
9
10
class BasicValueObjectNormalizer implements
11
    NormalizerInterface,
12
    DenormalizerInterface
13
{
14
    /**
15
     * {@inheritdoc}
16
     */
17 9
    public function normalize($object, $format = null, array $context = [])
18
    {
19 9
        if (!$object instanceof ValueObject) {
20 3
            throw new InvalidArgumentException(
21 3
                sprintf(
22 3
                    'The object must implement the "%s".',
23 3
                    ValueObject::class
24
                )
25
            );
26
        }
27
28 6
        return $object->value();
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 12
    public function supportsNormalization($data, $format = null)
35
    {
36 12
        return $data instanceof ValueObject;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 6
    public function denormalize(
43
        $data,
44
        $class,
45
        $format = null,
46
        array $context = []
47
    ) {
48 6
        return new $class($data);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 6
    public function supportsDenormalization($data, $type, $format = null)
55
    {
56 6
        return (new \ReflectionClass($type))
57 6
            ->implementsInterface(ValueObject::class);
58
    }
59
}
60