1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Entity\Survey; |
4
|
|
|
|
5
|
|
|
use AppBundle\Entity\User; |
6
|
|
|
use Doctrine\ORM\Mapping as ORM; |
7
|
|
|
use Knp\DoctrineBehaviors\Model as ORMBehaviors; |
8
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
9
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Survey. |
13
|
|
|
* |
14
|
|
|
* @ORM\Entity(repositoryClass="AppBundle\Repository\SurveyRepository") |
15
|
|
|
*/ |
16
|
|
|
class Survey |
17
|
|
|
{ |
18
|
|
|
use ORMBehaviors\Timestampable\Timestampable; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var int |
22
|
|
|
* |
23
|
|
|
* @ORM\Column(type="integer") |
24
|
|
|
* @ORM\Id |
25
|
|
|
* @ORM\GeneratedValue(strategy="AUTO") |
26
|
|
|
*/ |
27
|
|
|
private $id; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var SurveyType |
31
|
|
|
* @Assert\Type("object") |
32
|
|
|
* @Assert\Valid |
33
|
|
|
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Survey\SurveyType", inversedBy="surveys") |
34
|
|
|
* @ORM\JoinColumn(onDelete="SET NULL") |
35
|
|
|
*/ |
36
|
|
|
private $type; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
* @Assert\NotBlank() |
41
|
|
|
* @Assert\Type("string") |
42
|
|
|
* @ORM\Column(type="string") |
43
|
|
|
*/ |
44
|
|
|
private $status; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var bool |
48
|
|
|
* @Assert\Type("bool") |
49
|
|
|
* @ORM\Column(type="boolean") |
50
|
|
|
*/ |
51
|
|
|
private $reviewed; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var User |
55
|
|
|
* @Assert\Type("object") |
56
|
|
|
* @Assert\Valid |
57
|
|
|
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="surveys") |
58
|
|
|
* @ORM\JoinColumn(onDelete="CASCADE") |
59
|
|
|
*/ |
60
|
|
|
private $user; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var ArrayCollection[SurveyAnswer] |
64
|
|
|
* @ORM\OneToMany(targetEntity="SurveyAnswer", mappedBy="survey", cascade={"persist", "remove"}) |
65
|
|
|
*/ |
66
|
|
|
private $answers; |
67
|
|
|
|
68
|
1 |
|
public function __construct() |
69
|
|
|
{ |
70
|
1 |
|
$this->answers = new ArrayCollection(); |
71
|
1 |
|
$this->status = 'current'; |
72
|
1 |
|
$this->reviewed = false; |
73
|
1 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get id. |
77
|
|
|
* |
78
|
|
|
* @return int |
79
|
|
|
*/ |
80
|
2 |
|
public function getId() |
81
|
|
|
{ |
82
|
2 |
|
return $this->id; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Set type. |
87
|
|
|
* |
88
|
|
|
* @param SurveyType $type |
89
|
|
|
* |
90
|
|
|
* @return Survey |
91
|
|
|
*/ |
92
|
1 |
|
public function setType(SurveyType $type) |
93
|
|
|
{ |
94
|
1 |
|
$this->type = $type; |
95
|
|
|
|
96
|
1 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get type. |
101
|
|
|
* |
102
|
|
|
* @return SurveyType |
103
|
|
|
*/ |
104
|
6 |
|
public function getType() |
105
|
|
|
{ |
106
|
6 |
|
return $this->type; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Set status. |
111
|
|
|
* |
112
|
|
|
* @param string $status |
113
|
|
|
* |
114
|
|
|
* @return Survey |
115
|
|
|
*/ |
116
|
1 |
|
public function setStatus($status) |
117
|
|
|
{ |
118
|
1 |
|
$this->status = $status; |
119
|
|
|
|
120
|
1 |
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get status. |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
3 |
|
public function getStatus() |
129
|
|
|
{ |
130
|
3 |
|
return $this->status; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Set review. |
135
|
|
|
* |
136
|
|
|
* @param bool $review |
137
|
|
|
* |
138
|
|
|
* @return Survey |
139
|
|
|
*/ |
140
|
1 |
|
public function setReviewed($review) |
141
|
|
|
{ |
142
|
1 |
|
$this->reviewed = $review; |
143
|
|
|
|
144
|
1 |
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get review. |
149
|
|
|
* |
150
|
|
|
* @return bool |
151
|
|
|
*/ |
152
|
1 |
|
public function isReviewed() |
153
|
|
|
{ |
154
|
1 |
|
return $this->reviewed; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Set user. |
159
|
|
|
* |
160
|
|
|
* @param User $user |
161
|
|
|
* |
162
|
|
|
* @return Survey |
163
|
|
|
*/ |
164
|
1 |
|
public function setUser(User $user) |
165
|
|
|
{ |
166
|
1 |
|
$this->user = $user; |
167
|
|
|
|
168
|
1 |
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Get user. |
173
|
|
|
* |
174
|
|
|
* @return User |
175
|
|
|
*/ |
176
|
4 |
|
public function getUser() |
177
|
|
|
{ |
178
|
4 |
|
return $this->user; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param SurveyAnswer $answer |
183
|
|
|
* |
184
|
|
|
* @return Survey |
185
|
|
|
*/ |
186
|
|
|
public function addSurveyAnswer(SurveyAnswer $answer) |
187
|
|
|
{ |
188
|
|
|
if (!$this->answers->contains($answer)) { |
189
|
|
|
$this->answers->add($answer); |
190
|
|
|
$answer->setSurvey($this); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Get answers. |
198
|
|
|
* |
199
|
|
|
* @return ArrayCollection |
200
|
|
|
*/ |
201
|
|
|
public function getAnswers() |
202
|
|
|
{ |
203
|
|
|
return $this->answers; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Get questions. |
208
|
|
|
* |
209
|
|
|
* @return array |
210
|
|
|
*/ |
211
|
1 |
|
public function getQuestions() |
212
|
|
|
{ |
213
|
1 |
|
$sections = $this->getType()->getSections(); |
214
|
1 |
|
foreach ($sections as $section) { |
215
|
1 |
|
foreach ($section->getQuestions() as $quest) { |
216
|
1 |
|
$questions[] = $quest; |
|
|
|
|
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
1 |
|
return $questions; |
|
|
|
|
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Add answer |
225
|
|
|
* |
226
|
|
|
* @param \AppBundle\Entity\Survey\SurveyAnswer $answer |
227
|
|
|
* |
228
|
|
|
* @return Survey |
229
|
|
|
*/ |
230
|
|
|
public function addAnswer(\AppBundle\Entity\Survey\SurveyAnswer $answer) |
231
|
|
|
{ |
232
|
|
|
$this->answers[] = $answer; |
233
|
|
|
|
234
|
|
|
return $this; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Remove answer |
239
|
|
|
* |
240
|
|
|
* @param \AppBundle\Entity\Survey\SurveyAnswer $answer |
241
|
|
|
*/ |
242
|
|
|
public function removeAnswer(\AppBundle\Entity\Survey\SurveyAnswer $answer) |
243
|
|
|
{ |
244
|
|
|
$this->answers->removeElement($answer); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.