Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
245 changes: 136 additions & 109 deletions src/GoogleMaps/Geocoder.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

/**
* This file is part of Geoxygen
*
* (c) 2012 C�dric DERUE <cedric.derue@gmail.com>
* (c) 2012 C�dric DERUE <cedric.derue@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand All @@ -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;
}
}
}


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;
}
}
4 changes: 2 additions & 2 deletions src/GoogleMaps/Parameters/ComponentParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* This file is part of Geoxygen
*
* (c) 2012 C�dric DERUE <cedric.derue@gmail.com>
* (c) 2012 Cédric DERUE <cedric.derue@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand Down Expand Up @@ -79,6 +79,6 @@ public function getValue()
*/
public function toString()
{
return $this->key . '|' . $this->value();
return $this->key . ':' . $this->value;
}
}
6 changes: 4 additions & 2 deletions src/GoogleMaps/Parameters/ComponentSetParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* This file is part of Geoxygen
*
* (c) 2012 C�dric DERUE <cedric.derue@gmail.com>
* (c) 2012 C�dric DERUE <cedric.derue@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand All @@ -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;
Expand Down
Loading