public static function createFromXml(\SimpleXMLElement $xml, \DateTime $date, Stop $obj = null) { if (!$obj) { $obj = new Stop(); } $dateTime = null; $isArrival = false; $obj->station = Entity\Location\Station::createFromXml($xml->Station); // deprecated, use location instead foreach ($xml->children() as $location) { $location = Entity\LocationFactory::createFromXml($location); if ($location) { $obj->location = $location; break; } } if ($xml->Arr) { $isArrival = true; $dateTime = self::calculateDateTime((string) $xml->Arr->Time, $date); $obj->arrival = $dateTime->format(\DateTime::ISO8601); $obj->arrivalTimestamp = $dateTime->getTimestamp(); $obj->platform = trim((string) $xml->Arr->Platform->Text); } if ($xml->Dep) { $dateTime = self::calculateDateTime((string) $xml->Dep->Time, $date); $obj->departure = $dateTime->format(\DateTime::ISO8601); $obj->departureTimestamp = $dateTime->getTimestamp(); $obj->platform = trim((string) $xml->Dep->Platform->Text); } $obj->prognosis = Prognosis::createFromXml($xml->StopPrognosis, $dateTime, $isArrival); if ($obj->prognosis) { if ($obj->prognosis->arrival && $obj->arrival) { $obj->delay = (strtotime($obj->prognosis->arrival) - strtotime($obj->arrival)) / 60; } if ($obj->prognosis->departure && $obj->departure) { $obj->delay = (strtotime($obj->prognosis->departure) - strtotime($obj->departure)) / 60; } } return $obj; }
public function testCreateFromJsonWithUmlaute() { $jsonString = <<<'EOF' { "extId": "8508489", "name": "Nümmerland", "prodclass": "16", "puic": "85", "urlname": "N%FCmmerland", "x": "8382324", "y": "47003057" } EOF; $station = new Station(); $station->name = 'Nümmerland'; $station->id = 8508489; $coordinate = new Coordinate(); $coordinate->type = 'WGS84'; $coordinate->x = 47.003057; $coordinate->y = 8.382324000000001; $station->coordinate = $coordinate; $this->assertEquals($station, LocationFactory::createFromJson(json_decode($jsonString))); }
/** * @return array */ public function findNearbyLocations(NearbyQuery $query) { $url = self::URL_QUERY . '?' . http_build_query($query->toArray()); // send request $response = $this->browser->get($url); // fix broken JSON $content = $response->getContent(); $content = preg_replace('/(\\w+) ?:/i', '"\\1":', $content); $content = str_replace("\\'", "'", $content); // parse result $result = json_decode($content); $locations = array(); foreach ($result->stops as $stop) { $location = Entity\LocationFactory::createFromJson($stop); if ($location) { $location->distance = $location->coordinate->getDistanceTo($query->lat, $query->lon); $locations[] = $location; } } return $locations; }
/** * @return array */ public function findNearbyLocations(NearbyQuery $query) { $url = self::URL_QUERY . '?' . http_build_query($query->toArray()); // send request $response = $this->browser->get($url); // check for server error if ($response->isServerError()) { throw new \Exception('Server error from fahrplan.sbb.ch: ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase()); } // fix broken JSON $content = $response->getContent(); $content = preg_replace('/(\\w+) ?:/i', '"\\1":', $content); $content = str_replace("\\'", "'", $content); // parse result $result = json_decode($content); // check for JSON error if ($result === null) { throw new \Exception('Invalid JSON from fahrplan.sbb.ch: ' . $content); } $locations = []; foreach ($result->stops as $stop) { $location = Entity\LocationFactory::createFromJson($stop); if ($location) { $location->distance = $location->coordinate->getDistanceTo($query->lat, $query->lon); $locations[] = $location; } } return $locations; }