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 User |
48
|
|
|
* @Assert\Type("object") |
49
|
|
|
* @Assert\Valid |
50
|
|
|
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="surveys") |
51
|
|
|
* @ORM\JoinColumn(onDelete="CASCADE") |
52
|
|
|
*/ |
53
|
|
|
private $user; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var ArrayCollection[SurveyAnswer] |
57
|
|
|
* @ORM\OneToMany(targetEntity="SurveyAnswer", mappedBy="survey", cascade={"persist", "remove"}) |
58
|
|
|
*/ |
59
|
|
|
private $answers; |
60
|
|
|
|
61
|
1 |
|
public function __construct() |
62
|
|
|
{ |
63
|
1 |
|
$this->answers = new ArrayCollection(); |
64
|
1 |
|
$this->status = 'current'; |
65
|
1 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get id. |
69
|
|
|
* |
70
|
|
|
* @return int |
71
|
|
|
*/ |
72
|
3 |
|
public function getId() |
73
|
|
|
{ |
74
|
3 |
|
return $this->id; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set type. |
79
|
|
|
* |
80
|
|
|
* @param SurveyType $type |
81
|
|
|
* |
82
|
|
|
* @return Survey |
83
|
|
|
*/ |
84
|
1 |
|
public function setType(SurveyType $type) |
85
|
|
|
{ |
86
|
1 |
|
$this->type = $type; |
87
|
|
|
|
88
|
1 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get type. |
93
|
|
|
* |
94
|
|
|
* @return SurveyType |
95
|
|
|
*/ |
96
|
7 |
|
public function getType() |
97
|
|
|
{ |
98
|
7 |
|
return $this->type; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Set status. |
103
|
|
|
* |
104
|
|
|
* @param string $status |
105
|
|
|
* |
106
|
|
|
* @return Survey |
107
|
|
|
*/ |
108
|
1 |
|
public function setStatus($status) |
109
|
|
|
{ |
110
|
1 |
|
$this->status = $status; |
111
|
|
|
|
112
|
1 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get status. |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
3 |
|
public function getStatus() |
121
|
|
|
{ |
122
|
3 |
|
return $this->status; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Set user. |
127
|
|
|
* |
128
|
|
|
* @param User $user |
129
|
|
|
* |
130
|
|
|
* @return Survey |
131
|
|
|
*/ |
132
|
1 |
|
public function setUser(User $user) |
133
|
|
|
{ |
134
|
1 |
|
$this->user = $user; |
135
|
|
|
|
136
|
1 |
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get user. |
141
|
|
|
* |
142
|
|
|
* @return User |
143
|
|
|
*/ |
144
|
5 |
|
public function getUser() |
145
|
|
|
{ |
146
|
5 |
|
return $this->user; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param SurveyAnswer $answer |
151
|
|
|
* |
152
|
|
|
* @return Survey |
153
|
|
|
*/ |
154
|
|
|
public function addSurveyAnswer(SurveyAnswer $answer) |
155
|
|
|
{ |
156
|
|
|
if (!$this->answers->contains($answer)) { |
157
|
|
|
$this->answers->add($answer); |
158
|
|
|
$answer->setSurvey($this); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Get answers. |
166
|
|
|
* |
167
|
|
|
* @return ArrayCollection |
168
|
|
|
*/ |
169
|
|
|
public function getAnswers() |
170
|
|
|
{ |
171
|
|
|
return $this->answers; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Get questions. |
176
|
|
|
* |
177
|
|
|
* @return array |
178
|
|
|
*/ |
179
|
1 |
|
public function getQuestions() |
180
|
|
|
{ |
181
|
1 |
|
$sections = $this->getType()->getSections(); |
182
|
1 |
|
foreach ($sections as $section) { |
183
|
1 |
|
foreach ($section->getQuestions() as $quest) { |
184
|
1 |
|
$questions[] = $quest; |
|
|
|
|
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
1 |
|
return $questions; |
|
|
|
|
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
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.