]> Raphaël G. Git Repositories - userbundle/blob - Entity/Group.php
Add doctrine preUpdate lifecycleCallbacks on civility and group
[userbundle] / Entity / Group.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys PackBundle 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 $this->title = $title;
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->getEntity()) 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 return 'ROLE_'.strtoupper($this->title);
191 }
192 }