1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace VSV\GVQ_API\Question\Repositories\Entities; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use Doctrine\Common\Collections\Collection; |
7
|
|
|
use Doctrine\ORM\Mapping as ORM; |
8
|
|
|
use League\Uri\Uri; |
9
|
|
|
use Ramsey\Uuid\Uuid; |
10
|
|
|
use VSV\GVQ_API\Question\Models\Answer; |
11
|
|
|
use VSV\GVQ_API\Question\Models\Answers; |
12
|
|
|
use VSV\GVQ_API\Question\Models\Question; |
13
|
|
|
use VSV\GVQ_API\Question\ValueObjects\Language; |
14
|
|
|
use VSV\GVQ_API\Question\ValueObjects\NotEmptyString; |
15
|
|
|
use VSV\GVQ_API\Question\ValueObjects\Year; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @ORM\Entity() |
19
|
|
|
* @ORM\Table(name="question") |
20
|
|
|
*/ |
21
|
|
|
class QuestionEntity extends Entity |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
* |
26
|
|
|
* @ORM\Column(type="string", length=2, nullable=false) |
27
|
|
|
*/ |
28
|
|
|
private $language; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var int |
32
|
|
|
* |
33
|
|
|
* @ORM\Column(type="smallint", nullable=false) |
34
|
|
|
*/ |
35
|
|
|
private $year; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var CategoryEntity |
39
|
|
|
* |
40
|
|
|
* @ORM\ManyToOne(targetEntity="CategoryEntity", fetch="EAGER") |
41
|
|
|
* @ORM\JoinColumn(name="category_id", referencedColumnName="id", nullable=false) |
42
|
|
|
*/ |
43
|
|
|
private $categoryEntity; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
* |
48
|
|
|
* @ORM\Column(type="string", length=1024, nullable=false) |
49
|
|
|
*/ |
50
|
|
|
private $text; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string |
54
|
|
|
* |
55
|
|
|
* @ORM\Column(name="picture_uri", type="string", length=255, nullable=false) |
56
|
|
|
*/ |
57
|
|
|
private $pictureUri; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var Collection |
61
|
|
|
* |
62
|
|
|
* @ORM\OneToMany(targetEntity="AnswerEntity", mappedBy="questionEntity", cascade={"all"}) |
63
|
|
|
*/ |
64
|
|
|
private $answerEntities; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var string |
68
|
|
|
* |
69
|
|
|
* @ORM\Column(type="string", length=1024, nullable=false) |
70
|
|
|
*/ |
71
|
|
|
private $feedback; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $id |
75
|
|
|
* @param string $language |
76
|
|
|
* @param int $year |
77
|
|
|
* @param CategoryEntity $categoryEntity |
78
|
|
|
* @param string $text |
79
|
|
|
* @param string $pictureUri |
80
|
|
|
* @param Collection $answerEntities |
81
|
|
|
* @param string $feedback |
82
|
|
|
*/ |
83
|
|
|
private function __construct( |
84
|
|
|
string $id, |
85
|
|
|
string $language, |
86
|
|
|
int $year, |
87
|
|
|
CategoryEntity $categoryEntity, |
88
|
|
|
string $text, |
89
|
|
|
string $pictureUri, |
90
|
|
|
Collection $answerEntities, |
91
|
|
|
string $feedback |
92
|
|
|
) { |
93
|
|
|
parent::__construct($id); |
94
|
|
|
|
95
|
|
|
$this->language = $language; |
96
|
|
|
$this->year = $year; |
97
|
|
|
$this->categoryEntity = $categoryEntity; |
98
|
|
|
$this->text = $text; |
99
|
|
|
$this->pictureUri = $pictureUri; |
100
|
|
|
$this->answerEntities = $answerEntities; |
101
|
|
|
$this->feedback = $feedback; |
102
|
|
|
|
103
|
|
|
foreach ($answerEntities as $answerEntity) { |
104
|
|
|
$answerEntity->setQuestionEntity($this); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param Question $question |
110
|
|
|
* @return QuestionEntity |
111
|
|
|
*/ |
112
|
|
|
public static function fromQuestion(Question $question): QuestionEntity |
113
|
|
|
{ |
114
|
|
|
/** @var AnswerEntity[] $answerEntities */ |
115
|
|
|
$answerEntities = array_map( |
116
|
|
|
function (Answer $answer) { |
117
|
|
|
return AnswerEntity::fromAnswer($answer); |
118
|
|
|
}, |
119
|
|
|
$question->getAnswers()->toArray() |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
$questionEntity = new QuestionEntity( |
123
|
|
|
$question->getId()->toString(), |
124
|
|
|
$question->getLanguage()->toNative(), |
125
|
|
|
$question->getYear()->toNative(), |
126
|
|
|
CategoryEntity::fromCategory($question->getCategory()), |
127
|
|
|
$question->getText()->toNative(), |
128
|
|
|
$question->getPictureUri()->__toString(), |
129
|
|
|
new ArrayCollection($answerEntities), |
130
|
|
|
$question->getFeedback()->toNative() |
131
|
|
|
); |
132
|
|
|
|
133
|
|
|
return $questionEntity; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return Question |
138
|
|
|
*/ |
139
|
|
|
public function toQuestion(): Question |
140
|
|
|
{ |
141
|
|
|
$answers = new Answers( |
142
|
|
|
...array_map( |
143
|
|
|
function (AnswerEntity $answerEntity) { |
144
|
|
|
return $answerEntity->toAnswer(); |
145
|
|
|
}, |
146
|
|
|
$this->getAnswerEntities()->toArray() |
147
|
|
|
) |
148
|
|
|
); |
149
|
|
|
|
150
|
|
|
// Call toString method to solve side effect in Uri lib because the data |
151
|
|
|
// property gets created on calling this getter method. |
152
|
|
|
// @see https://github.com/thephpleague/uri-schemes/issues/10 |
153
|
|
|
$pictureUri = Uri::createFromString($this->getPictureUri()); |
154
|
|
|
$pictureUri->__toString(); |
155
|
|
|
|
156
|
|
|
return new Question( |
157
|
|
|
Uuid::fromString($this->getId()), |
158
|
|
|
new Language($this->getLanguage()), |
159
|
|
|
new Year($this->getYear()), |
160
|
|
|
$this->getCategoryEntity()->toCategory(), |
161
|
|
|
new NotEmptyString($this->getText()), |
162
|
|
|
$pictureUri, |
163
|
|
|
$answers, |
164
|
|
|
new NotEmptyString($this->getFeedback()) |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
|
|
public function getLanguage(): string |
172
|
|
|
{ |
173
|
|
|
return $this->language; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return int |
178
|
|
|
*/ |
179
|
|
|
public function getYear(): int |
180
|
|
|
{ |
181
|
|
|
return $this->year; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return CategoryEntity |
186
|
|
|
*/ |
187
|
|
|
public function getCategoryEntity(): CategoryEntity |
188
|
|
|
{ |
189
|
|
|
return $this->categoryEntity; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param CategoryEntity $categoryEntity |
194
|
|
|
*/ |
195
|
|
|
public function setCategoryEntity(CategoryEntity $categoryEntity): void |
196
|
|
|
{ |
197
|
|
|
$this->categoryEntity = $categoryEntity; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @return string |
202
|
|
|
*/ |
203
|
|
|
public function getText(): string |
204
|
|
|
{ |
205
|
|
|
return $this->text; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @return string |
210
|
|
|
*/ |
211
|
|
|
public function getPictureUri(): string |
212
|
|
|
{ |
213
|
|
|
return $this->pictureUri; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @return Collection |
218
|
|
|
*/ |
219
|
|
|
public function getAnswerEntities(): Collection |
220
|
|
|
{ |
221
|
|
|
return $this->answerEntities; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @return string |
226
|
|
|
*/ |
227
|
|
|
public function getFeedback(): string |
228
|
|
|
{ |
229
|
|
|
return $this->feedback; |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|