32 lines
836 B
PHP
32 lines
836 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Booking;
|
|
|
|
/**
|
|
* The Y-m-d day identifier that every board route speaks.
|
|
*/
|
|
final class DateParam
|
|
{
|
|
/** Route requirement, so a malformed day 404s instead of silently falling back to today. */
|
|
public const PATTERN = '\d{4}-\d{2}-\d{2}';
|
|
|
|
public static function parse(?string $value): ?\DateTimeImmutable
|
|
{
|
|
if (null === $value || '' === $value) {
|
|
return null;
|
|
}
|
|
|
|
// The leading "!" resets the time part, so the result is midnight rather than "now".
|
|
$date = \DateTimeImmutable::createFromFormat('!Y-m-d', $value);
|
|
|
|
return false === $date ? null : $date;
|
|
}
|
|
|
|
public static function parseOrToday(?string $value): \DateTimeImmutable
|
|
{
|
|
return self::parse($value) ?? new \DateTimeImmutable('today');
|
|
}
|
|
}
|