1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Serializer; |
4
|
|
|
|
5
|
|
|
use AppBundle\Entity\FormRequest; |
6
|
|
|
use AppBundle\Exception\JsonHttpException; |
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
|
5 |
|
public function __construct($classMDF, $nameCv, $propAs, $propTE) |
27
|
|
|
{ |
28
|
5 |
|
parent::__construct($classMDF, $nameCv, $propAs, $propTE); |
29
|
5 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
5 |
|
public function supportsNormalization($data, $format = null) |
35
|
|
|
{ |
36
|
5 |
|
return $data instanceof FormRequest; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
1 |
|
public function normalize($object, $format = null, array $context = []) |
43
|
|
|
{ |
44
|
1 |
|
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
|
1 |
|
$request = &$object; |
49
|
|
|
|
50
|
1 |
|
return $this->serializer->normalize(new \ArrayObject([ |
51
|
1 |
|
'id' => $request->getId(), |
52
|
1 |
|
'type' => $request->getType(), |
53
|
1 |
|
'status' => $request->getStatus(), |
54
|
1 |
|
'reason' => $request->getReason(), |
55
|
1 |
|
'date' => $request->getDate(), |
56
|
1 |
|
'createdAt' => $request->getCreatedAt(), |
57
|
1 |
|
'updatedAt' => $request->getUpdatedAt(), |
58
|
|
|
]), $format, $context); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
63
|
|
|
* |
64
|
|
|
* @param mixed $data |
65
|
|
|
* @param string $class |
66
|
|
|
* @param null $format |
67
|
|
|
* @param array $context |
68
|
|
|
* |
69
|
|
|
* @return FormRequest|JsonHttpException |
70
|
|
|
*/ |
71
|
|
|
public function denormalize($data, $class, $format = null, array $context = array()) |
72
|
|
|
{ |
73
|
|
|
if (!$this->serializer instanceof DenormalizerInterface) { |
74
|
|
|
throw new LogicException('Cannot normalize attributes because injected serializer is not a normalizer'); |
75
|
|
|
} |
76
|
|
|
/** @var FormRequest $request */ |
77
|
|
|
$request = $context[ObjectNormalizer::OBJECT_TO_POPULATE]; |
78
|
|
|
|
79
|
|
|
if (array_key_exists('date', $data)) { |
80
|
|
|
$request->setDate($data['date']); |
81
|
|
|
} |
82
|
|
|
if (isset($data['reason'])) { |
83
|
|
|
$request->setReason($data['reason']); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $request; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
1 |
|
public function supportsDenormalization($data, $type, $format = null) |
93
|
|
|
{ |
94
|
1 |
|
if ($type != FormRequest::class) { |
95
|
1 |
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|