Skip to content

Commit fdc3459

Browse files
committed
FIrst commit
0 parents  commit fdc3459

13 files changed

+522
-0
lines changed

LICENSE

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Copyright (c) 2013-2013 Maxime Veber
2+
Permission is hereby granted, free of charge, to any person obtaining a copy
3+
of this software and associated documentation files (the "Software"), to deal
4+
in the Software without restriction, including without limitation the rights
5+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
6+
copies of the Software, and to permit persons to whom the Software is furnished
7+
to do so, subject to the following conditions:
8+
The above copyright notice and this permission notice shall be included in all
9+
copies or substantial portions of the Software.
10+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16+
THE SOFTWARE.

README.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Places API
2+
==========
3+
4+
This library helps you to ask for Google Places API.
5+
6+
Installation
7+
------------
8+
9+
It's simple ! Just use the following composer command line.
10+
11+
```bash
12+
composer require 'nekland/places-api:~1.0'
13+
```
14+
15+
Usage
16+
-----
17+
18+
```php
19+
<?php
20+
21+
use Nekland\PlacesApi\Places;
22+
23+
$api = new Places();
24+
25+
$api->useAuthentication('PublicApiAccess', ['key' => 'MY KEY']);
26+
27+
$result = $api->getSearchApi()->search('49.8445057,3.2912589', 1000);
28+
```
29+
30+
31+
Who needs a documentation ? NeklandPlacesApi supports provide auto-completion for compatibles IDEs.
32+
33+
If yours don't, no problem: the documentation is available [here](docs/index.md).

composer.json

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "nekland/places-api",
3+
"type": "library",
4+
"description": "Google Places API made easy !",
5+
"autoload": {
6+
"psr-0": { "": "src/" }
7+
},
8+
"keywords": ["nekland", "api", "places"],
9+
"license": "MIT",
10+
"authors": [
11+
{
12+
"name": "Maxime Veber",
13+
"email": "[email protected]",
14+
"homepage": "http://nekland.fr"
15+
}
16+
],
17+
"require": {
18+
"php": ">=5.4",
19+
"guzzlehttp/guzzle": "~5.0",
20+
"nekland/base-api": "~1.0"
21+
},
22+
"require-dev": {
23+
"phpspec/phpspec": "~2.0"
24+
},
25+
"minimum-stability": "dev",
26+
"extra": {
27+
"branch-alias": {
28+
"dev-master": "1.x-dev"
29+
}
30+
}
31+
}
32+

docs/api_autocomplete.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Autocomplete API reference
2+
====================
3+
4+
Retrieving API
5+
--------------
6+
7+
```php
8+
<?php
9+
10+
$api = new Autocomplete();
11+
12+
$autocomplete = $api->getAutocompleteApi();
13+
```
14+
15+
Available methods
16+
-----------------
17+
18+
### Get autocomplete from input
19+
20+
```php
21+
<?php
22+
23+
array Autocomplete::autocomplete($input [, array $other = []])
24+
```
25+
26+
* `input`: the input of your user
27+
* `other`: see documentation here https://developers.google.com/places/documentation/autocomplete

docs/api_places.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Places API Reference
2+
====================
3+
4+
Retrieving API
5+
--------------
6+
7+
```php
8+
<?php
9+
10+
$api = new Places();
11+
12+
$places = $api->getPlacesApi();
13+
```
14+
15+
16+
Available methods
17+
-----------------
18+
19+
### Retrieve details for a place using id
20+
21+
```php
22+
<?php
23+
24+
array public Places::getPlaceById($id, [, array $extensions = [], [ $language = null ]])
25+
26+
```
27+
28+
* `id`: a string representing a place (notice that this is **not** the `reference` of the place)
29+
* `extensions`: an array of data you want to retrieve in addition of default data send by google
30+
* `language`: one of the languages listed here: https://developers.google.com/maps/faq#languagesupport

docs/api_search.md

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Search API reference
2+
====================
3+
4+
Retrieving API
5+
--------------
6+
7+
```php
8+
<?php
9+
10+
$api = new Places();
11+
12+
$search = $api->getSearchApi();
13+
```
14+
15+
Available methods
16+
-----------------
17+
18+
### Search by location and radius
19+
20+
```php
21+
<?php
22+
23+
array Search::search($location, $radius [, array $other = []])
24+
```
25+
26+
* `location`: a location (latitude,longitude), i.e. `48.8588589,2.3470599,13`
27+
* `radius`: distance in meters around the location, i.e. `1000` (meaning 1 kilometer)
28+
* `other`: see all possible parameters here: https://developers.google.com/places/documentation/search
29+
30+
### Search by text query
31+
32+
```php
33+
<?php
34+
35+
array Search::textSearch($text [, array $others = []])
36+
```
37+
38+
* `text`: some text defining a place
39+
* `other`: see https://developers.google.com/places/documentation/search
40+
41+
> Notice: if you specify location, you need to specify also radius

