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