Geocoder\Provider\MaxMind::geocode PHP Méthode

geocode() public méthode

{@inheritDoc}
public geocode ( $address )
    public function geocode($address)
    {
        if (null === $this->apiKey) {
            throw new InvalidCredentials('No API Key provided.');
        }
        if (!filter_var($address, FILTER_VALIDATE_IP)) {
            throw new UnsupportedOperation('The MaxMind provider does not support street addresses, only IP addresses.');
        }
        if (in_array($address, array('127.0.0.1', '::1'))) {
            return $this->returnResults([$this->getLocalhostDefaults()]);
        }
        $query = sprintf($this->useSsl ? self::GEOCODE_ENDPOINT_URL_SSL : self::GEOCODE_ENDPOINT_URL, $this->service, $this->apiKey, $address);
        return $this->executeQuery($query);
    }

Usage Example

Exemple #1
0
 public function testGeocodeOmniServiceWithRealIPv6WithSsl()
 {
     if (!isset($_SERVER['MAXMIND_API_KEY'])) {
         $this->markTestSkipped('You need to configure the MAXMIND_API_KEY value in phpunit.xml');
     }
     $provider = new MaxMind($this->getAdapter($_SERVER['MAXMIND_API_KEY']), $_SERVER['MAXMIND_API_KEY'], MaxMind::OMNI_SERVICE, true);
     $results = $provider->geocode('2002:4293:f4d6:0:0:0:0:0');
     $this->assertInstanceOf('Geocoder\\Model\\AddressCollection', $results);
     $this->assertCount(1, $results);
     /** @var \Geocoder\Model\Address $result */
     $result = $results->first();
     $this->assertInstanceOf('\\Geocoder\\Model\\Address', $result);
     $this->assertEquals(40.2181, $result->getLatitude(), '', 0.1);
     $this->assertEquals(-111.6133, $result->getLongitude(), '', 0.1);
     $this->assertFalse($result->getBounds()->isDefined());
     $this->assertNull($result->getStreetNumber());
     $this->assertNull($result->getStreetName());
     $this->assertEquals(84606, $result->getPostalCode());
     $this->assertEquals('Provo', $result->getLocality());
     $this->assertNull($result->getSubLocality());
     $this->assertCount(1, $result->getAdminLevels());
     $this->assertEquals('Utah', $result->getAdminLevels()->get(1)->getName());
     $this->assertEquals('UT', $result->getAdminLevels()->get(1)->getCode());
     $this->assertEquals('United States', $result->getCountry()->getName());
     $this->assertEquals('US', $result->getCountry()->getCode());
     $this->assertEquals('America/Denver', $result->getTimezone());
 }