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