]> Raphaël G. Git Repositories - userbundle/blobdiff - Entity/Civility.php
Version 0.6.2
[userbundle] / Entity / Civility.php
index f3509194542f2ac3711badf984c9d172b5c61c59..b88968b8f3185765d93710753c4a12d02ed9b028 100644 (file)
@@ -1,7 +1,7 @@
 <?php declare(strict_types=1);
 
 /*
- * This file is part of the Rapsys PackBundle package.
+ * This file is part of the Rapsys UserBundle package.
  *
  * (c) Raphaël Gertz <symfony@rapsys.eu>
  *
@@ -11,7 +11,9 @@
 
 namespace Rapsys\UserBundle\Entity;
 
+use Doctrine\Common\Collections\Collection;
 use Doctrine\Common\Collections\ArrayCollection;
+use Doctrine\ORM\Event\PreUpdateEventArgs;
 
 use Rapsys\UserBundle\Entity\User;
 
@@ -20,37 +22,36 @@ use Rapsys\UserBundle\Entity\User;
  */
 class Civility {
        /**
-        * @var integer
+        * Primary key
         */
-       protected $id;
+       protected ?int $id = null;
 
        /**
-        * @var string
+        * Create datetime
         */
-       protected $title;
+       protected \DateTimeImmutable $created;
 
        /**
-        * @var \DateTime
+        * Update datetime
         */
-       protected $created;
+       protected \DateTimeImmutable $updated;
 
        /**
-        * @var \DateTime
+        * Users collection
         */
-       protected $updated;
-
-       /**
-        * @var ArrayCollection
-        */
-       protected $users;
+       protected Collection $users;
 
        /**
         * Constructor
         *
         * @param string $title The civility name
         */
-       public function __construct(string $title) {
-               $this->title = $title;
+       public function __construct(protected string $title) {
+               //Set defaults
+               $this->created = new \DateTimeImmutable('now');
+               $this->updated = new \DateTimeImmutable('now');
+
+               //Set collections
                $this->users = new ArrayCollection();
        }
 
@@ -59,7 +60,7 @@ class Civility {
         *
         * @return integer
         */
-       public function getId(): int {
+       public function getId(): ?int {
                return $this->id;
        }
 
@@ -88,11 +89,11 @@ class Civility {
        /**
         * Set created
         *
-        * @param \DateTime $created
+        * @param \DateTimeImmutable $created
         *
         * @return Civility
         */
-       public function setCreated(\DateTime $created): Civility {
+       public function setCreated(\DateTimeImmutable $created): Civility {
                $this->created = $created;
 
                return $this;
@@ -101,20 +102,20 @@ class Civility {
        /**
         * Get created
         *
-        * @return \DateTime
+        * @return \DateTimeImmutable
         */
-       public function getCreated(): \DateTime {
+       public function getCreated(): \DateTimeImmutable {
                return $this->created;
        }
 
        /**
         * Set updated
         *
-        * @param \DateTime $updated
+        * @param \DateTimeImmutable $updated
         *
         * @return Civility
         */
-       public function setUpdated(\DateTime $updated): Civility {
+       public function setUpdated(\DateTimeImmutable $updated): Civility {
                $this->updated = $updated;
 
                return $this;
@@ -123,9 +124,9 @@ class Civility {
        /**
         * Get updated
         *
-        * @return \DateTime
+        * @return \DateTimeImmutable
         */
-       public function getUpdated(): \DateTime {
+       public function getUpdated(): \DateTimeImmutable {
                return $this->updated;
        }
 
@@ -160,6 +161,17 @@ class Civility {
                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 \DateTimeImmutable('now'));
+               }
+       }
+
        /**
         * Returns a string representation of the title
         *