]> Raphaël G. Git Repositories - airbundle/blob - Entity/Dance.php
Fix dance user add/remove from inverse side
[airbundle] / Entity / Dance.php
1 <?php declare(strict_types=1);
2
3 /*
4 * this file is part of the rapsys packbundle package.
5 *
6 * (c) raphaël gertz <symfony@rapsys.eu>
7 *
8 * for the full copyright and license information, please view the license
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\AirBundle\Entity;
13
14 use Doctrine\Common\Collections\ArrayCollection;
15 use Doctrine\ORM\Event\PreUpdateEventArgs;
16
17 /**
18 * Dance
19 */
20 class Dance {
21 /**
22 * @var integer
23 */
24 private $id;
25
26 /**
27 * @var string
28 */
29 protected $name;
30
31 /**
32 * @var string
33 */
34 protected $type;
35
36 /**
37 * @var \DateTime
38 */
39 private $created;
40
41 /**
42 * @var \DateTime
43 */
44 private $updated;
45
46 /**
47 * @var ArrayCollection
48 */
49 private $applications;
50
51 /**
52 * @var ArrayCollection
53 */
54 private $users;
55
56 /**
57 * Constructor
58 *
59 * @param string $name The dance name
60 * @param string $type The dance type
61 */
62 public function __construct(string $name, string $type) {
63 //Set defaults
64 $this->name = $name;
65 $this->type = $type;
66 $this->created = new \DateTime('now');
67 $this->updated = new \DateTime('now');
68
69 //Set collections
70 $this->applications = new ArrayCollection();
71 $this->users = new ArrayCollection();
72 }
73
74 /**
75 * Get id
76 *
77 * @return integer
78 */
79 public function getId(): int {
80 return $this->id;
81 }
82
83 /**
84 * Set name
85 *
86 * @param string $name
87 *
88 * @return Dance
89 */
90 public function setName(string $name): Dance {
91 $this->name = $name;
92
93 return $this;
94 }
95
96 /**
97 * Get name
98 *
99 * @return string
100 */
101 public function getName(): string {
102 return $this->name;
103 }
104
105 /**
106 * Set type
107 *
108 * @param string $type
109 *
110 * @return Dance
111 */
112 public function setType(string $type): Dance {
113 $this->type = $type;
114
115 return $this;
116 }
117
118 /**
119 * Get type
120 *
121 * @return string
122 */
123 public function getType(): string {
124 return $this->type;
125 }
126
127 /**
128 * Set created
129 *
130 * @param \DateTime $created
131 *
132 * @return Dance
133 */
134 public function setCreated(\DateTime $created): Dance {
135 $this->created = $created;
136
137 return $this;
138 }
139
140 /**
141 * Get created
142 *
143 * @return \DateTime
144 */
145 public function getCreated(): \DateTime {
146 return $this->created;
147 }
148
149 /**
150 * Set updated
151 *
152 * @param \DateTime $updated
153 *
154 * @return Dance
155 */
156 public function setUpdated(\DateTime $updated): Dance {
157 $this->updated = $updated;
158
159 return $this;
160 }
161
162 /**
163 * Get updated
164 *
165 * @return \DateTime
166 */
167 public function getUpdated(): \DateTime {
168 return $this->updated;
169 }
170
171 /**
172 * Add application
173 *
174 * @param Application $application
175 *
176 * @return Dance
177 */
178 public function addApplication(Application $application): Dance {
179 $this->applications[] = $application;
180
181 return $this;
182 }
183
184 /**
185 * Remove application
186 *
187 * @param Application $application
188 *
189 * @return bool
190 */
191 public function removeApplication(Application $application): bool {
192 return $this->applications->removeElement($application);
193 }
194
195 /**
196 * Get applications
197 *
198 * @return ArrayCollection
199 */
200 public function getApplications(): ArrayCollection {
201 return $this->applications;
202 }
203
204 /**
205 * Add user
206 *
207 * @param User $user
208 *
209 * @return Dance
210 */
211 public function addUser(User $user): Dance {
212 //Add from owning side
213 $user->addDance($this);
214
215 $this->users[] = $user;
216
217 return $this;
218 }
219
220 /**
221 * Remove user
222 *
223 * @param User $user
224 *
225 * @return bool
226 */
227 public function removeUser(User $user): bool {
228 if (!$this->dances->contains($user)) {
229 return true;
230 }
231
232 //Remove from owning side
233 $user->removeDance($this);
234
235 return $this->users->removeElement($user);
236 }
237
238 /**
239 * Get users
240 *
241 * @return ArrayCollection
242 */
243 public function getUsers(): ArrayCollection {
244 return $this->users;
245 }
246
247 /**
248 * {@inheritdoc}
249 */
250 public function preUpdate(PreUpdateEventArgs $eventArgs) {
251 //Check that we have a dance instance
252 if (($dance = $eventArgs->getEntity()) instanceof Dance) {
253 //Set updated value
254 $dance->setUpdated(new \DateTime('now'));
255 }
256 }
257
258 /**
259 * Returns a string representation of the slot
260 *
261 * @return string
262 */
263 public function __toString(): string {
264 return $this->name.' '.lcfirst($this->type);
265 }
266 }