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