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; }