Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Bradley Cornford committed Sep 22, 2016
2 parents 4c4bcb6 + 1be1aeb commit 8e8eb92
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 11 deletions.
12 changes: 12 additions & 0 deletions spec/Cornford/Googlmapper/MapperSpec.php
Expand Up @@ -47,6 +47,7 @@ public function it_throws_an_exception_with_incorrect_options()

public function it_can_return_a_location_when_a_location_is_searched()
{
$this->setKey('AIzaSyAtqWsq5Ai3GYv6dSa6311tZiYKlbYT4mw');
$this->location(self::LOCATION)->shouldReturnAnInstanceOf('Cornford\Googlmapper\Models\Location');
}

Expand All @@ -55,6 +56,17 @@ public function it_can_throws_an_exception_when_a_blank_location_is_searched()
$this->shouldThrow('Cornford\Googlmapper\Exceptions\MapperArgumentException')->during('location', ['']);
}

public function it_can_throws_an_exception_when_an_invalid_location_is_searched()
{
$this->shouldThrow('Cornford\Googlmapper\Exceptions\MapperSearchResultException')->during('location', ['abcdefghijklmnopqrstuvwxyz']);
}

public function it_can_throws_an_exception_when_an_invalid_key_is_used_to_location_search()
{
$this->setKey('123');
$this->shouldThrow('Cornford\Googlmapper\Exceptions\MapperSearchResultException')->during('location', [self::LOCATION]);
}

public function it_can_be_enabled()
{
$this->enableMapping();
Expand Down
2 changes: 2 additions & 0 deletions src/Cornford/Googlmapper/Contracts/MappingInterface.php
Expand Up @@ -3,6 +3,7 @@
use Cornford\Googlmapper\Exceptions\MapperArgumentException;
use Cornford\Googlmapper\Exceptions\MapperException;
use Cornford\Googlmapper\Exceptions\MapperSearchException;
use Cornford\Googlmapper\Exceptions\MapperSearchLimitException;
use Cornford\Googlmapper\Exceptions\MapperSearchResultException;
use Cornford\Googlmapper\Models\Location;

Expand All @@ -25,6 +26,7 @@ public function render($item = -1);
* @throws MapperArgumentException
* @throws MapperSearchException
* @throws MapperSearchResultException
* @throws MapperSearchLimitException
* @throws MapperException
*
* @return Location
Expand Down
@@ -0,0 +1,7 @@
<?php namespace Cornford\Googlmapper\Exceptions;

use Exception;

class MapperSearchLimitException extends Exception {

}
57 changes: 46 additions & 11 deletions src/Cornford/Googlmapper/Mapper.php
Expand Up @@ -4,6 +4,7 @@
use Cornford\Googlmapper\Exceptions\MapperArgumentException;
use Cornford\Googlmapper\Exceptions\MapperException;
use Cornford\Googlmapper\Exceptions\MapperSearchException;
use Cornford\Googlmapper\Exceptions\MapperSearchLimitException;
use Cornford\Googlmapper\Exceptions\MapperSearchResultException;
use Cornford\Googlmapper\Models\Location;
use Cornford\Googlmapper\Models\Map;
Expand All @@ -12,6 +13,13 @@

class Mapper extends MapperBase implements MappingInterface {

const GOOGLE_RESPONSE_OK = 'OK';
const GOOGLE_RESPONSE_ZERO_RESULTS = 'ZERO_RESULTS';
const GOOGLE_RESPONSE_QUERY_LIMIT = 'OVER_QUERY_LIMIT';
const GOOGLE_RESPONSE_DENIED = 'REQUEST_DENIED';
const GOOGLE_RESPONSE_INVALID = 'INVALID_REQUEST';
const GOOGLE_RESPONSE_UNKNOWN = 'UNKNOWN_ERROR';

/**
* Renders and returns Google Map code.
*
Expand Down Expand Up @@ -50,8 +58,13 @@ public function render($item = -1)
*/
protected function searchLocation($location)
{
$location = urlencode($location);
$request = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address={$location}&sensor=false");
$request = file_get_contents(
sprintf(
'https://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false&key=%s',
urlencode($location),
$this->getKey()
)
);

return json_decode($request);
}
Expand All @@ -64,6 +77,7 @@ protected function searchLocation($location)
* @throws MapperArgumentException
* @throws MapperSearchException
* @throws MapperSearchResultException
* @throws MapperSearchLimitException
* @throws MapperException
*
* @return Location
Expand All @@ -80,27 +94,48 @@ public function location($location)
throw new MapperSearchException('Unable to perform location search, the error was: "' . $exception->getMessage() . '".');
}

if (!isset($resultObject->results) || count($resultObject->results) == 0) {
if (isset($resultObject->status) && $resultObject->status == self::GOOGLE_RESPONSE_QUERY_LIMIT) {
throw new MapperSearchLimitException('Unable to perform location search, your API key is over your quota.');
}

if (isset($resultObject->status) &&
in_array(
$resultObject->status,
[
self::GOOGLE_RESPONSE_DENIED,
self::GOOGLE_RESPONSE_INVALID,
self::GOOGLE_RESPONSE_UNKNOWN
]
)
) {
throw new MapperSearchResultException('An error occurred performing the location search.');
}

if ((isset($resultObject->status) && $resultObject->status == self::GOOGLE_RESPONSE_ZERO_RESULTS) ||
!isset($resultObject->results) ||
(isset($resultObject->results) && count($resultObject->results) == 0)
) {
throw new MapperSearchResultException('No results found for the location search.');
}

if (!isset($resultObject->results[0]->formatted_address) ||
!isset($resultObject->results[0]->address_components[0]->types[0]) ||
!isset($resultObject->results[0]->geometry->location->lat) ||
!isset($resultObject->results[0]->geometry->location->lng) ||
!isset($resultObject->results[0]->place_id)
!isset($resultObject->results[0]->place_id) ||
isset($resultObject->status) && $resultObject->status != self::GOOGLE_RESPONSE_OK
) {
throw new MapperException('The location search return invalid result data.');
}

return new Location([
'mapper' => $this,
'search' => $location,
'address' => $resultObject->results[0]->formatted_address,
'type' => $resultObject->results[0]->address_components[0]->types[0],
'latitude' => $resultObject->results[0]->geometry->location->lat,
'mapper' => $this,
'search' => $location,
'address' => $resultObject->results[0]->formatted_address,
'type' => $resultObject->results[0]->address_components[0]->types[0],
'latitude' => $resultObject->results[0]->geometry->location->lat,
'longitude' => $resultObject->results[0]->geometry->location->lng,
'placeId' => $resultObject->results[0]->place_id,
'placeId' => $resultObject->results[0]->place_id,
]);
}

Expand Down Expand Up @@ -204,7 +239,7 @@ public function marker($latitude, $longitude, array $options = [])
public function informationWindow($latitude, $longitude, $content, array $options = [])
{
$items = $this->getItems();

if (empty($items)) {
throw new MapperException('No map found to add a information window to.');
}
Expand Down

0 comments on commit 8e8eb92

Please sign in to comment.