Completed
Push — master ( c34cac...3c68a7 )
by Pieter
04:47
created

ValueObjectNormalizer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

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

4 Methods

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