From bea2d1ed0bc22c98166f5cc2e4d67cab1a6971f6 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Thu, 10 Sep 2020 20:18:02 +0200 Subject: [PATCH] implement immutable datetime --- Account/Account.php | 14 +++++++------- DataStorage/Database/DataMapperAbstract.php | 4 +--- System/File/ContainerInterface.php | 2 +- System/File/Ftp/FileAbstract.php | 6 +++--- System/File/Local/FileAbstract.php | 6 +++--- tests/Account/AccountTest.php | 4 ++-- 6 files changed, 17 insertions(+), 19 deletions(-) diff --git a/Account/Account.php b/Account/Account.php index 59b68db4b..a22ed09a6 100644 --- a/Account/Account.php +++ b/Account/Account.php @@ -101,10 +101,10 @@ class Account implements \JsonSerializable, ArrayableInterface /** * Last activity. * - * @var \DateTime + * @var \DateTimeImmutable * @since 1.0.0 */ - protected \DateTime $createdAt; + protected \DateTimeImmutable $createdAt; /** * Groups. @@ -159,7 +159,7 @@ class Account implements \JsonSerializable, ArrayableInterface */ public function __construct(int $id = 0) { - $this->createdAt = new \DateTime('now'); + $this->createdAt = new \DateTimeImmutable('now'); $this->lastActive = new \DateTime('now'); $this->id = $id; $this->localization = new Localization(); @@ -423,11 +423,11 @@ class Account implements \JsonSerializable, ArrayableInterface /** * Get last activity. * - * @return \DateTime + * @return \DateTimeInterface * * @since 1.0.0 */ - public function getLastActive() : \DateTime + public function getLastActive() : \DateTimeInterface { return $this->lastActive ?? $this->getCreatedAt(); } @@ -435,11 +435,11 @@ class Account implements \JsonSerializable, ArrayableInterface /** * Get created at. * - * @return \DateTime + * @return \DateTimeInterface * * @since 1.0.0 */ - public function getCreatedAt() : \DateTime + public function getCreatedAt() : \DateTimeInterface { return $this->createdAt; } diff --git a/DataStorage/Database/DataMapperAbstract.php b/DataStorage/Database/DataMapperAbstract.php index da1563ccf..dd0eb5553 100644 --- a/DataStorage/Database/DataMapperAbstract.php +++ b/DataStorage/Database/DataMapperAbstract.php @@ -1065,12 +1065,10 @@ class DataMapperAbstract implements DataMapperInterface return (bool) $value; } elseif ($type === 'DateTime' || $type === 'DateTimeImmutable') { return $value === null ? null : $value->format('Y-m-d H:i:s'); - } elseif ($type === 'Json' || $type === 'jsonSerializable') { + } elseif ($type === 'Json' || $value instanceof \JsonSerializable) { return (string) \json_encode($value); } elseif ($type === 'Serializable') { return $value->serialize(); - } elseif ($value instanceof \JsonSerializable) { - return (string) \json_encode($value->jsonSerialize()); } elseif (\is_object($value) && \method_exists($value, 'getId')) { return $value->getId(); } diff --git a/System/File/ContainerInterface.php b/System/File/ContainerInterface.php index 67871909c..834720048 100644 --- a/System/File/ContainerInterface.php +++ b/System/File/ContainerInterface.php @@ -128,7 +128,7 @@ interface ContainerInterface * * @since 1.0.0 */ - public function getCreatedAt() : \DateTime; + public function getCreatedAt() : \DateTimeInterface; /** * Get the datetime when the resource got last modified. diff --git a/System/File/Ftp/FileAbstract.php b/System/File/Ftp/FileAbstract.php index 1a2a9a651..d0f782f60 100644 --- a/System/File/Ftp/FileAbstract.php +++ b/System/File/Ftp/FileAbstract.php @@ -66,7 +66,7 @@ abstract class FileAbstract implements ContainerInterface * @var \DateTime * @since 1.0.0 */ - protected \DateTime $createdAt; + protected \DateTimeImmutable $createdAt; /** * Last changed at. @@ -104,7 +104,7 @@ abstract class FileAbstract implements ContainerInterface $this->path = \rtrim($path, '/\\'); $this->name = \basename($path); - $this->createdAt = new \DateTime('now'); + $this->createdAt = new \DateTimeImmutable('now'); $this->changedAt = new \DateTime('now'); } @@ -151,7 +151,7 @@ abstract class FileAbstract implements ContainerInterface /** * {@inheritdoc} */ - public function getCreatedAt() : \DateTime + public function getCreatedAt() : \DateTimeInterface { return $this->createdAt; } diff --git a/System/File/Local/FileAbstract.php b/System/File/Local/FileAbstract.php index 3b5119d95..d350ffe29 100644 --- a/System/File/Local/FileAbstract.php +++ b/System/File/Local/FileAbstract.php @@ -66,7 +66,7 @@ abstract class FileAbstract implements ContainerInterface * @var \DateTime * @since 1.0.0 */ - protected \DateTime $createdAt; + protected \DateTimeImmutable $createdAt; /** * Last changed at. @@ -104,7 +104,7 @@ abstract class FileAbstract implements ContainerInterface $this->path = \rtrim($path, '/\\'); $this->name = \basename($path); - $this->createdAt = new \DateTime('now'); + $this->createdAt = new \DateTimeImmutable('now'); $this->changedAt = new \DateTime('now'); } @@ -151,7 +151,7 @@ abstract class FileAbstract implements ContainerInterface /** * {@inheritdoc} */ - public function getCreatedAt() : \DateTime + public function getCreatedAt() : \DateTimeInterface { return $this->createdAt; } diff --git a/tests/Account/AccountTest.php b/tests/Account/AccountTest.php index c3ccca6bd..d855049ac 100644 --- a/tests/Account/AccountTest.php +++ b/tests/Account/AccountTest.php @@ -105,8 +105,8 @@ class AccountTest extends \PHPUnit\Framework\TestCase self::assertEquals([], $account->getPermissions()); - self::assertInstanceOf('\DateTime', $account->getLastActive()); - self::assertInstanceOf('\DateTime', $account->getCreatedAt()); + self::assertInstanceOf('\DateTimeInterface', $account->getLastActive()); + self::assertInstanceOf('\DateTimeImmutable', $account->getCreatedAt()); $array = $account->toArray(); self::assertIsArray($array);