1 | <?php |
||
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) |
|
39 | |||
40 | /** |
||
41 | * {@inheritdoc} |
||
42 | */ |
||
43 | 5 | public function supportsNormalization($data, $format = null) |
|
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | public function normalize($object, $format = null, array $context = []) |
||
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() && $question->getId() != '71') { |
|
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) |
|
107 | } |
||
108 |