FreeGeoIp.php
public function geocodeQuery(GeocodeQuery $query): Collection
    {
        $address = $query->getText();
        if (!filter_var($address, FILTER_VALIDATE_IP)) {
            throw new UnsupportedOperation('The FreeGeoIp provider does not support street addresses.');
        }

        if (in_array($address, ['127.0.0.1', '::1'])) {
            return new AddressCollection([$this->getLocationForLocalhost()]);
        }

        $request = $this->getRequest(sprintf($this->baseUrl, $address));

        if (null !== $query->getLocale()) {
            $request = $request->withHeader('Accept-Language', $query->getLocale());
        }

        $body = $this->getParsedResponse($request);
        $data = json_decode($body, true);

        // Return empty collection if address was not found
        if ('' === $data['region_name']
        && '' === $data['region_code']
        && 0 === $data['latitude']
        && 0 === $data['longitude']
        && '' === $data['city']
        && '' === $data['zip_code']
        && '' === $data['country_name']
        && '' === $data['country_code']
        && '' === $data['time_zone']) {
            return new AddressCollection([]);
        }

        $builder = new AddressBuilder($this->getName());

        if (!empty($data['region_name'])) {
            $builder->addAdminLevel(1, $data['region_name'], $data['region_code'] ?? null);
        }

        if (0 !== $data['latitude'] || 0 !== $data['longitude']) {
            $builder->setCoordinates($data['latitude'] ?? null, $data['longitude'] ?? null);
        }
        $builder->setLocality(empty($data['city']) ? null : $data['city']);
        $builder->setPostalCode(empty($data['zip_code']) ? null : $data['zip_code']);
        $builder->setCountry(empty($data['country_name']) ? null : $data['country_name']);
        $builder->setCountryCode(empty($data['country_code']) ? null : $data['country_code']);
        $builder->setTimezone(empty($data['time_zone']) ? null : $data['time_zone']);

        return new AddressCollection([$builder->build()]);
    }
public function geocodeQuery(GeocodeQuery $query): Collection
    {
        $address = $query->getText();
        if (!filter_var($address, FILTER_VALIDATE_IP)) {
            throw new UnsupportedOperation('The FreeGeoIp provider does not support street addresses.');
        }

        if (in_array($address, ['127.0.0.1', '::1'])) {
            return new AddressCollection([$this->getLocationForLocalhost()]);
        }

        $request = $this->getRequest(sprintf($this->baseUrl, $address));

        if (null !== $query->getLocale()) {
            $request = $request->withHeader('Accept-Language', $query->getLocale());
        }

        $body = $this->getParsedResponse($request);
        $data = json_decode($body, true);

        // Return empty collection if address was not found
        if ('' === $data['region_name']
        && '' === $data['region_code']
        && 0 === $data['latitude']
        && 0 === $data['longitude']
        && '' === $data['city']
        && '' === $data['zip_code']
        && '' === $data['country_name']
        && '' === $data['country_code']
        && '' === $data['time_zone']) {
            return new AddressCollection([]);
        }

        $builder = new AddressBuilder($this->getName());

        if (!empty($data['region_name'])) {
            $builder->addAdminLevel(1, $data['region_name'], $data['region_code'] ?? null);
        }

        if (0 !== $data['latitude'] || 0 !== $data['longitude']) {
            $builder->setCoordinates($data['latitude'] ?? null, $data['longitude'] ?? null);
        }
        $builder->setLocality(empty($data['city']) ? null : $data['city']);
        $builder->setPostalCode(empty($data['zip_code']) ? null : $data['zip_code']);
        $builder->setCountry(empty($data['country_name']) ? null : $data['country_name']);
        $builder->setCountryCode(empty($data['country_code']) ? null : $data['country_code']);
        $builder->setTimezone(empty($data['time_zone']) ? null : $data['time_zone']);

        return new AddressCollection([$builder->build()]);
    }
public function geocodeQuery ( GeocodeQuery $ query ) : Collection { $ address = $ query -> getText ( ) ; if ( ! filter_var ( $ address , FILTER_VALIDATE_IP ) ) { throw new UnsupportedOperation ( 'The FreeGeoIp provider does not support street addresses.' ) ; } if ( in_array ( $ address , [ '127.0.0.1' , '::1' ] ) ) { return new AddressCollection ( [ $ this -> getLocationForLocalhost ( ) ] ) ; } $ request = $ this -> getRequest ( sprintf ( $ this -> baseUrl , $ address ) ) ; if ( null !== $ query -> getLocale ( ) ) { $ request = $ request -> withHeader ( 'Accept-Language' , $ query -> getLocale ( ) ) ; } $ body = $ this -> getParsedResponse ( $ request ) ; $ data = json_decode ( $ body , true ) ; // Return empty collection if address was not found if ( '' === $ data [ 'region_name' ] && '' === $ data [ 'region_code' ] && 0 === $ data [ 'latitude' ] && 0 === $ data [ 'longitude' ] && '' === $ data [ 'city' ] && '' === $ data [ 'zip_code' ] && '' === $ data [ 'country_name' ] && '' === $ data [ 'country_code' ] && '' === $ data [ 'time_zone' ] ) { return new AddressCollection ( [ ] ) ; } $ builder = new AddressBuilder ( $ this -> getName ( ) ) ; if ( ! empty ( $ data [ 'region_name' ] ) ) { $ builder -> addAdminLevel ( 1 , $ data [ 'region_name' ] , $ data [ 'region_code' ] ?? null ) ; } if ( 0 !== $ data [ 'latitude' ] || 0 !== $ data [ 'longitude' ] ) { $ builder -> setCoordinates ( $ data [ 'latitude' ] ?? null , $ data [ 'longitude' ] ?? null ) ; } $ builder -> setLocality ( empty ( $ data [ 'city' ] ) ? null : $ data [ 'city' ] ) ; $ builder -> setPostalCode ( empty ( $ data [ 'zip_code' ] ) ? null : $ data [ 'zip_code' ] ) ; $ builder -> setCountry ( empty ( $ data [ 'country_name' ] ) ? null : $ data [ 'country_name' ] ) ; $ builder -> setCountryCode ( empty ( $ data [ 'country_code' ] ) ? null : $ data [ 'country_code' ] ) ; $ builder -> setTimezone ( empty ( $ data [ 'time_zone' ] ) ? null : $ data [ 'time_zone' ] ) ; return new AddressCollection ( [ $ builder -> build ( ) ] ) ; }
{@inheritdoc}
{