]> Raphaël G. Git Repositories - userbundle/blobdiff - Entity/Group.php
Strict types
[userbundle] / Entity / Group.php
index c7000ea8627aa3d0fa01468e04938e1579ca8728..13b16ef821049e9bcfa41682b038911d33cd2b7a 100644 (file)
@@ -1,63 +1,89 @@
-<?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;
 
-class Group extends \Symfony\Component\Security\Core\Role\Role {
+use Doctrine\Common\Collections\Collection;
+use Doctrine\Common\Collections\ArrayCollection;
+use Doctrine\ORM\Event\PreUpdateEventArgs;
+
+use Rapsys\UserBundle\Entity\User;
+
+/**
+ * Group
+ */
+class Group {
        /**
-        * @var integer
+        * Primary key
         */
-       protected $id;
+       protected ?int $id = null;
 
        /**
-        * @var string
+        * Creation datetime
         */
-       protected $role;
+       protected \DateTime $created;
 
        /**
-        * @var \DateTime
+        * Update datetime
         */
-       protected $created;
+       protected \DateTime $updated;
 
        /**
-        * @var \DateTime
+        * Users collection
         */
-       protected $updated;
+       protected Collection $users;
 
        /**
-        * @var \Doctrine\Common\Collections\Collection
+        * Constructor
+        *
+        * @param string $title The group name
         */
-       protected $users;
+       public function __construct(protected string $title) {
+               //Set defaults
+               $this->created = new \DateTime('now');
+               $this->updated = new \DateTime('now');
+
+               //Set collections
+               $this->users = new ArrayCollection();
+       }
 
        /**
-        * Constructor
-        * @param string $role The role name
+        * Get id
+        *
+        * @return integer
         */
-       public function __construct($role) {
-               $this->role = (string) $role;
-               $this->users = new \Doctrine\Common\Collections\ArrayCollection();
+       public function getId(): ?int {
+               return $this->id;
        }
 
        /**
-        * Set role
+        * Set title
         *
-        * @param string $role
+        * @param string $title The group name
         *
-        * @return User
+        * @return Group
         */
-       public function setRole($role) {
-               $this->role = $role;
+       public function setTitle(string $title): Group {
+               $this->title = $title;
 
                return $this;
        }
 
        /**
-        * Get role
+        * Get title
         *
         * @return string
         */
-       public function getRole() {
-               return $this->role;
+       public function getTitle(): string {
+               return $this->title;
        }
 
        /**
@@ -65,9 +91,9 @@ class Group extends \Symfony\Component\Security\Core\Role\Role {
         *
         * @param \DateTime $created
         *
-        * @return User
+        * @return Group
         */
-       public function setCreated($created) {
+       public function setCreated(\DateTime $created): Group {
                $this->created = $created;
 
                return $this;
@@ -78,7 +104,7 @@ class Group extends \Symfony\Component\Security\Core\Role\Role {
         *
         * @return \DateTime
         */
-       public function getCreated() {
+       public function getCreated(): \DateTime {
                return $this->created;
        }
 
@@ -87,9 +113,9 @@ class Group extends \Symfony\Component\Security\Core\Role\Role {
         *
         * @param \DateTime $updated
         *
-        * @return User
+        * @return Group
         */
-       public function setUpdated($updated) {
+       public function setUpdated(\DateTime $updated): Group {
                $this->updated = $updated;
 
                return $this;
@@ -100,18 +126,18 @@ class Group extends \Symfony\Component\Security\Core\Role\Role {
         *
         * @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;
@@ -120,18 +146,48 @@ class Group extends \Symfony\Component\Security\Core\Role\Role {
        /**
         * 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
+        *
+        * @return string
+        */
+       public function __toString(): string {
+               return $this->title;
+       }
+
+       /**
+        * Get role
+        *
+        * @return string
+        */
+       public function getRole(): string {
+               //XXX: $prefix = 'ROLE_' set in Role*Voter classes
+               return 'ROLE_'.strtoupper($this->title);
+       }
 }