]> Raphaël G. Git Repositories - airbundle/blob - Entity/Country.php
Convert every optional field as toggleable
[airbundle] / Entity / Country.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\ArrayCollection;
15 use Doctrine\ORM\Event\PreUpdateEventArgs;
16
17 /**
18 * Country
19 */
20 class Country {
21 /**
22 * @var integer
23 */
24 private $id;
25
26 /**
27 * @var string
28 */
29 protected $code;
30
31 /**
32 * @var string
33 */
34 protected $alpha;
35
36 /**
37 * @var string
38 */
39 protected $title;
40
41 /**
42 * @var \DateTime
43 */
44 protected $created;
45
46 /**
47 * @var \DateTime
48 */
49 protected $updated;
50
51 /**
52 * @var ArrayCollection
53 */
54 protected $users;
55
56 /**
57 * Constructor
58 *
59 * @param string $code The country code
60 * @param string $alpha The country alpha
61 * @param string $title The country title
62 */
63 public function __construct(string $code, string $alpha, string $title) {
64 //Set defaults
65 $this->code = $code;
66 $this->alpha = $alpha;
67 $this->title = $title;
68 $this->created = new \DateTime('now');
69 $this->updated = new \DateTime('now');
70
71 //Set collections
72 $this->users = new ArrayCollection();
73 }
74
75 /**
76 * Get id
77 *
78 * @return integer
79 */
80 public function getId(): int {
81 return $this->id;
82 }
83
84 /**
85 * Set code
86 *
87 * @param string $code
88 *
89 * @return Country
90 */
91 public function setCode(string $code): Country {
92 $this->code = $code;
93
94 return $this;
95 }
96
97 /**
98 * Get code
99 *
100 * @return string
101 */
102 public function getCode(): string {
103 return $this->code;
104 }
105
106 /**
107 * Set alpha
108 *
109 * @param string $alpha
110 *
111 * @return Country
112 */
113 public function setAlpha(string $alpha): Country {
114 $this->alpha = $alpha;
115
116 return $this;
117 }
118
119 /**
120 * Get alpha
121 *
122 * @return string
123 */
124 public function getAlpha(): string {
125 return $this->alpha;
126 }
127
128 /**
129 * Set title
130 *
131 * @param string $title
132 *
133 * @return Country
134 */
135 public function setTitle(string $title): Country {
136 $this->title = $title;
137
138 return $this;
139 }
140
141 /**
142 * Get title
143 *
144 * @return string
145 */
146 public function getTitle(): string {
147 return $this->title;
148 }
149
150 /**
151 * Set created
152 *
153 * @param \DateTime $created
154 *
155 * @return Country
156 */
157 public function setCreated(\DateTime $created): Country {
158 $this->created = $created;
159
160 return $this;
161 }
162
163 /**
164 * Get created
165 *
166 * @return \DateTime
167 */
168 public function getCreated(): \DateTime {
169 return $this->created;
170 }
171
172 /**
173 * Set updated
174 *
175 * @param \DateTime $updated
176 *
177 * @return Country
178 */
179 public function setUpdated(\DateTime $updated): Country {
180 $this->updated = $updated;
181
182 return $this;
183 }
184
185 /**
186 * Get updated
187 *
188 * @return \DateTime
189 */
190 public function getUpdated(): \DateTime {
191 return $this->updated;
192 }
193
194 /**
195 * Add user
196 *
197 * @param User $user
198 *
199 * @return Country
200 */
201 public function addUser(User $user): User {
202 $this->users[] = $user;
203
204 return $this;
205 }
206
207 /**
208 * Remove user
209 *
210 * @param User $user
211 */
212 public function removeUser(User $user): bool {
213 return $this->users->removeElement($user);
214 }
215
216 /**
217 * Get users
218 *
219 * @return ArrayCollection
220 */
221 public function getUsers(): ArrayCollection {
222 return $this->users;
223 }
224
225 /**
226 * {@inheritdoc}
227 */
228 public function preUpdate(PreUpdateEventArgs $eventArgs) {
229 //Check that we have an country instance
230 if (($country = $eventArgs->getEntity()) instanceof Country) {
231 //Set updated value
232 $country->setUpdated(new \DateTime('now'));
233 }
234 }
235
236 /**
237 * Returns a string representation of the country
238 *
239 * @return string
240 */
241 public function __toString(): string {
242 return $this->title;
243 }
244 }