fix: correct sort order of pickups and drop-offs

This commit is contained in:
Björn Fromme
2026-02-19 12:56:06 +01:00
parent 08aaff1e2d
commit 4529fe5472
4 changed files with 331 additions and 9 deletions
+49 -2
View File
@@ -267,8 +267,9 @@ class TravelParser extends AbstractParser
public function getPickups(Crawler $node, ?\DateTimeImmutable $defaultDate = null, bool $isOutbound = true): array
{
$pickups = [];
$timeOnlyIds = [];
$node->each(function (Crawler $pickupNode) use (&$pickups, $defaultDate, $isOutbound) {
$node->each(function (Crawler $pickupNode) use (&$pickups, &$timeOnlyIds, $defaultDate, $isOutbound) {
$pickupId = (int) $pickupNode->attr('idbuspro');
$pickup = new Pickup();
@@ -285,12 +286,19 @@ class TravelParser extends AbstractParser
// parse date and time only for direction 'to' indicated by provided default date
if (null !== $defaultDate) {
$pickup->time = $this->stringToDateTimeFuzzy($pickupNode->attr('zeit'), $defaultDate);
$timeValue = $pickupNode->attr('zeit');
$pickup->time = $this->stringToDateTimeFuzzy($timeValue, $defaultDate);
if (null !== $timeValue && 1 === preg_match('/^\d{2}:\d{2}$/', $timeValue)) {
$timeOnlyIds[] = $pickupId;
}
}
$pickups[$pickupId] = $pickup;
});
$this->adjustOvernightTimes($pickups, $timeOnlyIds);
return $pickups;
}
@@ -440,4 +448,43 @@ class TravelParser extends AbstractParser
}
}
}
/**
* Adjusts time-only pickup entries for overnight bus routes.
*
* When a bus route crosses midnight, time-only entries (e.g. "01:00") are
* initially stamped with the departure date. This method detects overnight
* scenarios by comparing time-only entries against the latest full-datetime
* entry and adds one day where needed.
*
* @param array<int, Pickup> $pickups Pickups indexed by ID (modified in place)
* @param array<int> $timeOnlyIds IDs of pickups parsed from time-only values
*/
private function adjustOvernightTimes(array $pickups, array $timeOnlyIds): void
{
if (true === empty($timeOnlyIds)) {
return;
}
// Find latest datetime among full-datetime entries
$latestFullDateTime = null;
foreach ($pickups as $id => $pickup) {
if (null !== $pickup->time && false === in_array($id, $timeOnlyIds, true)) {
if (null === $latestFullDateTime || $pickup->time > $latestFullDateTime) {
$latestFullDateTime = $pickup->time;
}
}
}
if (null === $latestFullDateTime) {
return;
}
// Adjust time-only entries that fall before the latest full-datetime
foreach ($timeOnlyIds as $id) {
if (isset($pickups[$id]) && null !== $pickups[$id]->time && $pickups[$id]->time < $latestFullDateTime) {
$pickups[$id]->time = $pickups[$id]->time->modify('+1 day');
}
}
}
}