Geocoder\Provider\ArcGISOnline::geocode PHP Method

geocode() public method

{@inheritDoc}
public geocode ( $address )
    public function geocode($address)
    {
        if (filter_var($address, FILTER_VALIDATE_IP)) {
            throw new UnsupportedOperation('The ArcGISOnline provider does not support IP addresses, only street addresses.');
        }
        // Save a request if no valid address entered
        if (empty($address)) {
            throw new NoResult('Invalid address.');
        }
        $query = sprintf(self::ENDPOINT_URL, $this->protocol, urlencode($address));
        $json = $this->executeQuery($query);
        // no result
        if (empty($json->locations)) {
            throw new NoResult(sprintf('No results found for query "%s".', $query));
        }
        $results = [];
        foreach ($json->locations as $location) {
            $data = $location->feature->attributes;
            $coordinates = (array) $location->feature->geometry;
            $streetName = !empty($data->Match_addr) ? $data->Match_addr : null;
            $streetNumber = !empty($data->AddNum) ? $data->AddNum : null;
            $city = !empty($data->City) ? $data->City : null;
            $zipcode = !empty($data->Postal) ? $data->Postal : null;
            $countryCode = !empty($data->Country) ? $data->Country : null;
            $adminLevels = [];
            foreach (['Region', 'Subregion'] as $i => $property) {
                if (!empty($data->{$property})) {
                    $adminLevels[] = ['name' => $data->{$property}, 'level' => $i + 1];
                }
            }
            $results[] = array_merge($this->getDefaults(), ['latitude' => $coordinates['y'], 'longitude' => $coordinates['x'], 'streetNumber' => $streetNumber, 'streetName' => $streetName, 'locality' => $city, 'postalCode' => $zipcode, 'adminLevels' => $adminLevels, 'countryCode' => $countryCode]);
        }
        return $this->returnResults($results);
    }

Usage Example

Example #1
0
 /**
  * @expectedException \Geocoder\Exception\UnsupportedOperation
  * @expectedExceptionMessage The ArcGISOnline provider does not support IP addresses, only street addresses.
  */
 public function testGeocodeWithRealIPv6()
 {
     $provider = new ArcGISOnline($this->getMockAdapter($this->never()));
     $provider->geocode('::ffff:88.188.221.14');
 }