fix: correct sort order of pickups and drop-offs
This commit is contained in:
1 parent
f99a2d99a2
commit
44a9de7db7
4 files changed
+331
-9
No files matched your search
@@ -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
|
||||
|
||||
Reference in new issue
Block a user