docs/authentication.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Authentication
2+
==============
3+
4+
Public API access
5+
-----------------
6+
7+
To make this authentication work, you just have to generate an APIKEY from the google developer console. You can do it like that:
8+
9+
1. Go to https://console.developers.google.com/project;
10+
2. Create your project if it's not already in the project list (we do not care about the project id);
11+
3. Click on the name of your project in the list;
12+
4. Click on APIS & AUTH > API and on "Places API" in the API list and click on "OFF" button to make it becoming "ON";
13+
5. Click on APIS & AUTH > Credentials;
14+
6. In the section Public API access, create a new key of type Server Key and specify your IP address.
15+
16+
> Be careful, Google take care of IPv6.
17+
18+
```php
19+
<?php
20+
21+
$api = new Places();
22+
$api->authentication([
23+
'PublicApiAccess',
24+
['key' => 'MY_PLACES_APIKEY']
25+
]);
26+
```

docs/index.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Places API
2+
==========
3+
4+
Installation
5+
------------
6+
7+
```bash
8+
composer require 'nekland/places-api:~1.0'
9+
```
10+
11+
This will add `nekland/youtube-api` to your composer.json file and automatically download it to your `vendor` folder.
12+
13+
14+
Usage
15+
-----
16+
17+
18+
1. Declare a new instance of Places object
19+
2. Configure an authentication strategy
20+
3. Get an API class and ask data from it
21+
22+
Example:
23+
24+
```php
25+
<?php
26+
27+
use Nekland\PlacesApi\Places;
28+
29+
$api = new Places();
30+
31+
$api->useAuthentication('PublicApiAccess', ['key' => 'SOME KEY']);
32+
33+
$result = $api->getSearchApi()->search('49.8445057,3.2912589', 1000);
34+
35+
```
36+
37+
More
38+
----
39+
40+
1. Learn more about [authentication](authentication.md)
41+
2. [Api Search reference](api_search.md)
42+
3. [Api Places reference](api_places.md)
43+
4. [Api Autocomplete reference](api_autocomplete.md)
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/**
4+
* This file is a part of nekland places api package
5+
*
6+
* (c) Nekland <[email protected]>
7+
*
8+
* For the full license, take a look to the LICENSE file
9+
* on the root directory of this project
10+
*/
11+
12+
namespace Nekland\PlacesApi\Api;
13+
14+
15+
use Nekland\BaseApi\Api\AbstractApi;
16+
use Nekland\BaseApi\Exception\AuthenticationException;
17+
use Nekland\BaseApi\Exception\QuotaLimitException;
18+
19+
class Autocomplete extends AbstractApi
20+
{
21+
const URL = 'autocomplete/json';
22+
23+
public function autocomplete($input, array $other = [])
24+
{
25+
$body = array_merge(['input' => $input], $other);
26+
27+
$result = $this->get(self::URL, $body);
28+
29+
switch($result['status']) {
30+
case 'OK':
31+
return $result['predictions'];
32+
33+
case 'ZERO_RESULTS':
34+
return [];
35+
36+
case 'OVER_QUERY_LIMIT':
37+
throw new QuotaLimitException();
38+
break;
39+
40+
case 'REQUEST_DENIED':
41+
throw new AuthenticationException();
42+
break;
43+
44+
case 'INVALID_REQUEST':
45+
throw new \InvalidArgumentException('The input is not correct for autocomplete.');
46+
break;
47+
48+
default:
49+
throw new \RuntimeException('This case is not supported, please report these to NeklandPlacesApi.');
50+
}
51+
}
52+
}

src/Nekland/PlacesApi/Api/Places.php

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/**
4+
* This file is a part of nekland places api package
5+
*
6+
* (c) Nekland <[email protected]>
7+
*
8+
* For the full license, take a look to the LICENSE file
9+
* on the root directory of this project
10+
*/
11+
12+
namespace Nekland\PlacesApi\Api;
13+
14+
15+
use Doctrine\Instantiator\Exception\InvalidArgumentException;
16+
use Nekland\BaseApi\Api\AbstractApi;
17+
18+
/**
19+
* Class Places
20+
*
21+
* Return a place resource
22+
*/
23+
class Places extends AbstractApi
24+
{
25+
const URL = 'details/json';
26+
private $validExtensions = ['review_summary'];
27+
28+
/**
29+
* To see a list of supported languages, please checkout https://developers.google.com/maps/faq#languagesupport
30+
*
31+
* @param string $id
32+
* @param array $extensions
33+
* @param string $language
34+
* @return array
35+
*/
36+
public function getPlaceById($id, array $extensions = [], $language = null)
37+
{
38+
$this->validateExtension($extensions);
39+
$extensions = implode(',', $extensions);
40+
41+
$body = array_merge(
42+
['placeid' => $id],
43+
!empty($extensions) ? ['extensions' => $extensions] : [],
44+
null !== $language ? ['language' => $language] : []
45+
);
46+
47+
return $this->get(self::URL, $body);
48+
}
49+
50+
/**
51+
* @param array $extensions
52+
*/
53+
private function validateExtension(array $extensions)
54+
{
55+
foreach ($extensions as $extension) {
56+
if (!in_array($extension, $this->validExtensions)) {
57+
throw new \InvalidArgumentException(sprintf('The extensions can only be one of "%s"', implode(',', $this->validExtensions)));
58+
}
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)