1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the 2amigos/google-places-library project. |
5
|
|
|
* |
6
|
|
|
* (c) 2amigOS! <http://2amigos.us/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Da\Google\Places\Client; |
13
|
|
|
|
14
|
|
|
use GuzzleHttp\Exception\RequestException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Search handles places searching requests. |
18
|
|
|
* |
19
|
|
|
* @author Antonio Ramirez <[email protected]> |
20
|
|
|
* |
21
|
|
|
* @link http://www.ramirezcobos.com/ |
22
|
|
|
* @link http://www.2amigos.us/ |
23
|
|
|
* |
24
|
|
|
*/ |
25
|
|
|
class SearchClient extends AbstractClient |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Returns places within a specific area. |
29
|
|
|
* |
30
|
|
|
* @see https://developers.google.com/places/documentation/search |
31
|
|
|
* @see https://developers.google.com/places/documentation/supported_types |
32
|
|
|
* @see https://spreadsheets.google.com/pub?key=p9pdwsai2hDMsLkXsoM05KQ&gid=1 |
33
|
|
|
* |
34
|
|
|
* @param string $location The latitude/longitude around which to retrieve Place information. |
35
|
|
|
* This must be specified as latitude,longitude. |
36
|
|
|
* @param array $params optional parameters |
37
|
|
|
* |
38
|
|
|
* @throws \InvalidArgumentException |
39
|
|
|
* @throws RequestException if the request fails |
40
|
|
|
* |
41
|
|
|
* @return mixed|null |
42
|
|
|
*/ |
43
|
|
|
public function nearby($location, $params = []) |
44
|
|
|
{ |
45
|
|
|
$rankBy = trim(strtolower($this->getParamValue($params, 'rankby', 'prominence'))); |
46
|
|
|
if (!in_array($rankBy, ['distance', 'prominence'], true)) { |
47
|
|
|
throw new \InvalidArgumentException("Unrecognized rank '$rankBy'"); |
48
|
|
|
} |
49
|
|
|
if ($rankBy == 'distance') { |
50
|
|
|
if (!isset($params['keyword']) && !isset($params['name']) && !isset($params['type'])) { |
51
|
|
|
throw new \InvalidArgumentException( |
52
|
|
|
'When using "rankby":"distance", you must specify at least one of the following: keyword, name, type.' |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
unset($params['radius']); |
56
|
|
|
} |
57
|
|
|
if ($rankBy == 'prominence' && !isset($params['radius'])) { |
58
|
|
|
throw new \InvalidArgumentException('When using "rankby":"prominence" you must specify a radius.'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$params['location'] = $location; |
62
|
|
|
$params['sensor'] = $this->getParamValue($params, 'sensor', 'false'); |
63
|
|
|
|
64
|
|
|
return $this->request('nearbysearch', 'get', $params); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns places based on a string. |
69
|
|
|
* |
70
|
|
|
* @see https://developers.google.com/places/documentation/search#TextSearchRequests |
71
|
|
|
* |
72
|
|
|
* @param string $query The text string on which to search, for example: "restaurant". The Place service will return |
73
|
|
|
* candidate matches based on this string and order the results based on their perceived relevance. |
74
|
|
|
* @param array $params optional parameters |
75
|
|
|
* |
76
|
|
|
* @throws RequestException if the request fails |
77
|
|
|
* |
78
|
|
|
* @return mixed|null |
79
|
|
|
*/ |
80
|
|
|
public function text($query, $params = []) |
81
|
|
|
{ |
82
|
|
|
$params['query'] = $query; |
83
|
|
|
|
84
|
|
|
return $this->request('textsearch', 'get', $params); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns places of a specific area. |
89
|
|
|
* |
90
|
|
|
* @param string $location The latitude/longitude around which to retrieve Place information. This must be specified |
91
|
|
|
* as latitude,longitude. |
92
|
|
|
* @param string $radius Defines the distance (in meters) within which to return Place results. The maximum allowed |
93
|
|
|
* radius is 50 000 meters. |
94
|
|
|
* @param array $params optional parameters |
95
|
|
|
* |
96
|
|
|
* @throws \InvalidArgumentException |
97
|
|
|
* @throws RequestException if the request fails |
98
|
|
|
* |
99
|
|
|
* @return mixed|null |
100
|
|
|
*/ |
101
|
|
|
public function radar($location, $radius, $params = []) |
102
|
|
|
{ |
103
|
|
|
if (!isset($params['keyword']) && !isset($params['name']) && !isset($params['type'])) { |
104
|
|
|
throw new \InvalidArgumentException( |
105
|
|
|
'When using radar you must include at least one of keyword, name, or type.' |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
$params['location'] = $location; |
109
|
|
|
$params['radius'] = $radius; |
110
|
|
|
|
111
|
|
|
return $this->request('radarsearch', 'get', $params); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns place predictions based on specific text and optional geographic bounds. |
116
|
|
|
* |
117
|
|
|
* @see https://developers.google.com/places/documentation/autocomplete#place_autocomplete_requests |
118
|
|
|
* @see https://developers.google.com/places/documentation/autocomplete#example_autocomplete_requests |
119
|
|
|
* |
120
|
|
|
* @param string $input The text string on which to search. The Place Autocomplete service will return candidate |
121
|
|
|
* matches based on this string and order results based on their perceived relevance. |
122
|
|
|
* @param string $language The language in which to return results. |
123
|
|
|
* @param array $params optional parameters |
124
|
|
|
* |
125
|
|
|
* @throws RequestException if the request fails |
126
|
|
|
* |
127
|
|
|
* @return mixed|null |
128
|
|
|
*/ |
129
|
|
|
public function autoComplete($input, $language = 'en', $params = []) |
130
|
|
|
{ |
131
|
|
|
$params['input'] = $input; |
132
|
|
|
$params['language'] = $language; |
133
|
|
|
|
134
|
|
|
return $this->request('autocomplete', 'get', $params); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|