ValueObjectNormalizer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 5
c 1
b 0
f 1
dl 0
loc 44
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 3 1
A supportsNormalization() 0 3 1
A denormalize() 0 3 1
A supportsDenormalization() 0 3 1
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