1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace VSV\GVQ_API\Question\Serializers; |
4
|
|
|
|
5
|
|
|
use Ramsey\Uuid\Uuid; |
6
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
7
|
|
|
use VSV\GVQ_API\Question\Models\Answer; |
8
|
|
|
use VSV\GVQ_API\Question\Models\Answers; |
9
|
|
|
use VSV\GVQ_API\Question\Models\Category; |
10
|
|
|
use VSV\GVQ_API\Question\Models\Question; |
11
|
|
|
use VSV\GVQ_API\Common\ValueObjects\Language; |
12
|
|
|
use VSV\GVQ_API\Common\ValueObjects\NotEmptyString; |
13
|
|
|
use VSV\GVQ_API\Question\ValueObjects\Year; |
14
|
|
|
|
15
|
|
|
class QuestionDenormalizer implements DenormalizerInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var CategoryDenormalizer |
19
|
|
|
*/ |
20
|
|
|
private $categoryDenormalizer; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var AnswerDenormalizer |
24
|
|
|
*/ |
25
|
|
|
private $answerDenormalizer; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param CategoryDenormalizer $categoryDenormalizer |
29
|
|
|
* @param AnswerDenormalizer $answerDenormalizer |
30
|
|
|
*/ |
31
|
|
|
public function __construct( |
32
|
|
|
CategoryDenormalizer $categoryDenormalizer, |
33
|
|
|
AnswerDenormalizer $answerDenormalizer |
34
|
|
|
) { |
35
|
|
|
$this->categoryDenormalizer = $categoryDenormalizer; |
36
|
|
|
$this->answerDenormalizer = $answerDenormalizer; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritdoc |
42
|
|
|
*/ |
43
|
|
|
public function denormalize($data, $class, $format = null, array $context = []): Question |
44
|
|
|
{ |
45
|
|
|
$category = $this->categoryDenormalizer->denormalize( |
46
|
|
|
$data['category'], |
47
|
|
|
Category::class, |
48
|
|
|
$format, |
49
|
|
|
$context |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
$answers = array_map( |
53
|
|
|
function (array $answer) use ($format, $context) { |
54
|
|
|
return $this->answerDenormalizer->denormalize( |
55
|
|
|
$answer, |
56
|
|
|
Answer::class, |
57
|
|
|
$format, |
58
|
|
|
$context |
59
|
|
|
); |
60
|
|
|
}, |
61
|
|
|
$data['answers'] |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
return new Question( |
65
|
|
|
Uuid::fromString($data['id']), |
66
|
|
|
new Language($data['language']), |
67
|
|
|
new Year($data['year']), |
68
|
|
|
$category, |
69
|
|
|
new NotEmptyString($data['text']), |
70
|
|
|
new NotEmptyString($data['imageFileName']), |
71
|
|
|
new Answers(...$answers), |
72
|
|
|
new NotEmptyString($data['feedback']) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritdoc |
78
|
|
|
*/ |
79
|
|
|
public function supportsDenormalization($data, $type, $format = null): bool |
80
|
|
|
{ |
81
|
|
|
return ($type === Question::class) && ($format === 'json'); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|