|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AppBundle\Serializer; |
|
4
|
|
|
|
|
5
|
|
|
use AppBundle\Entity\Survey\Survey; |
|
6
|
|
|
use AppBundle\Entity\Survey\SurveyAnswer; |
|
7
|
|
|
use AppBundle\Entity\Survey\SurveyQuestion; |
|
8
|
|
|
use Doctrine\Bundle\DoctrineBundle\Registry; |
|
9
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
|
10
|
|
|
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface; |
|
11
|
|
|
use Symfony\Component\Serializer\Exception\LogicException; |
|
12
|
|
|
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; |
|
13
|
|
|
use Symfony\Component\Serializer\NameConverter\NameConverterInterface; |
|
14
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
|
15
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
|
16
|
|
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; |
|
17
|
|
|
|
|
18
|
|
|
class SurveyAnswerNormalizer extends ObjectNormalizer |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var Registry |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $doctrine; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* SurveyAnswerNormalizer constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param ClassMetadataFactoryInterface|null $classMDF |
|
29
|
|
|
* @param NameConverterInterface|null $nameCv |
|
30
|
|
|
* @param PropertyAccessorInterface|null $propAs |
|
31
|
|
|
* @param PropertyTypeExtractorInterface|null $propTE |
|
32
|
|
|
* @param Registry $doctrine |
|
33
|
|
|
*/ |
|
34
|
5 |
|
public function __construct($classMDF, $nameCv, $propAs, $propTE, Registry $doctrine) |
|
35
|
|
|
{ |
|
36
|
5 |
|
parent::__construct($classMDF, $nameCv, $propAs, $propTE); |
|
37
|
5 |
|
$this->doctrine = $doctrine; |
|
38
|
5 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
5 |
|
public function supportsNormalization($data, $format = null) |
|
44
|
|
|
{ |
|
45
|
5 |
|
return $data instanceof SurveyAnswer; |
|
46
|
|
|
} |
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
|
|
public function normalize($object, $format = null, array $context = []) |
|
51
|
|
|
{ |
|
52
|
|
|
if (!$this->serializer instanceof NormalizerInterface) { |
|
53
|
|
|
throw new LogicException('Cannot normalize attributes because injected serializer is not a normalizer'); |
|
54
|
|
|
} |
|
55
|
|
|
/** @var SurveyAnswer $answer */ |
|
56
|
|
|
$answer = &$object; |
|
57
|
|
|
|
|
58
|
|
|
return $this->serializer->normalize(new \ArrayObject([ |
|
59
|
|
|
'questionId' => $answer->getQuestion()->getId(), |
|
60
|
|
|
'content' => $answer->getContent(), |
|
61
|
|
|
]), $format, $context); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
1 |
|
public function denormalize($data, $class, $format = null, array $context = array()) |
|
68
|
|
|
{ |
|
69
|
1 |
|
if (!$this->serializer instanceof DenormalizerInterface) { |
|
70
|
|
|
throw new LogicException('Cannot normalize attributes because injected serializer is not a normalizer'); |
|
71
|
|
|
} |
|
72
|
|
|
/** @var Survey $survey */ |
|
73
|
1 |
|
$survey = $context[ObjectNormalizer::OBJECT_TO_POPULATE]; |
|
74
|
|
|
|
|
75
|
1 |
|
if (!array_key_exists('questionId', $data) || !array_key_exists('content', $data)) { |
|
76
|
|
|
throw new LogicException('Wrong json consruction'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
$newAnswer = new SurveyAnswer(); |
|
80
|
1 |
|
$question = $this->doctrine->getManager()->getRepository(SurveyQuestion::class) |
|
81
|
1 |
|
->find($data['questionId']); |
|
82
|
1 |
|
if (!in_array($question, $survey->getQuestions())) { |
|
83
|
|
|
throw new LogicException('Wrong question id'); |
|
84
|
|
|
} |
|
85
|
1 |
|
if ($question->getVariants()) { |
|
86
|
1 |
|
if (!in_array($data['content'], $question->getVariants())) { |
|
87
|
|
|
throw new LogicException('Wrong variants'); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
1 |
|
$newAnswer->setQuestion($question); |
|
91
|
1 |
|
$newAnswer->setContent($data['content']); |
|
92
|
|
|
|
|
93
|
1 |
|
return $newAnswer; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* {@inheritdoc} |
|
98
|
|
|
*/ |
|
99
|
1 |
|
public function supportsDenormalization($data, $type, $format = null) |
|
100
|
|
|
{ |
|
101
|
1 |
|
if ($type != SurveyAnswer::class) { |
|
102
|
1 |
|
return false; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
1 |
|
return true; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|