forked from geocoder-php/Geocoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeocoderUsProvider.php
More file actions
81 lines (68 loc) · 2.07 KB
/
Copy pathGeocoderUsProvider.php
File metadata and controls
81 lines (68 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/**
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Geocoder\Provider;
use Geocoder\Exception\UnsupportedException;
use Geocoder\Exception\NoResultException;
/**
* @author Antoine Corcy <contact@sbin.dk>
*/
class GeocoderUsProvider extends AbstractProvider implements ProviderInterface
{
/**
* @var string
*/
const ENDPOINT_URL = 'http://geocoder.us/service/rest/?address=%s';
/**
* {@inheritDoc}
*/
public function getGeocodedData($address)
{
// This API doesn't handle IPs
if (filter_var($address, FILTER_VALIDATE_IP)) {
throw new UnsupportedException('The GeocoderUsProvider does not support IP addresses.');
}
$query = sprintf(self::ENDPOINT_URL, urlencode($address));
return $this->executeQuery($query);
}
/**
* {@inheritDoc}
*/
public function getReversedData(array $coordinates)
{
throw new UnsupportedException('The GeocoderUsProvider is not able to do reverse geocoding.');
}
/**
* {@inheritDoc}
*/
public function getName()
{
return 'geocoder_us';
}
/**
* @param string $query
*
* @return array
*/
protected function executeQuery($query)
{
$content = $this->getAdapter()->getContent($query);
$doc = new \DOMDocument();
if (!@$doc->loadXML($content)) {
throw new NoResultException(sprintf('Could not execute query %s', $query));
}
$xpath = new \SimpleXMLElement($content);
$xpath->registerXPathNamespace('geo', 'http://www.w3.org/2003/01/geo/wgs84_pos#');
$lat = $xpath->xpath('//geo:lat');
$long = $xpath->xpath('//geo:long');
return array(array_merge($this->getDefaults(), array(
'latitude' => isset($lat[0]) ? (double) $lat[0] : null,
'longitude' => isset($long[0]) ? (double) $long[0] : null,
)));
}
}