/** * Gets an appropriate station from the new iRail API * * @param $name * @param $lang * @return Station * @throws Exception */ public static function getStationFromName($name, $lang) { //first check if it wasn't by any chance an id if (substr($name, 0, 1) == "0" || substr($name, 0, 7) == "BE.NMBS" || substr($name, 0, 7) == "http://") { return stations::getStationFromID($name, $lang); } $name = html_entity_decode($name, ENT_COMPAT | ENT_HTML401, "UTF-8"); $name = preg_replace("/[ ]?\\([a-zA-Z]+\\)/", "", $name); $name = str_replace(" [NMBS/SNCB]", "", $name); $name = explode("/", $name); $name = trim($name[0]); $stationsgraph = irail\stations\Stations::getStations($name); if (!isset($stationsgraph->{'@graph'}[0])) { throw new Exception("Could not match " . $name . " with a station id in iRail. Please report this issue at https://github.com/irail/stations/issues/new"); } $station = $stationsgraph->{'@graph'}[0]; //or find exact match using ugly breaks and strlen foreach ($stationsgraph->{'@graph'} as $stationitem) { if (strlen($stationitem->name) === strlen($name)) { $station = $stationitem; break; } elseif (isset($stationitem->alternative) && is_array($stationitem->alternative)) { foreach ($stationitem->alternative as $alt) { if (strlen($alt->{"@value"}) === strlen($name)) { $station = $stationitem; break; } } } elseif (isset($stationitem->alternative) && strlen($stationitem->alternative->{"@value"}) === strlen($name)) { $station = $stationitem; break; } } return stations::transformNewToOldStyle($station, $lang); }
public static function fillDataRoot($dataroot, $request) { //detect whether from was an id and change from accordingly $from = $request->getFrom(); if (sizeof(explode(".", $request->getFrom())) > 1) { $from = stations::getStationFromID($request->getFrom(), $request->getLang()); $from = $from->name; } $to = $request->getTo(); if (sizeof(explode(".", $request->getTo())) > 1) { $to = stations::getStationFromID($request->getTo(), $request->getLang()); $to = $to->name; } $dataroot->connection = connections::scrapeConnections($from, $to, $request->getTime(), $request->getDate(), $request->getResults(), $request->getLang(), $request->getTimeSel(), $request->getTypeOfTransport()); }
/** * @param $dataroot * @param $request * @throws Exception */ public static function fillDataRoot($dataroot, $request) { //detect if this is an id or a station if (sizeof(explode(".", $request->getStation())) > 1) { $dataroot->station = stations::getStationFromID($request->getStation(), $request->getLang()); } else { $dataroot->station = stations::getStationFromName($request->getStation(), $request->getLang()); } if ($request->getArrdep() == "ARR") { $xml = liveboard::fetchData($dataroot->station, $request->getTime(), $request->getLang(), "A"); $dataroot->arrival = liveboard::parseData($xml, $request->getLang()); } elseif ($request->getArrdep() == "DEP") { $xml = liveboard::fetchData($dataroot->station, $request->getTime(), $request->getLang(), "D"); $dataroot->departure = liveboard::parseData($xml, $request->getLang()); } else { throw new Exception("Not a good timeSel value: try ARR or DEP", 300); } }
/** * @param $html * @param $id * @param $lang * @return null|Vehicle * @throws Exception */ private static function getVehicleData($html, $id, $lang) { // determine the location of the vehicle $test = $html->getElementById('tq_trainroute_content_table_alteAnsicht'); if (!is_object($test)) { throw new Exception('Vehicle not found', 500); } // catch errors $nodes = $html->getElementById('tq_trainroute_content_table_alteAnsicht')->getElementByTagName('table')->children; for ($i = 1; $i < count($nodes); $i++) { $node = $nodes[$i]; if (!count($node->attr)) { continue; } // row with no class-attribute contain no data if (count($node->children[3]->find('a'))) { $as = $node->children[3]->find('a'); $stationname = reset($as[0]->nodes[0]->_); } else { // Foreign station, no anchorlink $stationname = reset($node->children[3]->nodes[0]->_); } $locationX = 0; $locationY = 0; // Station ID can be parsed from the station URL if (isset($node->children[3]->children[0])) { $link = $node->children[3]->children[0]->{'attr'}['href']; // With capital S if (strpos($link, 'StationId=')) { $nr = substr($link, strpos($link, 'StationId=') + strlen('StationId=')); } else { $nr = substr($link, strpos($link, 'stationId=') + strlen('stationId=')); } $nr = substr($nr, 0, strlen($nr) - 1); // delete ampersand on the end $stationId = '00' . $nr; $station = stations::getStationFromID($stationId, $lang); } else { $station = stations::getStationFromName($stationname, $lang); } if (isset($station)) { $locationX = $station->locationX; $locationY = $station->locationY; } $vehicle = new Vehicle(); $vehicle->name = $id; $vehicle->locationX = $locationX; $vehicle->locationY = $locationY; return $vehicle; } return; }