implement immutable datetime

This commit is contained in:
Dennis Eichhorn 2020-09-10 20:18:02 +02:00
parent 6035870c1f
commit bea2d1ed0b
6 changed files with 17 additions and 19 deletions

View File

@ -101,10 +101,10 @@ class Account implements \JsonSerializable, ArrayableInterface
/** /**
* Last activity. * Last activity.
* *
* @var \DateTime * @var \DateTimeImmutable
* @since 1.0.0 * @since 1.0.0
*/ */
protected \DateTime $createdAt; protected \DateTimeImmutable $createdAt;
/** /**
* Groups. * Groups.
@ -159,7 +159,7 @@ class Account implements \JsonSerializable, ArrayableInterface
*/ */
public function __construct(int $id = 0) public function __construct(int $id = 0)
{ {
$this->createdAt = new \DateTime('now'); $this->createdAt = new \DateTimeImmutable('now');
$this->lastActive = new \DateTime('now'); $this->lastActive = new \DateTime('now');
$this->id = $id; $this->id = $id;
$this->localization = new Localization(); $this->localization = new Localization();
@ -423,11 +423,11 @@ class Account implements \JsonSerializable, ArrayableInterface
/** /**
* Get last activity. * Get last activity.
* *
* @return \DateTime * @return \DateTimeInterface
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public function getLastActive() : \DateTime public function getLastActive() : \DateTimeInterface
{ {
return $this->lastActive ?? $this->getCreatedAt(); return $this->lastActive ?? $this->getCreatedAt();
} }
@ -435,11 +435,11 @@ class Account implements \JsonSerializable, ArrayableInterface
/** /**
* Get created at. * Get created at.
* *
* @return \DateTime * @return \DateTimeInterface
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public function getCreatedAt() : \DateTime public function getCreatedAt() : \DateTimeInterface
{ {
return $this->createdAt; return $this->createdAt;
} }

View File

@ -1065,12 +1065,10 @@ class DataMapperAbstract implements DataMapperInterface
return (bool) $value; return (bool) $value;
} elseif ($type === 'DateTime' || $type === 'DateTimeImmutable') { } elseif ($type === 'DateTime' || $type === 'DateTimeImmutable') {
return $value === null ? null : $value->format('Y-m-d H:i:s'); 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); return (string) \json_encode($value);
} elseif ($type === 'Serializable') { } elseif ($type === 'Serializable') {
return $value->serialize(); return $value->serialize();
} elseif ($value instanceof \JsonSerializable) {
return (string) \json_encode($value->jsonSerialize());
} elseif (\is_object($value) && \method_exists($value, 'getId')) { } elseif (\is_object($value) && \method_exists($value, 'getId')) {
return $value->getId(); return $value->getId();
} }

View File

@ -128,7 +128,7 @@ interface ContainerInterface
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public function getCreatedAt() : \DateTime; public function getCreatedAt() : \DateTimeInterface;
/** /**
* Get the datetime when the resource got last modified. * Get the datetime when the resource got last modified.

View File

@ -66,7 +66,7 @@ abstract class FileAbstract implements ContainerInterface
* @var \DateTime * @var \DateTime
* @since 1.0.0 * @since 1.0.0
*/ */
protected \DateTime $createdAt; protected \DateTimeImmutable $createdAt;
/** /**
* Last changed at. * Last changed at.
@ -104,7 +104,7 @@ abstract class FileAbstract implements ContainerInterface
$this->path = \rtrim($path, '/\\'); $this->path = \rtrim($path, '/\\');
$this->name = \basename($path); $this->name = \basename($path);
$this->createdAt = new \DateTime('now'); $this->createdAt = new \DateTimeImmutable('now');
$this->changedAt = new \DateTime('now'); $this->changedAt = new \DateTime('now');
} }
@ -151,7 +151,7 @@ abstract class FileAbstract implements ContainerInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCreatedAt() : \DateTime public function getCreatedAt() : \DateTimeInterface
{ {
return $this->createdAt; return $this->createdAt;
} }

View File

@ -66,7 +66,7 @@ abstract class FileAbstract implements ContainerInterface
* @var \DateTime * @var \DateTime
* @since 1.0.0 * @since 1.0.0
*/ */
protected \DateTime $createdAt; protected \DateTimeImmutable $createdAt;
/** /**
* Last changed at. * Last changed at.
@ -104,7 +104,7 @@ abstract class FileAbstract implements ContainerInterface
$this->path = \rtrim($path, '/\\'); $this->path = \rtrim($path, '/\\');
$this->name = \basename($path); $this->name = \basename($path);
$this->createdAt = new \DateTime('now'); $this->createdAt = new \DateTimeImmutable('now');
$this->changedAt = new \DateTime('now'); $this->changedAt = new \DateTime('now');
} }
@ -151,7 +151,7 @@ abstract class FileAbstract implements ContainerInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCreatedAt() : \DateTime public function getCreatedAt() : \DateTimeInterface
{ {
return $this->createdAt; return $this->createdAt;
} }

View File

@ -105,8 +105,8 @@ class AccountTest extends \PHPUnit\Framework\TestCase
self::assertEquals([], $account->getPermissions()); self::assertEquals([], $account->getPermissions());
self::assertInstanceOf('\DateTime', $account->getLastActive()); self::assertInstanceOf('\DateTimeInterface', $account->getLastActive());
self::assertInstanceOf('\DateTime', $account->getCreatedAt()); self::assertInstanceOf('\DateTimeImmutable', $account->getCreatedAt());
$array = $account->toArray(); $array = $account->toArray();
self::assertIsArray($array); self::assertIsArray($array);