/** * @return stdClass */ public function jsonSerialize() { $json = new stdClass(); $json->origin = $this->journey->getOrigin(); $json->destination = $this->journey->getDestination(); $json->departureTime = $this->getTime($this->journey->getDepartureTime()); $json->arrivalTime = $this->getTime($this->journey->getArrivalTime()); $json->legs = array_map([$this, 'getLeg'], $this->journey->getLegs()); return $json; }
/** * Slightly modified version of the CSA that returns the shortest journeys * to each station from the given origin. * * @param string $origin * @param int $departureTime * @return array */ public function getShortestPathTree($origin, $departureTime) { // set earliest arrivals $this->getEarliestArrivalTree($origin, $departureTime); // storage for the optimal results $bestJourneys = []; // filter out non timetable connection only journeys $nextDepartureTime = PHP_INT_MAX; foreach ($this->arrivals as $destination => $time) { $legs = $this->getLegsFromConnections($origin, $destination); if (count($legs) > 0) { $journey = new Journey($legs); $bestJourneys[$destination] = [$time => $journey]; $departureTime = $journey->getDepartureTime(); $nextDepartureTime = $departureTime > 0 ? min($nextDepartureTime, $departureTime) : $nextDepartureTime; } } while (count($this->timetable) > 0) { $this->getEarliestArrivalTree($origin, $nextDepartureTime + 1); $nextDepartureTime = PHP_INT_MAX; // foreach of the not yet optimal journeys foreach ($this->arrivals as $destination => $time) { $legs = $this->getLegsFromConnections($origin, $destination); if (count($legs) > 0) { $journey = new Journey($legs); $departureTime = $journey->getDepartureTime(); $nextDepartureTime = $departureTime > 0 ? min($nextDepartureTime, $departureTime) : $nextDepartureTime; $bestJourneys[$destination][$time] = $journey; } } } return $bestJourneys; }