61 lines
1.7 KiB
PHP
61 lines
1.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Chatbot;
|
|
|
|
use Symfony\Component\Clock\ClockInterface;
|
|
|
|
/**
|
|
* The named travel periods of the website search ("Reisezeitraum"), from the
|
|
* `chatbot_travel_periods` parameter.
|
|
*
|
|
* Duplicated from the TYPO3 search configuration on purpose, and updated together with it
|
|
* each season. Periods may overlap. A period whose end has passed is no longer offered.
|
|
*/
|
|
class TravelPeriods
|
|
{
|
|
/**
|
|
* @param list<array{label: string, from: string, to: string}> $periods
|
|
*/
|
|
public function __construct(
|
|
private readonly array $periods,
|
|
private readonly ClockInterface $clock,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* The date range of a current period, matched case-insensitively, or null.
|
|
*
|
|
* @return array{from: \DateTimeImmutable, to: \DateTimeImmutable}|null
|
|
*/
|
|
public function find(string $label): ?array
|
|
{
|
|
foreach ($this->current() as $period) {
|
|
if (0 === strcasecmp($period['label'], trim($label))) {
|
|
return ['from' => new \DateTimeImmutable($period['from']), 'to' => new \DateTimeImmutable($period['to'])];
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* @return list<string>
|
|
*/
|
|
public function labels(): array
|
|
{
|
|
return array_column($this->current(), 'label');
|
|
}
|
|
|
|
/**
|
|
* @return list<array{label: string, from: string, to: string}>
|
|
*/
|
|
private function current(): array
|
|
{
|
|
$today = $this->clock->now()->setTimezone(new \DateTimeZone('Europe/Berlin'))->format('Y-m-d');
|
|
|
|
return array_values(array_filter($this->periods, static fn (array $period): bool => $period['to'] >= $today));
|
|
}
|
|
}
|