Untitled
<?php namespace App\Services\TripPlanning; use App\Models\Stajaliste; class SpiderService { private $tripPlanningService; public function __construct() { $this->tripPlanningService = new TripPlanningService(); } public function findRoutes(float $lat1, float $lon1, float $lat2, float $lon2, int $depth = 1): array { $with = 'old_daljinari.linija.old_daljinari.stajaliste'; if ($depth > 1) { for ($i = 1; $i < $depth; $i++) { $with .= '.close_stops.old_daljinari.linija.old_daljinari.stajaliste'; } } $startStops = $this->tripPlanningService->findNearbyStops($lat1, $lon1, 400, 10, $with); $endStops = $this->tripPlanningService->findNearbyStops($lat2, $lon2, 400, 15); $endStopsIds = $endStops->pluck('id')->toArray(); $routes = []; foreach ($startStops as $startStop) { $initialSegment = [ 'startStop' => $startStop, 'endStop' => null, 'segments' => [ (object)[ 'type' => 'walking', 'startPoint' => [$lat1, $lon1], 'endPoint' => [$startStop->sirina, $startStop->duzina] ] ] ]; // Explore routes starting from the current stop $this->exploreRoutesFromStop($startStop, $endStops, $routes, $initialSegment, $depth); // Explore routes starting from close stops with an added walking segment foreach ($startStop->close_stops as $closeStop) { $segmentWithWalking = $initialSegment; $segmentWithWalking['segments'][] = (object)[ 'type' => 'walking', 'startPoint' => [$startStop->sirina, $startStop->duzina], 'endPoint' => [$closeStop->sirina, $closeStop->duzina] ]; $this->exploreRoutesFromStop($closeStop, $endStops, $routes, $segmentWithWalking, $depth); } // Unset the startStop object to free up memory unset($startStop); } return $routes; } protected function exploreRoutesFromStop($currentStop, $endStops, &$routes, $currentRoute, $depth) { foreach ($currentStop->old_daljinari as $oldDaljinar) { if (!$oldDaljinar->linija) { continue; } $nextStops = $oldDaljinar->linija->old_daljinari ->where('smer', $oldDaljinar->smer) ->where('redni_broj_stajalista', '>', $oldDaljinar->redni_broj_stajalista); foreach ($nextStops as $nextStop) { $routeSegment = $currentRoute; if (!$nextStop->stajaliste) { continue; } $routeSegment['segments'][] = (object)[ 'type' => 'bus', 'line' => $oldDaljinar->linija->kod_linije, 'startPoint' => [$currentStop->sirina, $currentStop->duzina], 'endPoint' => [$nextStop->stajaliste->sirina, $nextStop->stajaliste->duzina] ]; if ($endStops->contains('id', $nextStop->stajaliste->id)) { // Complete the route if an end stop is found $routeSegment['endStop'] = $nextStop->stajaliste; $routes[] = $routeSegment; } else { // Recursively explore further routes if depth is greater than 1 if ($depth > 1) { $this->exploreRoutesFromStop($nextStop->stajaliste, $endStops, $routes, $routeSegment, $depth - 1); } } // Unset the nextStop and routeSegment to free up memory unset($nextStop, $routeSegment); } // Unset the oldDaljinar and nextStops to free up memory unset($oldDaljinar, $nextStops); } // Unset the currentStop object to free up memory unset($currentStop); } }
Leave a Comment