|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\google_maps\Services; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\google_maps\ServiceException; |
|
7
|
|
|
use Psr\Http\Message\RequestInterface; |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Nearby service |
|
12
|
|
|
* |
|
13
|
|
|
* @see https://developers.google.com/maps/documentation/places/web-service/search-nearby |
|
14
|
|
|
*/ |
|
15
|
|
|
class Nearby extends AbstractService |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Nearby lookup |
|
19
|
|
|
* |
|
20
|
|
|
* @param string $keyword |
|
21
|
|
|
* @param array<string|int, float> $latlng ['lat', 'lng'] |
|
22
|
|
|
* @param float|null $radius |
|
23
|
|
|
* @param string|null $type as wanted by Google |
|
24
|
|
|
* @param array<string, string|int|float> $params Query parameters |
|
25
|
|
|
* @throws ServiceException |
|
26
|
|
|
* @return RequestInterface |
|
27
|
|
|
*/ |
|
28
|
5 |
|
public function nearby(string $keyword, array $latlng = [], ?float $radius = null, ?string $type = null, array $params = []): RequestInterface |
|
29
|
|
|
{ |
|
30
|
5 |
|
if (empty($keyword) && empty($latlng)) { |
|
31
|
1 |
|
throw new ServiceException('You must set where to look!'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
// Main wanted name |
|
35
|
4 |
|
if (!empty($keyword)) { |
|
36
|
1 |
|
$params['keyword'] = $keyword; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
// `location` seems to only allow `lat,lng` pattern |
|
40
|
4 |
|
if (!empty($latlng)) { |
|
41
|
|
|
|
|
42
|
3 |
|
if (isset($latlng['lat']) && isset($latlng['lng'])) { |
|
43
|
1 |
|
$params['latlng'] = sprintf('%1.08F,%1.08F', $latlng['lat'], $latlng['lng']); |
|
44
|
|
|
|
|
45
|
2 |
|
} elseif (isset($latlng[0]) && isset($latlng[1])) { |
|
46
|
1 |
|
$params['latlng'] = sprintf('%1.08F,%1.08F', $latlng[0], $latlng[1]); |
|
47
|
|
|
|
|
48
|
|
|
} else { |
|
49
|
1 |
|
throw new ServiceException('Passed invalid values into coordinates! You must use either array with lat and lng or 0 and 1 keys.'); |
|
50
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
3 |
|
if (!empty($radius)) { |
|
55
|
1 |
|
$params['radius'] = $radius; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
3 |
|
if (!empty($type)) { |
|
59
|
1 |
|
$params['type'] = $type; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
3 |
|
return $this->getWithDefaults( |
|
63
|
3 |
|
static::API_HOST . '/maps/api/place/nearbysearch/json', |
|
64
|
3 |
|
$this->queryParamsLang($params) |
|
65
|
3 |
|
); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|