Completed
Pull Request — dev (#40)
by
unknown
03:30
created

RequestNormalizer::normalize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 15
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 12
nc 2
nop 3
crap 6
1
<?php
2
3
namespace AppBundle\Serializer;
4
5
use AppBundle\Entity\FormRequest;
6
use Faker\Provider\DateTime;
7
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
8
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
9
use Symfony\Component\Serializer\Exception\LogicException;
10
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
11
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
12
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
13
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
14
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
15
16
class RequestNormalizer extends ObjectNormalizer
17
{
18
    /**
19
     * RequestNormalizer constructor.
20
     *
21
     * @param ClassMetadataFactoryInterface|null $classMDF
22
     * @param NameConverterInterface|null $nameCv
23
     * @param PropertyAccessorInterface|null $propAs
24
     * @param PropertyTypeExtractorInterface|null $propTE
25
     */
26
    public function __construct($classMDF, $nameCv, $propAs, $propTE)
27
    {
28
        parent::__construct($classMDF, $nameCv, $propAs, $propTE);
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function supportsNormalization($data, $format = null)
35
    {
36
        return $data instanceof FormRequest;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function normalize($object, $format = null, array $context = [])
43
    {
44
        if (!$this->serializer instanceof NormalizerInterface) {
45
            throw new LogicException('Cannot normalize attributes because injected serializer is not a normalizer');
46
        }
47
        /** @var FormRequest $request */
48
        $request = &$object;
49
50
        return $this->serializer->normalize(new \ArrayObject([
51
            'id' => $request->getId(),
52
            'type' => $request->getType(),
53
            'status' => $request->getStatus(),
54
            'date' => $request->getDate(),
55
            'createdAt' => $request->getCreatedAt(),
56
            'updatedAt' => $request->getUpdatedAt(),
57
        ]), $format, $context);
58
    }
59
60
    public function denormalize($data, $class, $format = null, array $context = array())
61
    {
62
        if (!$this->serializer instanceof DenormalizerInterface) {
63
            throw new LogicException('Cannot normalize attributes because injected serializer is not a normalizer');
64
        }
65
        /** @var FormRequest $request */
66
        $request = $context[ObjectNormalizer::OBJECT_TO_POPULATE];
67
68
        $request->setDate($data['date']);
69
70
        return $request;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function supportsDenormalization($data, $type, $format = null)
77
    {
78
        if ($type != FormRequest::class) {
79
            return false;
80
        }
81
82
        return true;
83
    }
84
}
85