fix: correct sort order of pickups and drop-offs
This commit is contained in:
@@ -62,23 +62,87 @@ class PickupLoader extends AbstractLoader
|
||||
public function patchPickupsDetails(Travel $travel): void
|
||||
{
|
||||
$travel->pickups = $this->patchAndSortPickups($travel->pickups);
|
||||
$travel->dropOffs = $this->patchAndSortPickups($travel->dropOffs);
|
||||
$travel->dropOffs = $this->patchAndOrderDropOffs($travel->dropOffs, $travel->pickups);
|
||||
}
|
||||
|
||||
private function patchAndSortPickups(array $pickups): array
|
||||
{
|
||||
foreach ($pickups as $pickupId => $pickup) {
|
||||
$pickupData = $this->loadById($pickupId);
|
||||
$this->enrichPickupDetails($pickups);
|
||||
uasort($pickups, fn (Pickup $a, Pickup $b): int => $this->comparePickupsByTime($a, $b));
|
||||
|
||||
return $pickups;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enriches drop-off details and determines their order.
|
||||
*
|
||||
* When drop-offs carry their own time data, they are sorted chronologically.
|
||||
* Otherwise the return journey order is derived by reversing the chronologically
|
||||
* sorted outbound pickup order. Any drop-offs without a matching pickup ID are
|
||||
* appended at the end.
|
||||
*
|
||||
* @param array<int, Pickup> $dropOffs Drop-offs indexed by ID
|
||||
* @param array<int, Pickup> $sortedPickups Already sorted pickups indexed by ID
|
||||
*
|
||||
* @return array<int, Pickup> Ordered drop-offs
|
||||
*/
|
||||
private function patchAndOrderDropOffs(array $dropOffs, array $sortedPickups): array
|
||||
{
|
||||
$this->enrichPickupDetails($dropOffs);
|
||||
|
||||
if ($this->hasTimeData($dropOffs)) {
|
||||
uasort($dropOffs, fn (Pickup $a, Pickup $b): int => $this->comparePickupsByTime($a, $b));
|
||||
|
||||
return $dropOffs;
|
||||
}
|
||||
|
||||
// Fallback: order drop-offs in reverse of the sorted pickup route
|
||||
$reversedPickupIds = array_reverse(array_keys($sortedPickups));
|
||||
|
||||
$ordered = [];
|
||||
foreach ($reversedPickupIds as $id) {
|
||||
if (isset($dropOffs[$id])) {
|
||||
$ordered[$id] = $dropOffs[$id];
|
||||
}
|
||||
}
|
||||
|
||||
// Append any drop-offs not present in pickups
|
||||
foreach ($dropOffs as $id => $dropOff) {
|
||||
if (false === isset($ordered[$id])) {
|
||||
$ordered[$id] = $dropOff;
|
||||
}
|
||||
}
|
||||
|
||||
return $ordered;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, Pickup> $pickups
|
||||
*/
|
||||
private function hasTimeData(array $pickups): bool
|
||||
{
|
||||
foreach ($pickups as $pickup) {
|
||||
if (null !== $pickup->time) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, Pickup> $pickups Pickups indexed by ID (modified in place)
|
||||
*/
|
||||
private function enrichPickupDetails(array $pickups): void
|
||||
{
|
||||
foreach ($pickups as $pickup) {
|
||||
$pickupData = $this->loadById($pickup->id);
|
||||
|
||||
$pickup->code = $pickupData->code;
|
||||
$pickup->postalCode = $pickupData->postalCode;
|
||||
$pickup->city = $pickupData->city;
|
||||
$pickup->street = $pickupData->street;
|
||||
}
|
||||
|
||||
uasort($pickups, fn (Pickup $a, Pickup $b): int => $this->comparePickupsByTime($a, $b));
|
||||
|
||||
return $pickups;
|
||||
}
|
||||
|
||||
private function comparePickupsByTime(Pickup $a, Pickup $b): int
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user