Geocoder\Provider\OpenCage::executeQuery PHP Méthode

executeQuery() private méthode

private executeQuery ( $query ) : Geocoder\Model\AddressCollection
$query
Résultat Geocoder\Model\AddressCollection
    private function executeQuery($query)
    {
        if (null !== $this->getLocale()) {
            $query = sprintf('%s&language=%s', $query, $this->getLocale());
        }
        $request = $this->getMessageFactory()->createRequest('GET', $query);
        $content = (string) $this->getHttpClient()->sendRequest($request)->getBody();
        if (empty($content)) {
            throw new NoResult(sprintf('Could not execute query "%s".', $query));
        }
        $json = json_decode($content, true);
        // https://geocoder.opencagedata.com/api#codes
        if (isset($json['status'])) {
            switch ($json['status']['code']) {
                case 400:
                    throw new InvalidArgument('Invalid request (a required parameter is missing).');
                case 402:
                    throw new QuotaExceeded('Valid request but quota exceeded.');
                case 403:
                    throw new InvalidCredentials('Invalid or missing api key.');
            }
        }
        if (!isset($json['total_results']) || $json['total_results'] == 0) {
            throw new NoResult(sprintf('Could not find results for query "%s".', $query));
        }
        $locations = $json['results'];
        if (empty($locations)) {
            throw new NoResult(sprintf('Could not find results for query "%s".', $query));
        }
        $results = [];
        foreach ($locations as $location) {
            $bounds = [];
            if (isset($location['bounds'])) {
                $bounds = ['south' => $location['bounds']['southwest']['lat'], 'west' => $location['bounds']['southwest']['lng'], 'north' => $location['bounds']['northeast']['lat'], 'east' => $location['bounds']['northeast']['lng']];
            }
            $comp = $location['components'];
            $adminLevels = [];
            foreach (['state', 'county'] as $i => $component) {
                if (isset($comp[$component])) {
                    $adminLevels[] = ['name' => $comp[$component], 'level' => $i + 1];
                }
            }
            $results[] = array_merge($this->getDefaults(), array('latitude' => $location['geometry']['lat'], 'longitude' => $location['geometry']['lng'], 'bounds' => $bounds ?: [], 'streetNumber' => isset($comp['house_number']) ? $comp['house_number'] : null, 'streetName' => $this->guessStreetName($comp), 'subLocality' => $this->guessSubLocality($comp), 'locality' => $this->guessLocality($comp), 'postalCode' => isset($comp['postcode']) ? $comp['postcode'] : null, 'adminLevels' => $adminLevels, 'country' => isset($comp['country']) ? $comp['country'] : null, 'countryCode' => isset($comp['country_code']) ? strtoupper($comp['country_code']) : null, 'timezone' => isset($location['annotations']['timezone']['name']) ? $location['annotations']['timezone']['name'] : null));
        }
        return $this->returnResults($results);
    }