ldc-boutique-cnc-sable/domain/Models/ValueObjects/DatePeriod.php

41 lines
1.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Domain\Models\ValueObjects;
use Webmozart\Assert\Assert;
final readonly class DatePeriod
{
public ?\DateTimeImmutable $start;
public ?\DateTimeImmutable $end;
public function __construct(
?\DateTimeInterface $start = null,
?\DateTimeInterface $end = null,
) {
$this->start = $start !== null ? \DateTimeImmutable::createFromInterface($start) : null;
$this->end = $end !== null ? \DateTimeImmutable::createFromInterface($end) : null;
if ($this->start !== null && $this->end !== null) {
Assert::true($this->start <= $this->end, 'The start date must be before or equal to the end date.');
}
}
public function isActiveAt(?\DateTimeInterface $date = null): bool
{
$target = $date !== null ? \DateTimeImmutable::createFromInterface($date) : new \DateTimeImmutable();
if ($this->start !== null && $target < $this->start) {
return false;
}
if ($this->end !== null && $target > $this->end) {
return false;
}
return true;
}
}