]> Raphaël G. Git Repositories - userbundle/blobdiff - Entity/Civility.php
Strict types
[userbundle] / Entity / Civility.php
index 83c7f47ef4b1d7359452c0108291a6c0207d0df9..ca2aee12e4486558a9d375c95395dcb9965adf3a 100644 (file)
@@ -1,41 +1,58 @@
-<?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.
+ */
 
 namespace Rapsys\UserBundle\Entity;
 
+use Doctrine\Common\Collections\Collection;
+use Doctrine\Common\Collections\ArrayCollection;
+use Doctrine\ORM\Event\PreUpdateEventArgs;
+
+use Rapsys\UserBundle\Entity\User;
+
 /**
  * Civility
  */
 class Civility {
        /**
-        * @var integer
-        */
-       protected $id;
-
-       /**
-        * @var string
+        * Primary key
         */
-       protected $title;
+       protected ?int $id = null;
 
        /**
-        * @var \DateTime
+        * Creation datetime
         */
-       protected $created;
+       protected \DateTime $created;
 
        /**
-        * @var \DateTime
+        * Update datetime
         */
-       protected $updated;
+       protected \DateTime $updated;
 
        /**
-        * @var \Doctrine\Common\Collections\Collection
+        * Users collection
         */
-       protected $users;
+       protected Collection $users;
 
        /**
         * Constructor
+        *
+        * @param string $title The civility name
         */
-       public function __construct() {
-               $this->users = new \Doctrine\Common\Collections\ArrayCollection();
+       public function __construct(protected string $title) {
+               //Set defaults
+               $this->created = new \DateTime('now');
+               $this->updated = new \DateTime('now');
+
+               //Set collections
+               $this->users = new ArrayCollection();
        }
 
        /**
@@ -43,7 +60,7 @@ class Civility {
         *
         * @return integer
         */
-       public function getId() {
+       public function getId(): ?int {
                return $this->id;
        }
 
@@ -54,7 +71,7 @@ class Civility {
         *
         * @return Civility
         */
-       public function setTitle($title) {
+       public function setTitle(string $title): Civility {
                $this->title = $title;
 
                return $this;
@@ -65,7 +82,7 @@ class Civility {
         *
         * @return string
         */
-       public function getTitle() {
+       public function getTitle(): ?string {
                return $this->title;
        }
 
@@ -76,7 +93,7 @@ class Civility {
         *
         * @return Civility
         */
-       public function setCreated($created) {
+       public function setCreated(\DateTime $created): Civility {
                $this->created = $created;
 
                return $this;
@@ -87,7 +104,7 @@ class Civility {
         *
         * @return \DateTime
         */
-       public function getCreated() {
+       public function getCreated(): \DateTime {
                return $this->created;
        }
 
@@ -98,7 +115,7 @@ class Civility {
         *
         * @return Civility
         */
-       public function setUpdated($updated) {
+       public function setUpdated(\DateTime $updated): Civility {
                $this->updated = $updated;
 
                return $this;
@@ -109,18 +126,18 @@ class Civility {
         *
         * @return \DateTime
         */
-       public function getUpdated() {
+       public function getUpdated(): \DateTime {
                return $this->updated;
        }
 
        /**
         * Add user
         *
-        * @param \Rapsys\UserBundle\Entity\User $user
+        * @param User $user
         *
         * @return Civility
         */
-       public function addUser(\Rapsys\UserBundle\Entity\User $user) {
+       public function addUser(User $user): Civility {
                $this->users[] = $user;
 
                return $this;
@@ -129,21 +146,32 @@ class Civility {
        /**
         * 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 civility instance
+               if (($user = $eventArgs->getObject()) instanceof Civility) {
+                       //Set updated value
+                       $user->setUpdated(new \DateTime('now'));
+               }
+       }
+
        /**
         * Returns a string representation of the title
         *