diff --git a/src/GoogleMaps/Geocoder.php b/src/GoogleMaps/Geocoder.php index 7da2655..10472c8 100644 --- a/src/GoogleMaps/Geocoder.php +++ b/src/GoogleMaps/Geocoder.php @@ -1,8 +1,9 @@ + * (c) 2012 C�dric DERUE * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -19,115 +20,141 @@ use Zend\Uri\Uri; -class Geocoder +class Geocoder { - const GOOGLE_MAPS_APIS_URL = 'maps.googleapis.com'; - const GOOGLE_GEOCODING_API_PATH = '/maps/api/geocode/json'; - const XML_FORMAT = 'xml'; - const JSON_FORMAT = 'json'; - - /** - * Response format (XML or JSON) - * - * @var string - */ - protected $format; - - /** - * Http client - * - * @var HttpClient - */ - protected $httpClient; - - /** - * - * @param string $format - * @throws Exception\InvalidArgumentException - */ - public function __construct($format = 'json') - { - $validFormats = array( - self::JSON_FORMAT, - self::XML_FORMAT, - ); - if (!in_array($format, $validFormats)) { - throw new Exception\InvalidArgumentException('format'); - } - $this->format = $format; - } - - /** - * - * @return string - */ - public function getFormat() - { - return $this->format; - } - - /** - * Get the HttpClient instance - * - * @return HttpClient - */ - public function getHttpClient() - { - if (empty($this->httpClient)) { + const GOOGLE_MAPS_APIS_URL = 'maps.googleapis.com'; + const GOOGLE_GEOCODING_API_PATH = '/maps/api/geocode/json'; + const XML_FORMAT = 'xml'; + const JSON_FORMAT = 'json'; + + /** + * Response format (XML or JSON) + * + * @var string + */ + protected $format; + + /** + * Http client + * + * @var HttpClient + */ + protected $httpClient; + + + + protected $language; + + /** + * + * @param string $format + * @throws Exception\InvalidArgumentException + */ + public function __construct($format = 'json') + { + $validFormats = array( + self::JSON_FORMAT, + self::XML_FORMAT, + ); + if (!in_array($format, $validFormats)) { + throw new Exception\InvalidArgumentException('format'); + } + $this->format = $format; + } + + public function getLanguage() + { + return $this->language; + } + public function setLanguage($language) + { + $this->language = $language; + return $this; + } + + /** + * + * @return string + */ + public function getFormat() + { + return $this->format; + } + + /** + * Get the HttpClient instance + * + * @return HttpClient + */ + public function getHttpClient() + { + if (empty($this->httpClient)) { $this->httpClient = new HttpClient(); } return $this->httpClient; - } - - - - /** - * Execute geocoding - * - * @param Request $request - * @return Response - */ - public function geocode(Request $request) - { - if (null === $request) { - throw new Exception\InvalidArgumentException('request'); - } - $uri = new Uri(); - $uri->setHost(self::GOOGLE_MAPS_APIS_URL); - $uri->setPath(self::GOOGLE_GEOCODING_API_PATH); - - $urlParameters = $request->getUrlParameters(); - if (null === $urlParameters) { - throw new Exception\RuntimeException('Invalid URL parameters'); - } - - $uri->setQuery($urlParameters); - $client = $this->getHttpClient(); - $client->resetParameters(); - $client->setUri($uri->toString()); - $stream = $client->send(); - - $body = Json::decode($stream->getBody(), Json::TYPE_ARRAY); - $hydrator = new ArraySerializable(); - - $response = new Response(); - $response->setRawBody($body); - if (!isset($body['status'])) { - throw new Exception\RuntimeException('Invalid status'); - } - $response->setStatus($body['status']); - if (!isset($body['results'])) { - throw new Exception\RuntimeException('Invalid results'); - } - - $resultSet = new ResultSet(); - foreach ($body['results'] as $data) { - $result = new Result(); - $hydrator->hydrate($data, $result); - $resultSet->addElement($result); - } - $response->setResults($resultSet); - - return $response; - } -} \ No newline at end of file + } + + + public function setHttpClient(HttpClient $httpClient) + { + $this->httpClient = $httpClient; + return $this; + } + + + /** + * Execute geocoding + * + * @param Request $request + * @return Response + */ + public function geocode(Request $request) + { + if (null === $request) { + throw new Exception\InvalidArgumentException('request'); + } + $uri = new Uri(); + $uri->setScheme('https'); + $uri->setHost(self::GOOGLE_MAPS_APIS_URL); + $uri->setPath(self::GOOGLE_GEOCODING_API_PATH); + + $urlParameters = $request->getUrlParameters(); + if (null === $urlParameters) { + throw new Exception\RuntimeException('Invalid URL parameters'); + } + + $uri->setQuery($urlParameters); + $client = $this->getHttpClient(); + $client->resetParameters(); + $client->setUri($uri->toString()); + $client->setOptions(['sslverifypeer' => false]); + if ($this->getLanguage()) { + $client->setHeaders(array('Accept-Language' => $this->getLanguage())); + } + + $stream = $client->send(); + + $body = Json::decode($stream->getBody(), Json::TYPE_ARRAY); + $hydrator = new ArraySerializable(); + + $response = new Response(); + $response->setRawBody($body); + if (!isset($body['status'])) { + throw new Exception\RuntimeException('Invalid status'); + } + $response->setStatus($body['status']); + if (!isset($body['results'])) { + throw new Exception\RuntimeException('Invalid results'); + } + + $resultSet = new ResultSet(); + foreach ($body['results'] as $data) { + $result = new Result(); + $hydrator->hydrate($data, $result); + $resultSet->addElement($result); + } + $response->setResults($resultSet); + + return $response; + } +} diff --git a/src/GoogleMaps/Parameters/ComponentParameter.php b/src/GoogleMaps/Parameters/ComponentParameter.php index 87a50da..b83a5e5 100644 --- a/src/GoogleMaps/Parameters/ComponentParameter.php +++ b/src/GoogleMaps/Parameters/ComponentParameter.php @@ -2,7 +2,7 @@ /** * This file is part of Geoxygen * - * (c) 2012 Cédric DERUE + * (c) 2012 Cédric DERUE * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -79,6 +79,6 @@ public function getValue() */ public function toString() { - return $this->key . '|' . $this->value(); + return $this->key . ':' . $this->value; } } \ No newline at end of file diff --git a/src/GoogleMaps/Parameters/ComponentSetParameter.php b/src/GoogleMaps/Parameters/ComponentSetParameter.php index 931acf9..bbd3866 100644 --- a/src/GoogleMaps/Parameters/ComponentSetParameter.php +++ b/src/GoogleMaps/Parameters/ComponentSetParameter.php @@ -2,7 +2,7 @@ /** * This file is part of Geoxygen * - * (c) 2012 Cédric DERUE + * (c) 2012 C�dric DERUE * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -27,8 +27,10 @@ public function __construct() */ public function toString() { - $components = null; + $components = null; foreach ($this->collection as $element) { + if (isset($components)) + $components .= '|'; $components .= $element->toString(); } return $components; diff --git a/src/GoogleMaps/Request.php b/src/GoogleMaps/Request.php index 450a739..a396869 100644 --- a/src/GoogleMaps/Request.php +++ b/src/GoogleMaps/Request.php @@ -1,8 +1,9 @@ + * (c) 2012 C�dric DERUE * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -17,218 +18,241 @@ class Request { - const SENSOR = 'true'; - const NO_SENSOR = 'false'; - - /** - * Address to perform geocoding (required) - * - * @var string - */ - protected $address; - - /** - * Latitude / longitude to perform reverse geocoding (required) - * - * @var LatLng - */ - protected $latLng; - - /** - * Components filter to perform geocoding (optional if an address is provided else required) - * - * @var array - */ - protected $components; - - /** - * Indicates if the request is provided by a device with a location sensor (required) - * - * @var boolean - */ - protected $sensor; - - /** - * Bounding box to limit results within a given viewport (optional) - * - * @var LatLngBounds - */ - protected $bounds; - - /** - * Region code (ccTLD or ISO-3166-1 value) to limit results within a particular region (optional) - * - * @var string - */ - protected $region; - - /** - * Specify the language in which to return results - * - * @var string - */ - protected $language; - - /** - * Contructor - * - * @param boolean $sensor - */ - public function __construct($sensor = self::NO_SENSOR) - { - $this->sensor = $sensor; - } - - /** - * @return the $address - */ - public function getAddress() - { - return $this->address; - } - - /** - * @param string $address - */ - public function setAddress($address) - { - $this->address = $address; - } - - /** - * @return the $latLng - */ - public function getLatLng() - { - return $this->latLng; - } - - /** - * @param LatLngParameter $latLng - */ - public function setLatLng(LatLngParameter $latLng) - { - $this->latLng = $latLng; - } - - /** - * @return the $bounds - */ - public function getBounds() - { - return $this->bounds; - } - - /** - * @param LatLngBoundsParameter $bounds - */ - public function setBounds(LatLngBoundsParameter $bounds) - { - $this->bounds = $bounds; - } - - /** - * @return the $language - */ - public function getLanguage() - { - return $this->language; - } - - /** - * @param string $language - */ - public function setLanguage($language) - { - $this->language = $language; - } - - /** - * @return the $region - */ - public function getRegion() - { - return $this->region; - } - - /** - * @param string $region - */ - public function setRegion($region) - { - $this->region = $region; - } - - /** - * @return the $components - */ - public function getComponents() - { - return $this->components; - } - - /** - * @param \GoogleMaps\unknown_type $componentsFilter - */ - public function setComponents(ComponentSetParameter $components) - { - $this->components = $components; - } - - /** - * @return the $sensor - */ - public function getSensor() - { - return $this->sensor; - } - - /** - * @param boolean $sensor - */ - public function setSensor($sensor) - { - $this->sensor = $sensor; - } - - /** - * Tranform request to URL parameters - * - * @return NULL|string - */ - public function getUrlParameters() - { - $requiredParameters = array('address', 'latlng', 'components', 'sensor'); - $optionalParameters = array('bounds', 'language', 'region', 'components'); - - $url = ''; - foreach ($requiredParameters as $parameter) { - $method = 'get' . $parameter; - $requiredParam = $this->$method(); - if (isset($requiredParam)) { - if ($url !== '') { - $url .= '&'; - } - if (is_object($requiredParam)) { - $requiredParam = $requiredParam->toString(); - } - $url .= $parameter . '=' . urlencode($requiredParam); - } - } - if ($url === '') { - return null; - } - - foreach ($optionalParameters as $option) { - $method = 'get' . $option; - $optionParam = $this->$method(); - if (!empty($optionParam)) { - if (is_object($optionParam)) { - $optionParam = $optionParam->toString(); - } - $url .= '&' . $option . '=' . urlencode($optionParam); - } - } - return $url; - } -} \ No newline at end of file + const SENSOR = 'true'; + const NO_SENSOR = 'false'; + + /** + * Address to perform geocoding (required) + * + * @var string + */ + protected $address; + + /** + * Latitude / longitude to perform reverse geocoding (required) + * + * @var LatLng + */ + protected $latLng; + + /** + * Components filter to perform geocoding (optional if an address is provided else required) + * + * @var array + */ + protected $components; + + /** + * Indicates if the request is provided by a device with a location sensor (required) + * + * @var boolean + */ + protected $sensor; + + /** + * Bounding box to limit results within a given viewport (optional) + * + * @var LatLngBounds + */ + protected $bounds; + + /** + * Region code (ccTLD or ISO-3166-1 value) to limit results within a particular region (optional) + * + * @var string + */ + protected $region; + + /** + * Specify the language in which to return results + * + * @var string + */ + protected $language; + + /** + * Google API key + * + * @var string + */ + protected $key; + + /** + * Contructor + * + * @param boolean $sensor + */ + public function __construct($sensor = self::NO_SENSOR) + { + $this->sensor = $sensor; + } + + /** + * @return the $address + */ + public function getAddress() + { + return $this->address; + } + + /** + * @param string $address + */ + public function setAddress($address) + { + $this->address = $address; + } + + /** + * @return the $latLng + */ + public function getLatLng() + { + return $this->latLng; + } + + /** + * @param LatLngParameter $latLng + */ + public function setLatLng(LatLngParameter $latLng) + { + $this->latLng = $latLng; + } + + /** + * @return the $bounds + */ + public function getBounds() + { + return $this->bounds; + } + + /** + * @param LatLngBoundsParameter $bounds + */ + public function setBounds(LatLngBoundsParameter $bounds) + { + $this->bounds = $bounds; + } + + /** + * @return the $language + */ + public function getLanguage() + { + return $this->language; + } + + /** + * @param string $language + */ + public function setLanguage($language) + { + $this->language = $language; + } + + /** + * @return the $region + */ + public function getRegion() + { + return $this->region; + } + + /** + * @param string $region + */ + public function setRegion($region) + { + $this->region = $region; + } + + /** + * @return the $components + */ + public function getComponents() + { + return $this->components; + } + + /** + * @param \GoogleMaps\unknown_type $componentsFilter + */ + public function setComponents(ComponentSetParameter $components) + { + $this->components = $components; + } + + /** + * @return the $sensor + */ + public function getSensor() + { + return $this->sensor; + } + + /** + * @param boolean $sensor + */ + public function setSensor($sensor) + { + $this->sensor = $sensor; + } + + /** + * @return string + */ + public function getKey() + { + return $this->key; + } + + /** + * @param string $key Google API key + */ + public function setKey($key) + { + $this->key = $key; + } + + /** + * Tranform request to URL parameters + * + * @return NULL|string + */ + public function getUrlParameters() + { + $requiredParameters = array('address', 'latlng', 'components', 'sensor'); + $optionalParameters = array('bounds', 'language', 'region', 'key'); + + $url = ''; + foreach ($requiredParameters as $parameter) { + $method = 'get' . $parameter; + $requiredParam = $this->$method(); + if (isset($requiredParam)) { + if ($url !== '') { + $url .= '&'; + } + if (is_object($requiredParam)) { + $requiredParam = $requiredParam->toString(); + } + $url .= $parameter . '=' . $requiredParam; + } + } + if ($url === '') { + return null; + } + + foreach ($optionalParameters as $option) { + $method = 'get' . $option; + $optionParam = $this->$method(); + if (!empty($optionParam)) { + if (is_object($optionParam)) { + $optionParam = $optionParam->toString(); + } + $url .= '&' . $option . '=' . $optionParam; + } + } + return $url; + } +}