1
|
|
|
<?php namespace Arcanedev\GeoLocation\Google\Geocoding; |
2
|
|
|
|
3
|
|
|
use GuzzleHttp\ClientInterface; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class GeocodingService |
7
|
|
|
* |
8
|
|
|
* @package Arcanedev\GeoLocation\Google\Geocoding |
9
|
|
|
* @author ARCANEDEV <[email protected]> |
10
|
|
|
* |
11
|
|
|
* @link https://developers.google.com/maps/documentation/geocoding/intro |
12
|
|
|
*/ |
13
|
|
|
class GeocodingService |
14
|
|
|
{ |
15
|
|
|
/* ----------------------------------------------------------------- |
16
|
|
|
| Constants |
17
|
|
|
| ----------------------------------------------------------------- |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
const BASE_URL = 'https://maps.googleapis.com/maps/api/geocode/json'; |
21
|
|
|
|
22
|
|
|
/* ----------------------------------------------------------------- |
23
|
|
|
| Properties |
24
|
|
|
| ----------------------------------------------------------------- |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
/** @var \GuzzleHttp\ClientInterface */ |
28
|
|
|
protected $client; |
29
|
|
|
|
30
|
|
|
/** @var string|null */ |
31
|
|
|
protected $key = null; |
32
|
|
|
|
33
|
|
|
/* ----------------------------------------------------------------- |
34
|
|
|
| Constructor |
35
|
|
|
| ----------------------------------------------------------------- |
36
|
|
|
*/ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* GoogleDistanceMatrix constructor. |
40
|
|
|
* |
41
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
42
|
|
|
*/ |
43
|
6 |
|
public function __construct(ClientInterface $client) |
44
|
|
|
{ |
45
|
6 |
|
$this->setHttpClient($client); |
46
|
6 |
|
$this->setKey(getenv('GOOGLE_MAPS_GEOCODING_KEY')); |
47
|
6 |
|
} |
48
|
|
|
|
49
|
|
|
/* ----------------------------------------------------------------- |
50
|
|
|
| Getters & Setters |
51
|
|
|
| ----------------------------------------------------------------- |
52
|
|
|
*/ |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Set the HTTP Client. |
56
|
|
|
* |
57
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
58
|
|
|
* |
59
|
|
|
* @return self |
60
|
|
|
*/ |
61
|
6 |
|
public function setHttpClient(ClientInterface $client) |
62
|
|
|
{ |
63
|
6 |
|
$this->client = $client; |
64
|
|
|
|
65
|
6 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set the API Key. |
70
|
|
|
* |
71
|
|
|
* @param string $key |
72
|
|
|
* |
73
|
|
|
* @return self |
74
|
|
|
*/ |
75
|
6 |
|
public function setKey($key) |
76
|
|
|
{ |
77
|
6 |
|
$this->key = $key; |
78
|
|
|
|
79
|
6 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/* ----------------------------------------------------------------- |
83
|
|
|
| Main Methods |
84
|
|
|
| ----------------------------------------------------------------- |
85
|
|
|
*/ |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get the geocoding response. |
89
|
|
|
* |
90
|
|
|
* @param string $address |
91
|
|
|
* @param array $options |
92
|
|
|
* |
93
|
|
|
* @return \Arcanedev\GeoLocation\Google\Geocoding\GeocodingResponse |
94
|
|
|
*/ |
95
|
3 |
|
public function geocode($address, array $options = []) |
96
|
|
|
{ |
97
|
3 |
|
$url = static::BASE_URL.'?'.$this->prepareQuery($address); |
98
|
|
|
|
99
|
3 |
|
$result = $this->client->request('GET', $url, $options); |
100
|
|
|
|
101
|
3 |
|
return new GeocodingResponse( |
102
|
3 |
|
json_decode($result->getBody(), true) |
103
|
1 |
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Prepare the URL query. |
108
|
|
|
* |
109
|
|
|
* @param string $address |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
3 |
|
private function prepareQuery($address) |
114
|
|
|
{ |
115
|
3 |
|
$queryData = array_filter([ |
116
|
3 |
|
'address' => urlencode($address), |
117
|
3 |
|
'key' => $this->key, |
118
|
1 |
|
]); |
119
|
|
|
|
120
|
3 |
|
return urldecode(http_build_query($queryData)); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|