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.
*
* @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;
}

View File

@ -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();
}

View File

@ -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.

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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);