ValueObjectNormalizer::supportsDenormalization()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 3
dl 0
loc 3
rs 10
1
<?php
2
namespace Apie\ValueObjectPlugin\Normalizers;
3
4
use Apie\ValueObjects\ValueObjectInterface;
5
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
6
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
7
8
/**
9
 * Normalizer that normalizes value objects implementing ValueObjectInterface
10
 */
11
class ValueObjectNormalizer implements NormalizerInterface, DenormalizerInterface
12
{
13
    /**
14
     * @param mixed $data
15
     * @param string $class
16
     * @param string|null $format
17
     * @param array $context
18
     * @return ValueObjectInterface
19
     */
20
    public function denormalize($data, $class, $format = null, array $context = [])
21
    {
22
        return $class::fromNative($data);
23
    }
24
25
    /**
26
     * @param mixed $data
27
     * @param string $type
28
     * @param string|null $format
29
     * @return bool
30
     */
31
    public function supportsDenormalization($data, $type, $format = null)
32
    {
33
        return is_a($type, ValueObjectInterface::class, true);
34
    }
35
36
    /**
37
     * @param ValueObjectInterface $object
38
     * @param string|null $format
39
     * @param array $context
40
     * @return mixed
41
     */
42
    public function normalize($object, $format = null, array $context = [])
43
    {
44
        return $object->toNative();
45
    }
46
47
    /**
48
     * @param mixed $data
49
     * @param string|null $format
50
     * @return bool
51
     */
52
    public function supportsNormalization($data, $format = null)
53
    {
54
        return $data instanceof ValueObjectInterface;
55
    }
56
}
57