This commit is contained in:
Josh Sherman 2012-12-13 22:31:44 -05:00
parent 38ef651ec2
commit 627c9df2e6

View file

@ -4,6 +4,8 @@ class GooglePlaces
{ {
private $key = ''; private $key = '';
private $base_url = 'https://maps.googleapis.com/maps/api/place'; private $base_url = 'https://maps.googleapis.com/maps/api/place';
private $method = null;
private $response = null;
public $keyword = null; public $keyword = null;
public $language = 'en'; public $language = 'en';
@ -28,9 +30,37 @@ class GooglePlaces
$this->$variable = $value; $this->$variable = $value;
} }
public function next($token)
{
if ($token)
{
$this->pagetoken = $token;
}
elseif (isset($this->response['next_page_token']))
{
$this->pagetoken = $this->response['next_page_token'];
}
else
{
throw new Exception('Previous call did not return a next_page_token and you didn\'t supply one. What did you expect?');
}
if ($method === null)
{
throw new Exception('You cannot call next() before making a call.');
}
$this->$method();
}
public function __call($method, $arguments) public function __call($method, $arguments)
{ {
$method = strtolower($method); if (count($arguments) > 0)
{
$this->pagetoken = $arguments[0];
}
$method = $this->method = strtolower($method);
$url = implode('/', array($this->base_url, $method, $this->output)); $url = implode('/', array($this->base_url, $method, $this->output));
$parameters = array(); $parameters = array();
@ -39,7 +69,7 @@ class GooglePlaces
foreach (get_object_vars($this) as $variable => $value) foreach (get_object_vars($this) as $variable => $value)
{ {
// Except these variables // Except these variables
if (!in_array($variable, array('base_url', 'output'))) if (!in_array($variable, array('base_url', 'method', 'output', 'response')))
{ {
// Assuming it's not null // Assuming it's not null
if ($value !== null) if ($value !== null)
@ -62,6 +92,16 @@ class GooglePlaces
} }
break; break;
// Checks that the output is value
case 'output':
$value = strtolower($value);
if (!in_array($value, array('json', 'xml')))
{
throw new Exception('Invalid output, please specify either "json" or "xml".');
}
break;
// Checks that it's a value rank by value // Checks that it's a value rank by value
case 'rankby': case 'rankby':
$value = strtolower($value); $value = strtolower($value);
@ -91,12 +131,13 @@ class GooglePlaces
case 'nearbysearch': case 'nearbysearch':
if (!isset($parameters['location'])) if (!isset($parameters['location']))
{ {
throw new Exception('You must specify a location before calling nearbysearch.'); throw new Exception('You must specify a location before calling nearbysearch().');
} }
elseif (isset($parameters['rankby'])) elseif (isset($parameters['rankby']))
{ {
if ($parameters['rankby'] == 'distance') switch ($parameters['rankby'])
{ {
case 'distance':
if (!isset($parameters['keyword']) && !isset($parameters['name']) && !isset($parameters['types'])) if (!isset($parameters['keyword']) && !isset($parameters['name']) && !isset($parameters['types']))
{ {
throw new Exception('You much specify at least one of the following: keyword, name, types.'); throw new Exception('You much specify at least one of the following: keyword, name, types.');
@ -106,12 +147,23 @@ class GooglePlaces
{ {
unset($parameters['radius']); unset($parameters['radius']);
} }
break;
case 'prominence':
if (!isset($parameters['radius']))
{
throw new Exception('You must specify a radius.');
}
break;
} }
} }
break; break;
} }
// Couldn't seem to get http_build_query() to work right so...
$querystring = ''; $querystring = '';
foreach ($parameters as $variable => $value) foreach ($parameters as $variable => $value)
@ -124,8 +176,6 @@ class GooglePlaces
$querystring .= $variable . '=' . $value; $querystring .= $variable . '=' . $value;
} }
echo $url . '?' . $querystring;
$curl = curl_init(); $curl = curl_init();
$options = array( $options = array(
@ -139,10 +189,35 @@ class GooglePlaces
$response = curl_exec($curl); $response = curl_exec($curl);
var_dump($response, curl_error($curl)); if ($error = curl_error($curl))
{
throw new Exception('CURL Error: ' . $error);
}
if ($this->output == 'json')
{
$response = json_decode($response, true);
if ($response === null)
{
throw new Exception('The returned JSON was malformed or nonexistent.');
}
}
else
{
throw new Exception('XML is terrible, don\'t use it, ever.');
}
curl_close($curl); curl_close($curl);
exit; $this->response = $response;
if ($this->pagetoken !== null)
{
$this->pagetoken = null;
}
return $this->response;
} }
// @todo Method to sanity check passed types // @todo Method to sanity check passed types