This commit is contained in:
bartek 2014-06-05 18:37:45 +00:00
commit 1247cc18e2

View file

@ -2,6 +2,9 @@
namespace joshtronic;
/**
* GooglePlaces PHP wrapper
*/
class GooglePlaces
{
private $key = '';
@ -26,11 +29,20 @@ class GooglePlaces
public $getmax = true;
private $grid = null;
/**
* Constructor.
*
* @param $key
*/
public function __construct($key)
{
$this->key = $key;
}
/**
* @param $variable
* @param $value
*/
function __set($variable, $value)
{
// Compensates for mixed variable naming
@ -38,6 +50,11 @@ class GooglePlaces
$this->$variable = $value;
}
/**
* @param $method
* @param $arguments
* @return mixed|null
*/
public function __call($method, $arguments)
{
$method = $this->method = strtolower($method);
@ -53,31 +70,31 @@ class GooglePlaces
return $this->queryGoogle($url, $parameters);
}
/**
* Loops through all of our variables to make a parameter list
*
* @param $parameters
* @return mixed
* @throws \Exception
*/
private function parameterBuilder($parameters)
{
foreach (get_object_vars($this) as $variable => $value)
{
foreach (get_object_vars($this) as $variable => $value) {
// Except these variables
if (!in_array($variable, array('base_url', 'method', 'output', 'pagetoken', 'response', 'subradius', 'getmax','grid')))
{
if (!in_array($variable, array('base_url', 'method', 'output',
'pagetoken', 'response', 'subradius', 'getmax', 'grid'))) {
// Assuming it's not null
if ($value !== null)
{
if ($value !== null) {
// Converts boolean to string
if (is_bool($value))
{
if (is_bool($value)) {
$value = $value ? 'true' : 'false';
}
switch ($variable)
{
switch ($variable) {
// Allows LatLng to be passed as an array
case 'location':
if (is_array($value))
{
if (is_array($value)) {
// Just in case it's an associative array
$value = array_values($value);
$value = $value[0] . ',' . $value[1];
@ -88,8 +105,7 @@ class GooglePlaces
case 'output':
$value = strtolower($value);
if (!in_array($value, array('json', 'xml')))
{
if (!in_array($value, array('json', 'xml'))) {
throw new \Exception('Invalid output, please specify either "json" or "xml".');
}
break;
@ -98,16 +114,16 @@ class GooglePlaces
case 'rankby':
$value = strtolower($value);
if (!in_array($value, array('prominence', 'distance')))
{
throw new \Exception('Invalid rank by value, please specify either "prominence" or "distance".');
if (!in_array($value, array('prominence', 'distance'))) {
throw new \Exception(
'Invalid rank by value, please specify either "prominence" or "distance".'
);
}
break;
// Allows types to be passed as an array
case 'types':
if (is_array($value))
{
if (is_array($value)) {
$value = implode('|', $value);
}
break;
@ -120,40 +136,37 @@ class GooglePlaces
return $parameters;
}
/**
* takes the parameters and method to throw exceptions or modify parameters as needed
* Takes the parameters and method to throw exceptions or modify parameters as needed
* @todo Method to sanity check passed types
*
* @param $parameters
* @param $method
* @return mixed
* @throws \Exception
*/
private function methodChecker($parameters, $method)
{
if (!isset($parameters['pagetoken']))
{
switch ($method)
{
if (!isset($parameters['pagetoken'])) {
switch ($method) {
case 'nearbysearch':
if (!isset($parameters['location']))
{
if (!isset($parameters['location'])) {
throw new \Exception('You must specify a location before calling nearbysearch().');
}
elseif (isset($parameters['rankby']))
{
switch ($parameters['rankby'])
{
} elseif (isset($parameters['rankby'])) {
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.');
}
if (isset($parameters['radius']))
{
if (isset($parameters['radius'])) {
unset($parameters['radius']);
}
break;
case 'prominence':
if (!isset($parameters['radius']))
{
if (!isset($parameters['radius'])) {
throw new \Exception('You must specify a radius.');
}
break;
@ -163,34 +176,27 @@ class GooglePlaces
break;
case 'radarsearch':
if (!isset($parameters['location']))
{
if (!isset($parameters['location'])) {
throw new \Exception('You must specify a location before calling nearbysearch().');
}
elseif (!isset($parameters['radius']))
{
} elseif (!isset($parameters['radius'])) {
throw new \Exception('You must specify a radius.');
}
elseif (empty($parameters['keyword']) && empty($parameters['name']) && empty($parameters['types']))
{
throw new \Exception('A Radar Search request must include at least one of keyword, name, or types.');
} elseif (empty($parameters['keyword']) && empty($parameters['name']) && empty($parameters['types'])) {
throw new \Exception('A Radar Search request must include at least one of keyword, name, or types.'
);
}
if (isset($parameters['rankby']))
{
if (isset($parameters['rankby'])) {
unset($parameters['rankby']);
}
break;
case 'details':
if (!isset($parameters['reference']))
{
if (!isset($parameters['reference'])) {
throw new \Exception('You must specify a reference before calling details().');
}
if (isset($parameters['rankby']))
{
if (isset($parameters['rankby'])) {
unset($parameters['rankby']);
}
@ -202,11 +208,15 @@ class GooglePlaces
/**
* Submits request via curl, sets the response, then returns the response
*
* @param $url
* @param $parameters
* @return mixed
* @throws \Exception
*/
private function queryGoogle($url, $parameters)
{
if ($this->pagetoken !== null)
{
if ($this->pagetoken !== null) {
$parameters['pagetoken'] = $this->pagetoken;
sleep(3);
}
@ -214,10 +224,8 @@ class GooglePlaces
// Couldn't seem to get http_build_query() to work right so...
$querystring = '';
foreach ($parameters as $variable => $value)
{
if ($querystring != '')
{
foreach ($parameters as $variable => $value) {
if ($querystring != '') {
$querystring .= '&';
}
@ -237,22 +245,17 @@ class GooglePlaces
$response = curl_exec($curl);
if ($error = curl_error($curl))
{
if ($error = curl_error($curl)) {
throw new \Exception('CURL Error: ' . $error);
}
if ($this->output == 'json')
{
if ($this->output == 'json') {
$response = json_decode($response, true);
if ($response === null)
{
if ($response === null) {
throw new \Exception('The returned JSON was malformed or nonexistent.');
}
}
else
{
} else {
throw new \Exception('XML is terrible, don\'t use it, ever.');
}
@ -265,6 +268,10 @@ class GooglePlaces
/**
* Returns the longitude equal to a given distance (meters) at a given latitude
*
* @param $meters
* @param $latitude
* @return float
*/
public function meters2lng($meters, $latitude)
{
@ -273,6 +280,9 @@ class GooglePlaces
/**
* Returns the latitude equal to a given distance (meters)
*
* @param $meters
* @return float
*/
public function meters2lat($meters)
{
@ -281,11 +291,15 @@ class GooglePlaces
/**
* Returns the aggregated responses for a subdivided search
*
* @param $url
* @param $parameters
* @return null
* @throws \Exception
*/
private function subdivide($url, $parameters)
{
if (($this->radius % $this->subradius) || ($this->subradius < 200) || (($this->radius / $this->subradius) % 2))
{
if (($this->radius % $this->subradius) || ($this->subradius < 200) || (($this->radius / $this->subradius) % 2)) {
throw new \Exception('Subradius should divide evenly into radius. Also, subradius should be 200 meters or so. (ex: 2000/200 = 10x10 grid. NOT 2000/33 = 60.6x60.6 grid. NOT 2000/16 = 125x125 grid)');
}
@ -297,13 +311,11 @@ class GooglePlaces
$this->grid['results'] = array();
for ($i = $count / 2 * -1; $i <= $count / 2; $i++)
{
for ($i = $count / 2 * -1; $i <= $count / 2; $i++) {
$lat = $centerlat + $i * $lati;
$lngi = $this->meters2lng($this->subradius * 2, $lat);
for ($j = $count / 2 * -1; $j <= $count / 2; $j++)
{
for ($j = $count / 2 * -1; $j <= $count / 2; $j++) {
$lng = $centerlng + $j * $lngi;
$loc = $lat . ',' . $lng;
@ -315,8 +327,7 @@ class GooglePlaces
$this->grid[$i][$j] = $this->response;
$this->grid['results'] = array_merge($this->grid['results'], $this->response['results']);
while ($this->response['next_page_token'])
{
while ($this->response['next_page_token']) {
$this->pagetoken = $this->response['next_page_token'];
$this->queryGoogle($url, $parameters);
@ -331,5 +342,3 @@ class GooglePlaces
return $this->grid;
}
}
?>