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