]> Raphaël G. Git Repositories - userbundle/blobdiff - Entity/Group.php
Rename user bundle alias
[userbundle] / Entity / Group.php
index c503b2f7894f0c07204b9ec7213e486c2a56df23..aa60f77e0f9e48c3a77a1a42f6aa154b53e89806 100644 (file)
@@ -1,8 +1,24 @@
-<?php
+<?php declare(strict_types=1);
+
+/*
+ * This file is part of the Rapsys UserBundle package.
+ *
+ * (c) Raphaël Gertz <symfony@rapsys.eu>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
 
-// src/Rapsys/UserBundle/Entity/Group.php
 namespace Rapsys\UserBundle\Entity;
 
+use Doctrine\Common\Collections\ArrayCollection;
+use Doctrine\ORM\Event\PreUpdateEventArgs;
+
+use Rapsys\UserBundle\Entity\User;
+
+/**
+ * Group
+ */
 class Group {
        /**
         * @var integer
@@ -25,7 +41,7 @@ class Group {
        protected $updated;
 
        /**
-        * @var \Doctrine\Common\Collections\Collection
+        * @var ArrayCollection
         */
        protected $users;
 
@@ -35,8 +51,11 @@ class Group {
         * @param string $title The group name
         */
        public function __construct(string $title) {
-               $this->title = (string) $title;
-               $this->users = new \Doctrine\Common\Collections\ArrayCollection();
+               //Set defaults
+               $this->title = $title;
+               $this->created = new \DateTime('now');
+               $this->updated = new \DateTime('now');
+               $this->users = new ArrayCollection();
        }
 
        /**
@@ -44,7 +63,7 @@ class Group {
         *
         * @return integer
         */
-       public function getId() {
+       public function getId(): int {
                return $this->id;
        }
 
@@ -53,9 +72,9 @@ class Group {
         *
         * @param string $title The group name
         *
-        * @return User
+        * @return Group
         */
-       public function setTitle($title) {
+       public function setTitle(string $title): Group {
                $this->title = $title;
 
                return $this;
@@ -66,7 +85,7 @@ class Group {
         *
         * @return string
         */
-       public function getTitle() {
+       public function getTitle(): ?string {
                return $this->title;
        }
 
@@ -75,9 +94,9 @@ class Group {
         *
         * @param \DateTime $created
         *
-        * @return User
+        * @return Group
         */
-       public function setCreated($created) {
+       public function setCreated(\DateTime $created): Group {
                $this->created = $created;
 
                return $this;
@@ -88,7 +107,7 @@ class Group {
         *
         * @return \DateTime
         */
-       public function getCreated() {
+       public function getCreated(): \DateTime {
                return $this->created;
        }
 
@@ -97,9 +116,9 @@ class Group {
         *
         * @param \DateTime $updated
         *
-        * @return User
+        * @return Group
         */
-       public function setUpdated($updated) {
+       public function setUpdated(\DateTime $updated): Group {
                $this->updated = $updated;
 
                return $this;
@@ -110,18 +129,18 @@ class Group {
         *
         * @return \DateTime
         */
-       public function getUpdated() {
+       public function getUpdated(): \DateTime {
                return $this->updated;
        }
 
        /**
         * Add user
         *
-        * @param \Rapsys\UserBundle\Entity\User $user
+        * @param User $user
         *
         * @return Group
         */
-       public function addUser(\Rapsys\UserBundle\Entity\User $user) {
+       public function addUser(User $user) {
                $this->users[] = $user;
 
                return $this;
@@ -130,21 +149,32 @@ class Group {
        /**
         * Remove user
         *
-        * @param \Rapsys\UserBundle\Entity\User $user
+        * @param User $user
         */
-       public function removeUser(\Rapsys\UserBundle\Entity\User $user) {
+       public function removeUser(User $user) {
                $this->users->removeElement($user);
        }
 
        /**
         * Get users
         *
-        * @return \Doctrine\Common\Collections\Collection
+        * @return ArrayCollection
         */
-       public function getUsers() {
+       public function getUsers(): ArrayCollection {
                return $this->users;
        }
 
+       /**
+        * {@inheritdoc}
+        */
+       public function preUpdate(PreUpdateEventArgs $eventArgs) {
+               //Check that we have a group instance
+               if (($user = $eventArgs->getObject()) instanceof Group) {
+                       //Set updated value
+                       $user->setUpdated(new \DateTime('now'));
+               }
+       }
+
        /**
         * Returns a string representation of the group
         *
@@ -159,7 +189,8 @@ class Group {
         *
         * @return string
         */
-       public function getRole() {
+       public function getRole(): string {
+               //XXX: $prefix = 'ROLE_' set in Role*Voter classes
                return 'ROLE_'.strtoupper($this->title);
        }
 }