1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ivory Google Map package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ivory\GoogleMap\Service; |
13
|
|
|
|
14
|
|
|
use Http\Client\HttpClient; |
15
|
|
|
use Http\Message\MessageFactory; |
16
|
|
|
use Ivory\GoogleMap\Service\Utility\JsonParser; |
17
|
|
|
use Ivory\GoogleMap\Service\Utility\Parser; |
18
|
|
|
use Ivory\GoogleMap\Service\Utility\XmlParser; |
19
|
|
|
use Psr\Http\Message\RequestInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author GeLo <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
abstract class AbstractService |
25
|
|
|
{ |
26
|
|
|
const FORMAT_JSON = Parser::FORMAT_JSON; |
27
|
|
|
const FORMAT_XML = Parser::FORMAT_XML; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var HttpClient |
31
|
|
|
*/ |
32
|
|
|
private $client; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var MessageFactory |
36
|
|
|
*/ |
37
|
|
|
private $messageFactory; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var Parser |
41
|
|
|
*/ |
42
|
|
|
private $parser; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $url; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var bool |
51
|
|
|
*/ |
52
|
|
|
private $https = true; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
private $format = self::FORMAT_JSON; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var string|null |
61
|
|
|
*/ |
62
|
|
|
private $key; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var BusinessAccount|null |
66
|
|
|
*/ |
67
|
|
|
private $businessAccount; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param HttpClient $client |
71
|
|
|
* @param MessageFactory $messageFactory |
72
|
|
|
* @param string $url |
73
|
|
|
* @param Parser|null $parser |
74
|
|
|
*/ |
75
|
|
|
public function __construct(HttpClient $client, MessageFactory $messageFactory, $url, Parser $parser = null) |
76
|
|
|
{ |
77
|
|
|
if ($parser === null) { |
78
|
|
|
$parser = new Parser([ |
79
|
|
|
self::FORMAT_JSON => new JsonParser(), |
80
|
|
|
self::FORMAT_XML => new XmlParser(), |
81
|
|
|
]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->setClient($client); |
85
|
|
|
$this->setMessageFactory($messageFactory); |
86
|
|
|
$this->setUrl($url); |
87
|
|
|
$this->setParser($parser); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return HttpClient |
92
|
|
|
*/ |
93
|
|
|
public function getClient() |
94
|
|
|
{ |
95
|
|
|
return $this->client; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param HttpClient $client |
100
|
|
|
*/ |
101
|
|
|
public function setClient(HttpClient $client) |
102
|
|
|
{ |
103
|
|
|
$this->client = $client; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return MessageFactory |
108
|
|
|
*/ |
109
|
|
|
public function getMessageFactory() |
110
|
|
|
{ |
111
|
|
|
return $this->messageFactory; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param MessageFactory $messageFactory |
116
|
|
|
*/ |
117
|
|
|
public function setMessageFactory(MessageFactory $messageFactory) |
118
|
|
|
{ |
119
|
|
|
$this->messageFactory = $messageFactory; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return Parser |
124
|
|
|
*/ |
125
|
|
|
public function getParser() |
126
|
|
|
{ |
127
|
|
|
return $this->parser; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param Parser $parser |
132
|
|
|
*/ |
133
|
|
|
public function setParser(Parser $parser) |
134
|
|
|
{ |
135
|
|
|
$this->parser = $parser; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
public function getUrl() |
142
|
|
|
{ |
143
|
|
|
if ($this->isHttps()) { |
144
|
|
|
return str_replace('http://', 'https://', $this->url); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $this->url; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param string $url |
152
|
|
|
*/ |
153
|
|
|
public function setUrl($url) |
154
|
|
|
{ |
155
|
|
|
$this->url = $url; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return bool |
160
|
|
|
*/ |
161
|
|
|
public function isHttps() |
162
|
|
|
{ |
163
|
|
|
return $this->https; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param bool $https |
168
|
|
|
*/ |
169
|
|
|
public function setHttps($https) |
170
|
|
|
{ |
171
|
|
|
$this->https = $https; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
|
|
public function getFormat() |
178
|
|
|
{ |
179
|
|
|
return $this->format; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param string $format |
184
|
|
|
*/ |
185
|
|
|
public function setFormat($format) |
186
|
|
|
{ |
187
|
|
|
$this->format = $format; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return bool |
192
|
|
|
*/ |
193
|
|
|
public function hasKey() |
194
|
|
|
{ |
195
|
|
|
return $this->key !== null; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return string|null |
200
|
|
|
*/ |
201
|
|
|
public function getKey() |
202
|
|
|
{ |
203
|
|
|
return $this->key; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param string|null $key |
208
|
|
|
*/ |
209
|
|
|
public function setKey($key) |
210
|
|
|
{ |
211
|
|
|
$this->key = $key; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @return bool |
216
|
|
|
*/ |
217
|
|
|
public function hasBusinessAccount() |
218
|
|
|
{ |
219
|
|
|
return $this->businessAccount !== null; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @return BusinessAccount |
224
|
|
|
*/ |
225
|
|
|
public function getBusinessAccount() |
226
|
|
|
{ |
227
|
|
|
return $this->businessAccount; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param BusinessAccount $businessAccount |
232
|
|
|
*/ |
233
|
|
|
public function setBusinessAccount(BusinessAccount $businessAccount = null) |
234
|
|
|
{ |
235
|
|
|
$this->businessAccount = $businessAccount; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @param string[] $query |
240
|
|
|
* |
241
|
|
|
* @return RequestInterface |
242
|
|
|
*/ |
243
|
|
|
protected function createRequest(array $query) |
244
|
|
|
{ |
245
|
|
|
if ($this->hasKey()) { |
246
|
|
|
$query['key'] = $this->key; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
$url = $this->getUrl().'/'.$this->getFormat().'?'.http_build_query($query, '', '&'); |
250
|
|
|
|
251
|
|
|
if ($this->hasBusinessAccount()) { |
252
|
|
|
$url = $this->businessAccount->signUrl($url); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
return $this->messageFactory->createRequest('GET', $url); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @param string $data |
260
|
|
|
* @param mixed[] $options |
261
|
|
|
* |
262
|
|
|
* @return mixed[] |
263
|
|
|
*/ |
264
|
|
|
protected function parse($data, array $options = []) |
265
|
|
|
{ |
266
|
|
|
return $this->parser->parse($data, $this->getFormat(), $options); |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|