1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace VSV\GVQ_API\Question\Repositories\Entities; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Mapping as ORM; |
6
|
|
|
use Ramsey\Uuid\Uuid; |
7
|
|
|
use VSV\GVQ_API\Common\Repositories\Entities\Entity; |
8
|
|
|
use VSV\GVQ_API\Question\Models\Answer; |
9
|
|
|
use VSV\GVQ_API\Common\ValueObjects\NotEmptyString; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @ORM\Entity() |
13
|
|
|
* @ORM\Table(name="answer") |
14
|
|
|
*/ |
15
|
|
|
class AnswerEntity extends Entity |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
* |
20
|
|
|
* @ORM\Column(type="string", length=1024, nullable=false) |
21
|
|
|
*/ |
22
|
|
|
private $text; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var bool |
26
|
|
|
* |
27
|
|
|
* @ORM\Column(type="boolean", nullable=false) |
28
|
|
|
*/ |
29
|
|
|
private $correct; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var QuestionEntity |
33
|
|
|
* |
34
|
|
|
* @ORM\ManyToOne(targetEntity="QuestionEntity", inversedBy="answerEntities") |
35
|
|
|
* @ORM\JoinColumn(name="question_id", referencedColumnName="id", nullable=false) |
36
|
|
|
*/ |
37
|
|
|
private $questionEntity; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $id |
41
|
|
|
* @param string $text |
42
|
|
|
* @param bool $correct |
43
|
|
|
*/ |
44
|
|
|
private function __construct( |
45
|
|
|
string $id, |
46
|
|
|
string $text, |
47
|
|
|
bool $correct |
48
|
|
|
) { |
49
|
|
|
parent::__construct($id); |
50
|
|
|
|
51
|
|
|
$this->text = $text; |
52
|
|
|
$this->correct = $correct; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param Answer $answer |
57
|
|
|
* @return AnswerEntity |
58
|
|
|
*/ |
59
|
|
|
public static function fromAnswer(Answer $answer): AnswerEntity |
60
|
|
|
{ |
61
|
|
|
return new AnswerEntity( |
62
|
|
|
$answer->getId()->toString(), |
63
|
|
|
$answer->getText()->toNative(), |
64
|
|
|
$answer->isCorrect() |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return Answer |
70
|
|
|
*/ |
71
|
|
|
public function toAnswer(): Answer |
72
|
|
|
{ |
73
|
|
|
return new Answer( |
74
|
|
|
Uuid::fromString($this->getId()), |
75
|
|
|
new NotEmptyString($this->getText()), |
76
|
|
|
$this->isCorrect() |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function getText(): string |
84
|
|
|
{ |
85
|
|
|
return $this->text; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
public function isCorrect(): bool |
92
|
|
|
{ |
93
|
|
|
return $this->correct; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param QuestionEntity $questionEntity |
98
|
|
|
*/ |
99
|
|
|
public function setQuestionEntity(QuestionEntity $questionEntity): void |
100
|
|
|
{ |
101
|
|
|
$this->questionEntity = $questionEntity; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|