Skip to content

Commit 0e24631

Browse files
author
Steven Poelmans
committed
Initial commit Places api
1 parent caad510 commit 0e24631

File tree

7 files changed

+314
-15
lines changed

7 files changed

+314
-15
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# GoogleApi
22
PHP library for Google APIs
33

4+
# Installation
5+
6+
composer require treeleaf/google-api
7+
48
# Implemented APIs
59
Places API:
610
- autocomplete
7-
11+
- details (with placeid)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Treeleaf\GoogleApi;
4+
5+
use Treeleaf\GoogleApi\Configurator\ConfiguratorCollection;
6+
use Treeleaf\GoogleApi\Configurator\Places\PlacesAutoCompleteConfigurator;
7+
use Treeleaf\GoogleApi\Configurator\Places\PlacesDetailConfigurator;
8+
use Treeleaf\GoogleApi\Service\Places\PlacesApi;
9+
10+
/**
11+
* Class Bootstrap.
12+
*/
13+
class Bootstrap
14+
{
15+
/**
16+
* @param string $apiKey
17+
*
18+
* @return ConfiguratorCollection
19+
*/
20+
public function getConfiguratorCollection(string $apiKey): ConfiguratorCollection
21+
{
22+
$collection = new ConfiguratorCollection();
23+
$collection->addConfigurator(
24+
PlacesApi::CONFIGURATOR_SERVICE_PLACE_AUTO_COMPLETE,
25+
new PlacesAutoCompleteConfigurator($apiKey)
26+
);
27+
$collection->addConfigurator(
28+
PlacesApi::CONFIGURATOR_SERVICE_PLACE_DETAIL,
29+
new PlacesDetailConfigurator($apiKey)
30+
);
31+
32+
return $collection;
33+
}
34+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Treeleaf\GoogleApi\Configurator\Places;
4+
5+
use Treeleaf\GoogleApi\Configurator\AbstractConfigurator;
6+
7+
/**
8+
* Class PlacesConfigurator.
9+
*/
10+
class PlacesDetailConfigurator extends AbstractConfigurator
11+
{
12+
const URI_PLACE_DETAIL = '/maps/api/place/details';
13+
14+
/**
15+
* PlacesConfigurator constructor.
16+
*
17+
* @param string $apiKey
18+
*/
19+
public function __construct(string $apiKey)
20+
{
21+
parent::__construct($apiKey);
22+
}
23+
24+
/**
25+
* @param string $format
26+
*
27+
* @throws \InvalidArgumentException
28+
*
29+
* @return string
30+
*/
31+
public function getUri(string $format): string
32+
{
33+
$this->checkFormat($format);
34+
35+
return sprintf("%s/%s", self::URI_PLACE_DETAIL, $format);
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function getRequiredParameters(): array
42+
{
43+
return ['key', 'placeid'];
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public function getOptionalParameters(): array
50+
{
51+
return [];
52+
}
53+
}

src/Treeleaf/GoogleApi/Service/AbstractService.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
*/
1313
abstract class AbstractService
1414
{
15+
const STATUS_OK = 'OK';
16+
const STATUS_ZERO_RESULTS = 'ZERO_RESULTS';
17+
const STATUS_OVER_QUERY_LIMIT = 'OVER_QUERY_LIMIT';
18+
const STATUS_INVALID_REQUEST = 'INVALID_REQUEST';
19+
const STATUS_NOT_FOUND = 'NOT_FOUND';
20+
const STATUS_UNKNOWN_ERROR = 'UNKNOWN_ERROR';
21+
1522
/**
1623
* @var Client
1724
*/

src/Treeleaf/GoogleApi/Service/Places/PlacesApi.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Treeleaf\GoogleApi\Service\Places;
44

5-
use Treeleaf\GoogleApi\Configurator\Places\PlacesAutoCompleteConfigurator;
65
use Treeleaf\GoogleApi\Service\AbstractService;
76

87
/**
@@ -11,22 +10,36 @@
1110
class PlacesApi extends AbstractService
1211
{
1312
const CONFIGURATOR_SERVICE_PLACE_AUTO_COMPLETE = 'service.placeAutoComplete';
13+
const CONFIGURATOR_SERVICE_PLACE_DETAIL = 'service.placeDetail';
1414

1515
/**
16-
* @var PlacesAutoCompleteConfigurator
16+
* @param string $query
17+
* @param array $args
18+
* @param string $format
19+
*
20+
* @return string
1721
*/
18-
protected $placesAutoCompleteConfigurator;
22+
public function placeAutoComplete(string $query, array $args = [], string $format = 'json'): string
23+
{
24+
$configurator = $this->getConfigurator(self::CONFIGURATOR_SERVICE_PLACE_AUTO_COMPLETE);
1925

26+
$result = $this->makeRequest($configurator, ['input' => $query], $format, $args);
27+
$response = (string) $result->getBody()->getContents();
28+
29+
return $response;
30+
}
2031
/**
21-
* @param string $query
32+
* @param string $placeId
33+
* @param array $args
34+
* @param string $format
2235
*
2336
* @return string
2437
*/
25-
public function placeAutoComplete(string $query): string
38+
public function placeDetails(string $placeId, array $args = [], string $format = 'json'): string
2639
{
27-
$configurator = $this->getConfigurator(self::CONFIGURATOR_SERVICE_PLACE_AUTO_COMPLETE);
40+
$configurator = $this->getConfigurator(self::CONFIGURATOR_SERVICE_PLACE_DETAIL);
2841

29-
$result = $this->makeRequest($configurator, ['input' => $query]);
42+
$result = $this->makeRequest($configurator, ['placeid' => $placeId], $format, $args);
3043
$response = (string) $result->getBody()->getContents();
3144

3245
return $response;

tests/Treeleaf/GoogleApi/Service/Places/PlacesApiTest.php

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Tests\Treeleaf\GoogleApi\Service\Places;
44

5+
use Treeleaf\GoogleApi\Bootstrap;
56
use Treeleaf\GoogleApi\Configurator\AbstractConfigurator;
7+
use Treeleaf\GoogleApi\Configurator\Places\PlacesDetailConfigurator;
68
use Treeleaf\GoogleApi\Test\AbstractBaseTestCase;
79
use Treeleaf\GoogleApi\Configurator\ConfiguratorCollection;
810
use Treeleaf\GoogleApi\Configurator\Places\PlacesAutoCompleteConfigurator;
@@ -48,7 +50,7 @@ public function testPlaceAutoComplete()
4850
$this->equalTo('GET'),
4951
$this->equalTo(sprintf("%s://%s/%s/json", AbstractConfigurator::DEFAULT_SCHEME, AbstractConfigurator::HOST_GOOGLE_MAPS, PlacesAutoCompleteConfigurator::URI_PLACE_AUTOCOMPLETE)),
5052
$this->equalTo(['query' => [
51-
'key' => 'DummyKey',
53+
'key' => 'DummyKey',
5254
'input' => 'Paris'
5355
]])
5456
)
@@ -65,17 +67,65 @@ public function testPlaceAutoComplete()
6567
$this->assertJsonStringEqualsJsonFile($jsonDir . '/' . $jsonFile, $result);
6668
}
6769

70+
/**
71+
* Test Place auto complete function.
72+
*/
73+
public function testPlaceDetails()
74+
{
75+
$jsonDir = $this->getTestsDataDir();
76+
$jsonFile = 'service.places.placesApi.placeDetails.json';
77+
$json = $this->getTestDataJson($jsonFile);
78+
79+
$streamMock = $this->getMockBuilder('GuzzleHttp\Psr7\Stream')
80+
->disableOriginalConstructor()
81+
->setMethods(['getContents'])
82+
->getMock()
83+
;
84+
$streamMock->expects($this->once())->method('getContents')->willReturn($json);
85+
86+
$responseMock = $this->getMockBuilder('GuzzleHttp\Psr7\Response')
87+
->disableOriginalConstructor()
88+
->setMethods(['getBody'])
89+
->getMock()
90+
;
91+
$responseMock->expects($this->once())->method('getBody')->willReturn($streamMock);
92+
93+
$httpClientMock = $this->getMockBuilder('GuzzleHttp\Client')
94+
->disableOriginalConstructor()
95+
->setMethods(['request'])
96+
->getMock()
97+
;
98+
$httpClientMock
99+
->expects($this->once())
100+
->method('request')
101+
->with(
102+
$this->equalTo('GET'),
103+
$this->equalTo(sprintf("%s://%s/%s/json", AbstractConfigurator::DEFAULT_SCHEME, AbstractConfigurator::HOST_GOOGLE_MAPS, PlacesDetailConfigurator::URI_PLACE_DETAIL)),
104+
$this->equalTo(['query' => [
105+
'key' => 'DummyKey',
106+
'placeid' => 'ChIJD7fiBh9u5kcRYJSMaMOCCwQ'
107+
]])
108+
)
109+
->willReturn($responseMock)
110+
;
111+
112+
$collection = $this->getConfiguratorCollection();
113+
$placesApiService = new PlacesApi($collection, $httpClientMock);
114+
115+
116+
$result = $placesApiService->placeDetails('ChIJD7fiBh9u5kcRYJSMaMOCCwQ');
117+
118+
119+
$this->assertJsonStringEqualsJsonFile($jsonDir . '/' . $jsonFile, $result);
120+
}
121+
68122
/**
69123
* @return ConfiguratorCollection
70124
*/
71125
protected function getConfiguratorCollection(): ConfiguratorCollection
72126
{
73-
$collection = new ConfiguratorCollection();
74-
$collection->addConfigurator(
75-
PlacesApi::CONFIGURATOR_SERVICE_PLACE_AUTO_COMPLETE,
76-
new PlacesAutoCompleteConfigurator('DummyKey')
77-
);
127+
$bootstrap = new Bootstrap();
78128

79-
return $collection;
129+
return $bootstrap->getConfiguratorCollection('DummyKey');
80130
}
81131
}

0 commit comments

Comments
 (0)