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