/** * @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; }
/** * @param OutputInterface $out * @param array $locations * @param Journey $journey */ private function displayRoute(OutputInterface $out, array $locations, Journey $journey) { $this->outputHeading($out, "Route"); $out->writeln("Duration " . $journey->getDuration()); foreach ($journey->getLegs() as $leg) { if (!$leg->isTransfer()) { foreach ($leg->getConnections() as $connection) { $origin = sprintf('%-30s', $locations[$connection->getOrigin()]); $destination = sprintf('%30s', $locations[$connection->getDestination()]); $out->writeln(gmdate('H:i', $connection->getDepartureTime()) . ' ' . $origin . ' ' . sprintf('%-6s', $connection->getService()) . ' ' . $destination . ' ' . gmdate('H:i', $connection->getArrivalTime())); } } else { $origin = sprintf('%-30s', $locations[$leg->getOrigin()]); $destination = sprintf('%30s', $locations[$leg->getDestination()]); $out->writeln(sprintf('%-6s', $leg->getMode()) . $origin . ' to' . $destination . " (" . $leg->getDuration() / 60 . "mins)"); } } }
/** * 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; }