1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2018 - present |
4
|
|
|
* Google Maps PHP - Api.php |
5
|
|
|
* author: Roberto Belotti - [email protected] |
6
|
|
|
* web : robertobelotti.com, github.com/biscolab |
7
|
|
|
* Initial version created on: 5/9/2018 |
8
|
|
|
* MIT license: https://github.com/biscolab/google-maps-php/blob/master/LICENSE |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Biscolab\GoogleMaps\Abstracts; |
12
|
|
|
|
13
|
|
|
use Biscolab\GoogleMaps\Enum\GoogleMapsApiConfigFields; |
14
|
|
|
use Biscolab\GoogleMaps\Fields\GoogleMapsRequestFields; |
15
|
|
|
use Biscolab\GoogleMaps\GoogleMapsApi; |
16
|
|
|
use Biscolab\GoogleMaps\Http\GoogleMapsRequest; |
17
|
|
|
use Biscolab\GoogleMaps\Http\GoogleMapsResponse; |
18
|
|
|
use Biscolab\GoogleMaps\Http\GoogleMapsResult; |
19
|
|
|
use Biscolab\GoogleMaps\Http\GoogleMapsResultsCollection; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class Api |
23
|
|
|
* @package Biscolab\GoogleMaps\Abstracts |
24
|
|
|
*/ |
25
|
|
|
abstract class Api |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
const SERVICE_ENDPOINT = null; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var GoogleMapsApi |
35
|
|
|
*/ |
36
|
|
|
protected $google_maps_api = null; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $result_type = ''; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $result_collection_type = ''; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var GoogleMapsResponse |
50
|
|
|
*/ |
51
|
|
|
protected $response; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var GoogleMapsRequest |
55
|
|
|
*/ |
56
|
|
|
protected $request; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Api constructor. |
60
|
|
|
* |
61
|
|
|
* @param array $config |
62
|
|
|
*/ |
63
|
32 |
|
public function __construct(array $config = []) |
64
|
|
|
{ |
65
|
|
|
|
66
|
32 |
|
$service_config = $this->getServiceConfig($config); |
67
|
32 |
|
$this->setGoogleMapsApi(new GoogleMapsApi($service_config)); |
68
|
32 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param array $config |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
32 |
|
protected function getServiceConfig(array $config = []): array |
76
|
|
|
{ |
77
|
|
|
|
78
|
32 |
|
return array_merge($config, [ |
79
|
32 |
|
GoogleMapsApiConfigFields::SERVICE_ENDPOINT => $this->getServiceEndpoint() |
80
|
|
|
]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
32 |
|
public function getServiceEndpoint(): string |
87
|
|
|
{ |
88
|
|
|
|
89
|
32 |
|
return static::SERVICE_ENDPOINT ?? ''; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param array $params |
94
|
|
|
* @param null|string $endpoint |
95
|
|
|
* |
96
|
|
|
* @return GoogleMapsResult|GoogleMapsResultsCollection |
97
|
|
|
*/ |
98
|
|
|
public function callApi(array $params, ?string $endpoint = null) |
99
|
|
|
{ |
100
|
|
|
|
101
|
|
|
$this->createRequest($params, $endpoint); |
102
|
|
|
|
103
|
|
|
return $this->getResponseResult(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param array $params |
108
|
|
|
* @param null|string $endpoint since 0.5.0 |
109
|
|
|
* |
110
|
|
|
* @return GoogleMapsRequest |
111
|
|
|
*/ |
112
|
|
|
public function createRequest(array $params, ?string $endpoint = null): GoogleMapsRequest |
113
|
|
|
{ |
114
|
|
|
|
115
|
|
|
$this->request = new GoogleMapsRequest($params, $endpoint);; |
116
|
|
|
|
117
|
|
|
return $this->request; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return GoogleMapsResult|GoogleMapsResultsCollection |
122
|
|
|
*/ |
123
|
|
|
public function getResponseResult() |
124
|
|
|
{ |
125
|
|
|
|
126
|
|
|
$result = $this->getResponse()->getResult(); |
127
|
|
|
if ($result) { |
|
|
|
|
128
|
|
|
$result_type = $this->result_type; |
129
|
|
|
|
130
|
|
|
return new $result_type($result); |
131
|
|
|
} |
132
|
|
|
$results = $this->getResponse()->getResults(); |
133
|
|
|
|
134
|
|
|
$result_collection_type_class = $this->result_collection_type; |
135
|
|
|
|
136
|
|
|
return new $result_collection_type_class($results); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return GoogleMapsResponse |
141
|
|
|
*/ |
142
|
|
|
public function getResponse(): GoogleMapsResponse |
143
|
|
|
{ |
144
|
|
|
|
145
|
|
|
$this->response = $this->getGoogleMapsApi()->get($this->request); |
146
|
|
|
|
147
|
|
|
return $this->response; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return GoogleMapsApi |
152
|
|
|
*/ |
153
|
10 |
|
public function getGoogleMapsApi(): GoogleMapsApi |
154
|
|
|
{ |
155
|
|
|
|
156
|
10 |
|
return $this->google_maps_api; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param GoogleMapsApi $google_maps_api |
161
|
|
|
* |
162
|
|
|
* @return Api |
163
|
|
|
*/ |
164
|
32 |
|
public function setGoogleMapsApi(GoogleMapsApi $google_maps_api): Api |
165
|
|
|
{ |
166
|
|
|
|
167
|
32 |
|
$this->google_maps_api = $google_maps_api; |
168
|
|
|
|
169
|
32 |
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return GoogleMapsResultsCollection |
174
|
|
|
*/ |
175
|
|
|
public function getSingleResult(): GoogleMapsResultsCollection |
176
|
|
|
{ |
177
|
|
|
|
178
|
|
|
$results = $this->getResponse()->getResults(); |
179
|
|
|
|
180
|
|
|
$result_collection_type_class = $this->result_collection_type; |
181
|
|
|
|
182
|
|
|
return new $result_collection_type_class($results); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return GoogleMapsResultsCollection |
187
|
|
|
*/ |
188
|
|
|
public function getNextPage(): GoogleMapsResultsCollection |
189
|
|
|
{ |
190
|
|
|
|
191
|
|
|
if ($this->responseHasNewPage()) { |
192
|
|
|
$this->request->setParam(GoogleMapsRequestFields::NEXT_PAGE_TOKEN, $this->response->getNextPageToken()); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return $this->getResponseResult(); |
|
|
|
|
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return bool |
200
|
|
|
*/ |
201
|
|
|
public function responseHasNewPage(): bool |
202
|
|
|
{ |
203
|
|
|
|
204
|
|
|
return ($this->response instanceof GoogleMapsResponse) ? $this->response->getNextPageToken() : false; |
|
|
|
|
205
|
|
|
} |
206
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.