50 lines
1 KiB
PHP
50 lines
1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Domain\Models\ValueObjects;
|
|
|
|
use Carbon\CarbonInterval;
|
|
|
|
final readonly class PickupTimeSlot implements \Stringable
|
|
{
|
|
public const LENGTH = 10;
|
|
|
|
private \DateTimeInterface $at;
|
|
|
|
private \DateInterval $for;
|
|
|
|
public function __construct(
|
|
\DateTimeInterface $at,
|
|
\DateInterval $for,
|
|
) {
|
|
$this->at = clone $at;
|
|
$this->for = clone $for;
|
|
}
|
|
|
|
public function getAt(): \DateTimeImmutable
|
|
{
|
|
return $this->at instanceof \DateTimeImmutable
|
|
? $this->at
|
|
: \DateTimeImmutable::createFromInterface($this->at);
|
|
}
|
|
|
|
public function getFor(): \DateInterval
|
|
{
|
|
return $this->for;
|
|
}
|
|
|
|
public function getEndsAt(): \DateTimeImmutable
|
|
{
|
|
return $this->getAt()->add($this->for);
|
|
}
|
|
|
|
public function __toString(): string
|
|
{
|
|
return sprintf(
|
|
'%s (%s)',
|
|
$this->getAt()->format(\DateTimeInterface::RFC3339),
|
|
CarbonInterval::instance($this->for)->spec()
|
|
);
|
|
}
|
|
}
|