|
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\Place\Detail; |
|
13
|
|
|
|
|
14
|
|
|
use Http\Client\HttpClient; |
|
15
|
|
|
use Http\Message\MessageFactory; |
|
16
|
|
|
use Ivory\GoogleMap\Base\Bound; |
|
17
|
|
|
use Ivory\GoogleMap\Base\Coordinate; |
|
18
|
|
|
use Ivory\GoogleMap\Service\AbstractParsableService; |
|
19
|
|
|
use Ivory\GoogleMap\Service\Base\AddressComponent; |
|
20
|
|
|
use Ivory\GoogleMap\Service\Base\Geometry; |
|
21
|
|
|
use Ivory\GoogleMap\Service\Place\Base\AlternatePlaceId; |
|
22
|
|
|
use Ivory\GoogleMap\Service\Place\Base\AspectRating; |
|
23
|
|
|
use Ivory\GoogleMap\Service\Place\Base\OpenClosePeriod; |
|
24
|
|
|
use Ivory\GoogleMap\Service\Place\Base\OpeningHours; |
|
25
|
|
|
use Ivory\GoogleMap\Service\Place\Base\Period; |
|
26
|
|
|
use Ivory\GoogleMap\Service\Place\Base\Photo; |
|
27
|
|
|
use Ivory\GoogleMap\Service\Place\Base\Place; |
|
28
|
|
|
use Ivory\GoogleMap\Service\Place\Base\Review; |
|
29
|
|
|
use Ivory\GoogleMap\Service\Place\Detail\Request\PlaceDetailRequestInterface; |
|
30
|
|
|
use Ivory\GoogleMap\Service\Place\Detail\Response\PlaceDetailResponse; |
|
31
|
|
|
use Ivory\GoogleMap\Service\Utility\Parser; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @author GeLo <[email protected]> |
|
35
|
|
|
*/ |
|
36
|
|
|
class PlaceDetailService extends AbstractParsableService |
|
37
|
|
|
{ |
|
38
|
|
|
/** |
|
39
|
|
|
* @param HttpClient $client |
|
40
|
|
|
* @param MessageFactory $messageFactory |
|
41
|
|
|
* @param Parser|null $parser |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct(HttpClient $client, MessageFactory $messageFactory, Parser $parser = null) |
|
44
|
|
|
{ |
|
45
|
|
|
parent::__construct($client, $messageFactory, 'http://maps.googleapis.com/maps/api/place/details', $parser); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
public function setHttps($https) |
|
52
|
|
|
{ |
|
53
|
|
|
if (!$https) { |
|
54
|
|
|
throw new \InvalidArgumentException('The http scheme is not supported.'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
parent::setHttps($https); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param PlaceDetailRequestInterface $request |
|
62
|
|
|
* |
|
63
|
|
|
* @return PlaceDetailResponse |
|
64
|
|
|
*/ |
|
65
|
|
|
public function process(PlaceDetailRequestInterface $request) |
|
66
|
|
|
{ |
|
67
|
|
|
$httpRequest = $this->createRequest($request); |
|
68
|
|
|
$httpResponse = $this->getClient()->sendRequest($httpRequest); |
|
69
|
|
|
|
|
70
|
|
|
$data = $this->parse((string) $httpResponse->getBody(), [ |
|
71
|
|
|
'pluralization_rules' => [ |
|
72
|
|
|
'address_component' => 'address_components', |
|
73
|
|
|
'aspect' => 'aspects', |
|
74
|
|
|
'html_attribution' => 'html_attributions', |
|
75
|
|
|
'period' => 'periods', |
|
76
|
|
|
'photo' => 'photos', |
|
77
|
|
|
'type' => 'types', |
|
78
|
|
|
'review' => 'reviews', |
|
79
|
|
|
], |
|
80
|
|
|
]); |
|
81
|
|
|
|
|
82
|
|
|
$response = $this->buildResponse($data); |
|
83
|
|
|
$response->setRequest($request); |
|
84
|
|
|
|
|
85
|
|
|
return $response; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param mixed[] $data |
|
90
|
|
|
* |
|
91
|
|
|
* @return PlaceDetailResponse |
|
92
|
|
|
*/ |
|
93
|
|
View Code Duplication |
private function buildResponse(array $data) |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
$response = new PlaceDetailResponse(); |
|
96
|
|
|
$response->setStatus($data['status']); |
|
97
|
|
|
|
|
98
|
|
|
if (isset($data['result'])) { |
|
99
|
|
|
$response->setResult($this->buildResult($data['result'])); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if (isset($data['html_attributions'])) { |
|
103
|
|
|
$response->setHtmlAttributions($data['html_attributions']); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $response; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param mixed[] $data |
|
111
|
|
|
* |
|
112
|
|
|
* @return Place |
|
113
|
|
|
*/ |
|
114
|
|
|
private function buildResult(array $data) |
|
115
|
|
|
{ |
|
116
|
|
|
$result = new Place(); |
|
117
|
|
|
$result->setId($data['id']); |
|
118
|
|
|
$result->setPlaceId($data['place_id']); |
|
119
|
|
|
$result->setName($data['name']); |
|
120
|
|
|
$result->setFormattedAddress($data['formatted_address']); |
|
121
|
|
|
$result->setFormattedPhoneNumber($data['formatted_phone_number']); |
|
122
|
|
|
$result->setInternationalPhoneNumber($data['international_phone_number']); |
|
123
|
|
|
$result->setUrl($data['url']); |
|
124
|
|
|
$result->setIcon($data['icon']); |
|
125
|
|
|
$result->setScope($data['scope']); |
|
126
|
|
|
$result->setRating($data['rating']); |
|
127
|
|
|
$result->setUtcOffset($data['utc_offset']); |
|
128
|
|
|
$result->setVicinity($data['vicinity']); |
|
129
|
|
|
$result->setWebsite($data['website']); |
|
130
|
|
|
$result->setTypes($data['types']); |
|
131
|
|
|
$result->setAddressComponents($this->buildAddressComponents($data['address_components'])); |
|
132
|
|
|
$result->setPhotos($this->buildPhotos($data['photos'])); |
|
133
|
|
|
$result->setGeometry($this->buildGeometry($data['geometry'])); |
|
134
|
|
|
$result->setOpeningHours($this->buildOpeningHours($data['opening_hours'])); |
|
135
|
|
|
$result->setReviews($this->buildReviews($data['reviews'])); |
|
136
|
|
|
|
|
137
|
|
|
if (isset($data['price_level'])) { |
|
138
|
|
|
$result->setPriceLevel($data['price_level']); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
if (isset($data['permanently_closed'])) { |
|
142
|
|
|
$result->setPermanentlyClose($data['permanently_closed']); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
if (isset($data['alt_ids'])) { |
|
146
|
|
|
$result->setAlternatePlaceIds($this->buildAlternativePlaceIds($data['alt_ids'])); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $result; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param mixed[] $data |
|
154
|
|
|
* |
|
155
|
|
|
* @return AddressComponent[] |
|
156
|
|
|
*/ |
|
157
|
|
|
private function buildAddressComponents(array $data) |
|
158
|
|
|
{ |
|
159
|
|
|
$addressComponents = []; |
|
160
|
|
|
|
|
161
|
|
|
foreach ($data as $addressComponent) { |
|
162
|
|
|
$addressComponents[] = $this->buildAddressComponent($addressComponent); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return $addressComponents; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param mixed[] $data |
|
170
|
|
|
* |
|
171
|
|
|
* @return AddressComponent |
|
172
|
|
|
*/ |
|
173
|
|
View Code Duplication |
private function buildAddressComponent(array $data) |
|
|
|
|
|
|
174
|
|
|
{ |
|
175
|
|
|
$address = new AddressComponent(); |
|
176
|
|
|
$address->setLongName($data['long_name']); |
|
177
|
|
|
$address->setShortName($data['short_name']); |
|
178
|
|
|
$address->setTypes($data['types']); |
|
179
|
|
|
|
|
180
|
|
|
return $address; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @param mixed[] $data |
|
185
|
|
|
* |
|
186
|
|
|
* @return Photo[] |
|
187
|
|
|
*/ |
|
188
|
|
|
private function buildPhotos(array $data) |
|
189
|
|
|
{ |
|
190
|
|
|
$photos = []; |
|
191
|
|
|
|
|
192
|
|
|
foreach ($data as $photo) { |
|
193
|
|
|
$photos[] = $this->buildPhoto($photo); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
return $photos; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @param mixed[] $data |
|
201
|
|
|
* |
|
202
|
|
|
* @return Photo |
|
203
|
|
|
*/ |
|
204
|
|
View Code Duplication |
private function buildPhoto(array $data) |
|
|
|
|
|
|
205
|
|
|
{ |
|
206
|
|
|
$photo = new Photo(); |
|
207
|
|
|
$photo->setReference($data['photo_reference']); |
|
208
|
|
|
$photo->setWidth($data['width']); |
|
209
|
|
|
$photo->setHeight($data['height']); |
|
210
|
|
|
$photo->setHtmlAttributions($data['html_attributions']); |
|
211
|
|
|
|
|
212
|
|
|
return $photo; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @param mixed[] $data |
|
217
|
|
|
* |
|
218
|
|
|
* @return AlternatePlaceId[] |
|
219
|
|
|
*/ |
|
220
|
|
|
private function buildAlternativePlaceIds(array $data) |
|
221
|
|
|
{ |
|
222
|
|
|
$alternativePlaceIds = []; |
|
223
|
|
|
|
|
224
|
|
|
foreach ($data as $alternativePlaceId) { |
|
225
|
|
|
$alternativePlaceIds[] = $this->buildAlternativePlaceId($alternativePlaceId); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
return $alternativePlaceIds; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @param mixed[] $data |
|
233
|
|
|
* |
|
234
|
|
|
* @return AlternatePlaceId |
|
235
|
|
|
*/ |
|
236
|
|
View Code Duplication |
private function buildAlternativePlaceId(array $data) |
|
|
|
|
|
|
237
|
|
|
{ |
|
238
|
|
|
$alternativePlaceId = new AlternatePlaceId(); |
|
239
|
|
|
$alternativePlaceId->setPlaceId($data['place_id']); |
|
240
|
|
|
$alternativePlaceId->setScope($data['scope']); |
|
241
|
|
|
|
|
242
|
|
|
return $alternativePlaceId; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* @param mixed[] $data |
|
247
|
|
|
* |
|
248
|
|
|
* @return Review[] |
|
249
|
|
|
*/ |
|
250
|
|
|
private function buildReviews(array $data) |
|
251
|
|
|
{ |
|
252
|
|
|
$reviews = []; |
|
253
|
|
|
|
|
254
|
|
|
foreach ($data as $review) { |
|
255
|
|
|
$reviews[] = $this->buildReview($review); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
return $reviews; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* @param mixed[] $data |
|
263
|
|
|
* |
|
264
|
|
|
* @return Review |
|
265
|
|
|
*/ |
|
266
|
|
|
private function buildReview(array $data) |
|
267
|
|
|
{ |
|
268
|
|
|
$review = new Review(); |
|
269
|
|
|
$review->setAuthorName($data['author_name']); |
|
270
|
|
|
$review->setAuthorUrl($data['author_url']); |
|
271
|
|
|
$review->setText($data['text']); |
|
272
|
|
|
$review->setRating($data['rating']); |
|
273
|
|
|
$review->setTime(new \DateTime('@'.$data['time'])); |
|
274
|
|
|
$review->setLanguage($data['language']); |
|
275
|
|
|
$review->setAspects($this->buildAspectRatings($data['aspects'])); |
|
276
|
|
|
|
|
277
|
|
|
return $review; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* @param mixed[] $data |
|
282
|
|
|
* |
|
283
|
|
|
* @return AspectRating[] |
|
284
|
|
|
*/ |
|
285
|
|
|
private function buildAspectRatings(array $data) |
|
286
|
|
|
{ |
|
287
|
|
|
$aspectRatings = []; |
|
288
|
|
|
|
|
289
|
|
|
foreach ($data as $aspectRating) { |
|
290
|
|
|
$aspectRatings[] = $this->buildAspectRating($aspectRating); |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
return $aspectRatings; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* @param mixed[] $data |
|
298
|
|
|
* |
|
299
|
|
|
* @return AspectRating |
|
300
|
|
|
*/ |
|
301
|
|
|
private function buildAspectRating(array $data) |
|
302
|
|
|
{ |
|
303
|
|
|
if (isset($data['types'])) { |
|
304
|
|
|
$data['type'] = reset($data['types']); |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
$aspectRating = new AspectRating(); |
|
308
|
|
|
$aspectRating->setType($data['type']); |
|
309
|
|
|
$aspectRating->setRating($data['rating']); |
|
310
|
|
|
|
|
311
|
|
|
return $aspectRating; |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
/** |
|
315
|
|
|
* @param mixed[] $data |
|
316
|
|
|
* |
|
317
|
|
|
* @return Geometry |
|
318
|
|
|
*/ |
|
319
|
|
|
private function buildGeometry(array $data) |
|
320
|
|
|
{ |
|
321
|
|
|
$geometry = new Geometry(); |
|
322
|
|
|
$geometry->setLocation($this->buildCoordinate($data['location'])); |
|
323
|
|
|
|
|
324
|
|
|
if (isset($data['viewport'])) { |
|
325
|
|
|
$geometry->setViewport($this->buildBound($data['viewport'])); |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
return $geometry; |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
/** |
|
332
|
|
|
* @param array $data |
|
333
|
|
|
* |
|
334
|
|
|
* @return OpeningHours |
|
335
|
|
|
*/ |
|
336
|
|
|
private function buildOpeningHours(array $data) |
|
337
|
|
|
{ |
|
338
|
|
|
$openingHours = new OpeningHours(); |
|
339
|
|
|
$openingHours->setOpenNow($data['open_now']); |
|
340
|
|
|
$openingHours->setWeekdayTexts($data['weekday_text']); |
|
341
|
|
|
$openingHours->setPeriods($this->buildPeriods($data['periods'])); |
|
342
|
|
|
|
|
343
|
|
|
return $openingHours; |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
/** |
|
347
|
|
|
* @param mixed[] $data |
|
348
|
|
|
* |
|
349
|
|
|
* @return Period[] |
|
350
|
|
|
*/ |
|
351
|
|
|
private function buildPeriods(array $data) |
|
352
|
|
|
{ |
|
353
|
|
|
$periods = []; |
|
354
|
|
|
|
|
355
|
|
|
foreach ($data as $period) { |
|
356
|
|
|
$periods[] = $this->buildPeriod($period); |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
return $periods; |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
/** |
|
363
|
|
|
* @param mixed[] $data |
|
364
|
|
|
* |
|
365
|
|
|
* @return Period |
|
366
|
|
|
*/ |
|
367
|
|
|
private function buildPeriod(array $data) |
|
368
|
|
|
{ |
|
369
|
|
|
$period = new Period(); |
|
370
|
|
|
$period->setOpen($this->buildOpenClosePeriod($data['open'])); |
|
371
|
|
|
$period->setClose($this->buildOpenClosePeriod($data['close'])); |
|
372
|
|
|
|
|
373
|
|
|
return $period; |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
/** |
|
377
|
|
|
* @param mixed[] $data |
|
378
|
|
|
* |
|
379
|
|
|
* @return OpenClosePeriod |
|
380
|
|
|
*/ |
|
381
|
|
|
private function buildOpenClosePeriod(array $data) |
|
382
|
|
|
{ |
|
383
|
|
|
$openClosePeriod = new OpenClosePeriod(); |
|
384
|
|
|
$openClosePeriod->setDay($data['day']); |
|
385
|
|
|
$openClosePeriod->setTime(\DateTime::createFromFormat('!Hi', $data['time'])); |
|
|
|
|
|
|
386
|
|
|
|
|
387
|
|
|
return $openClosePeriod; |
|
388
|
|
|
} |
|
389
|
|
|
|
|
390
|
|
|
/** |
|
391
|
|
|
* @param mixed[] $data |
|
392
|
|
|
* |
|
393
|
|
|
* @return Bound |
|
394
|
|
|
*/ |
|
395
|
|
|
private function buildBound(array $data) |
|
396
|
|
|
{ |
|
397
|
|
|
return new Bound( |
|
398
|
|
|
$this->buildCoordinate($data['southwest']), |
|
399
|
|
|
$this->buildCoordinate($data['northeast']) |
|
400
|
|
|
); |
|
401
|
|
|
} |
|
402
|
|
|
|
|
403
|
|
|
/** |
|
404
|
|
|
* @param mixed[] $data |
|
405
|
|
|
* |
|
406
|
|
|
* @return Coordinate |
|
407
|
|
|
*/ |
|
408
|
|
|
private function buildCoordinate(array $data) |
|
409
|
|
|
{ |
|
410
|
|
|
return new Coordinate($data['lat'], $data['lng']); |
|
411
|
|
|
} |
|
412
|
|
|
} |
|
413
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.