Skip to content

Commit

Permalink
Add IdentityIntTrait with unsigned int. Add static AnzuApp::date func…
Browse files Browse the repository at this point in the history
…tion.
  • Loading branch information
sakulb committed Jul 24, 2023
1 parent 3fca06e commit d4ee7c7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/AnzuApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,20 @@ public static function getAppDate(): DateTimeImmutable
return self::$appDate;
}

/**
* Create new DateTimeImmutable (or use appDate) without IDE warning of unhandled exception.
*
* @noinspection PhpUnhandledExceptionInspection
*/
public static function date(?string $datetime = null): DateTimeImmutable
{
if (null === $datetime) {
return self::getAppDate();
}

return new DateTimeImmutable($datetime);
}

/**
* Get dateTime of start of unix epoch.
*/
Expand Down
50 changes: 50 additions & 0 deletions src/Entity/Traits/IdentityIntTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace AnzuSystems\Contracts\Entity\Traits;

use AnzuSystems\SerializerBundle\Attributes\Serialize;
use AnzuSystems\Contracts\Entity\Interfaces\BaseIdentifiableInterface;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

trait IdentityIntTrait
{
use NamedResourceTrait;

/**
* Primary key of this item.
*/
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
#[ORM\Column(type: Types::INTEGER, options: ['unsigned' => true])]
#[Serialize]
protected ?int $id = null;

public function getId(): ?int
{
return $this->id;
}

public function setId(?int $id): static
{
$this->id = $id;

return $this;
}

public function is(BaseIdentifiableInterface $identifiable): bool
{
if ($identifiable::getResourceName() === $this->getResourceName()) {
return $identifiable->getId() === $this->getId();
}

return false;
}

public function isNot(BaseIdentifiableInterface $identifiable): bool
{
return false === $this->is($identifiable);
}
}

0 comments on commit d4ee7c7

Please sign in to comment.