]> Raphaël G. Git Repositories - userbundle/blob - Entity/Group.php
Initialize all fields
[userbundle] / Entity / Group.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys UserBundle 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\UserBundle\Entity;
13
14 use Doctrine\Common\Collections\ArrayCollection;
15 use Doctrine\ORM\Event\PreUpdateEventArgs;
16
17 use Rapsys\UserBundle\Entity\User;
18
19 /**
20 * Group
21 */
22 class Group {
23 /**
24 * @var integer
25 */
26 protected $id;
27
28 /**
29 * @var string
30 */
31 protected $title;
32
33 /**
34 * @var \DateTime
35 */
36 protected $created;
37
38 /**
39 * @var \DateTime
40 */
41 protected $updated;
42
43 /**
44 * @var ArrayCollection
45 */
46 protected $users;
47
48 /**
49 * Constructor
50 *
51 * @param string $title The group name
52 */
53 public function __construct(string $title) {
54 //Set defaults
55 $this->title = $title;
56 $this->created = new \DateTime('now');
57 $this->updated = new \DateTime('now');
58 $this->users = new ArrayCollection();
59 }
60
61 /**
62 * Get id
63 *
64 * @return integer
65 */
66 public function getId(): int {
67 return $this->id;
68 }
69
70 /**
71 * Set title
72 *
73 * @param string $title The group name
74 *
75 * @return Group
76 */
77 public function setTitle(string $title): Group {
78 $this->title = $title;
79
80 return $this;
81 }
82
83 /**
84 * Get title
85 *
86 * @return string
87 */
88 public function getTitle(): ?string {
89 return $this->title;
90 }
91
92 /**
93 * Set created
94 *
95 * @param \DateTime $created
96 *
97 * @return Group
98 */
99 public function setCreated(\DateTime $created): Group {
100 $this->created = $created;
101
102 return $this;
103 }
104
105 /**
106 * Get created
107 *
108 * @return \DateTime
109 */
110 public function getCreated(): \DateTime {
111 return $this->created;
112 }
113
114 /**
115 * Set updated
116 *
117 * @param \DateTime $updated
118 *
119 * @return Group
120 */
121 public function setUpdated(\DateTime $updated): Group {
122 $this->updated = $updated;
123
124 return $this;
125 }
126
127 /**
128 * Get updated
129 *
130 * @return \DateTime
131 */
132 public function getUpdated(): \DateTime {
133 return $this->updated;
134 }
135
136 /**
137 * Add user
138 *
139 * @param User $user
140 *
141 * @return Group
142 */
143 public function addUser(User $user) {
144 $this->users[] = $user;
145
146 return $this;
147 }
148
149 /**
150 * Remove user
151 *
152 * @param User $user
153 */
154 public function removeUser(User $user) {
155 $this->users->removeElement($user);
156 }
157
158 /**
159 * Get users
160 *
161 * @return ArrayCollection
162 */
163 public function getUsers(): ArrayCollection {
164 return $this->users;
165 }
166
167 /**
168 * {@inheritdoc}
169 */
170 public function preUpdate(PreUpdateEventArgs $eventArgs) {
171 //Check that we have a group instance
172 if (($user = $eventArgs->getEntity()) instanceof Group) {
173 //Set updated value
174 $user->setUpdated(new \DateTime('now'));
175 }
176 }
177
178 /**
179 * Returns a string representation of the group
180 *
181 * @return string
182 */
183 public function __toString(): string {
184 return $this->title;
185 }
186
187 /**
188 * Get role
189 *
190 * @return string
191 */
192 public function getRole(): string {
193 return 'ROLE_'.strtoupper($this->title);
194 }
195 }