|
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\Search; |
|
13
|
|
|
|
|
14
|
|
|
use Ivory\GoogleMap\Base\Coordinate; |
|
15
|
|
|
use Ivory\GoogleMap\Service\Base\Geometry; |
|
16
|
|
|
use Ivory\GoogleMap\Service\Place\AbstractPlaceParsableService; |
|
17
|
|
|
use Ivory\GoogleMap\Service\Place\Base\AlternatePlaceId; |
|
18
|
|
|
use Ivory\GoogleMap\Service\Place\Base\OpeningHours; |
|
19
|
|
|
use Ivory\GoogleMap\Service\Place\Base\Photo; |
|
20
|
|
|
use Ivory\GoogleMap\Service\Place\Base\Place; |
|
21
|
|
|
use Ivory\GoogleMap\Service\Place\Search\Request\PlaceSearchRequestInterface; |
|
22
|
|
|
use Ivory\GoogleMap\Service\Place\Search\Response\PlaceSearchResponse; |
|
23
|
|
|
use Ivory\GoogleMap\Service\Place\Search\Response\PlaceSearchResponseIterator; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @author GeLo <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
class PlaceSearchService extends AbstractPlaceParsableService |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
|
|
public function setHttps($https) |
|
34
|
|
|
{ |
|
35
|
|
|
if (!$https) { |
|
36
|
|
|
throw new \InvalidArgumentException('The http scheme is not supported.'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
parent::setHttps($https); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param PlaceSearchRequestInterface $request |
|
44
|
|
|
* |
|
45
|
|
|
* @return PlaceSearchResponseIterator |
|
46
|
|
|
*/ |
|
47
|
|
View Code Duplication |
public function process(PlaceSearchRequestInterface $request) |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
$httpRequest = $this->createRequest($request); |
|
50
|
|
|
$httpResponse = $this->getClient()->sendRequest($httpRequest); |
|
51
|
|
|
|
|
52
|
|
|
$data = $this->parse((string) $httpResponse->getBody(), [ |
|
53
|
|
|
'pluralization_rules' => [ |
|
54
|
|
|
'html_attribution' => 'html_attributions', |
|
55
|
|
|
'photo' => 'photos', |
|
56
|
|
|
'result' => 'results', |
|
57
|
|
|
'type' => 'types', |
|
58
|
|
|
], |
|
59
|
|
|
]); |
|
60
|
|
|
|
|
61
|
|
|
$response = $this->buildResponse($data); |
|
62
|
|
|
$response->setRequest($request); |
|
63
|
|
|
|
|
64
|
|
|
return new PlaceSearchResponseIterator($this, $response); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param mixed[] $data |
|
69
|
|
|
* |
|
70
|
|
|
* @return PlaceSearchResponse |
|
71
|
|
|
*/ |
|
72
|
|
View Code Duplication |
private function buildResponse(array $data) |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
$response = new PlaceSearchResponse(); |
|
75
|
|
|
$response->setStatus($data['status']); |
|
76
|
|
|
$response->setResults($this->buildResults($data['results'])); |
|
77
|
|
|
|
|
78
|
|
|
if (isset($data['html_attributions'])) { |
|
79
|
|
|
$response->setHtmlAttributions($data['html_attributions']); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if (isset($data['next_page_token'])) { |
|
83
|
|
|
$response->setNextPageToken($data['next_page_token']); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $response; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param mixed[] $data |
|
91
|
|
|
* |
|
92
|
|
|
* @return Place[] |
|
93
|
|
|
*/ |
|
94
|
|
|
private function buildResults(array $data) |
|
95
|
|
|
{ |
|
96
|
|
|
$results = []; |
|
97
|
|
|
|
|
98
|
|
|
foreach ($data as $result) { |
|
99
|
|
|
$results[] = $this->buildResult($result); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $results; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param mixed[] $data |
|
107
|
|
|
* |
|
108
|
|
|
* @return Place |
|
109
|
|
|
*/ |
|
110
|
|
|
private function buildResult(array $data) |
|
111
|
|
|
{ |
|
112
|
|
|
$result = new Place(); |
|
113
|
|
|
$result->setPlaceId($data['place_id']); |
|
114
|
|
|
$result->setGeometry($this->buildGeometry($data['geometry'])); |
|
115
|
|
|
|
|
116
|
|
|
if (isset($data['name'])) { |
|
117
|
|
|
$result->setName($data['name']); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
if (isset($data['formatted_address'])) { |
|
121
|
|
|
$result->setFormattedAddress($data['formatted_address']); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
if (isset($data['icon'])) { |
|
125
|
|
|
$result->setIcon($data['icon']); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
if (isset($data['scope'])) { |
|
129
|
|
|
$result->setScope($data['scope']); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if (isset($data['price_level'])) { |
|
133
|
|
|
$result->setPriceLevel($data['price_level']); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
if (isset($data['rating'])) { |
|
137
|
|
|
$result->setRating($data['rating']); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
if (isset($data['vicinity'])) { |
|
141
|
|
|
$result->setVicinity($data['vicinity']); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
if (isset($data['permanently_closed'])) { |
|
145
|
|
|
$result->setPermanentlyClose($data['permanently_closed']); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
if (isset($data['types'])) { |
|
149
|
|
|
$result->setTypes($data['types']); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
if (isset($data['photos'])) { |
|
153
|
|
|
$result->setPhotos($this->buildPhotos($data['photos'])); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
if (isset($data['alt_ids'])) { |
|
157
|
|
|
$result->setAlternatePlaceIds($this->buildAlternativePlaceIds($data['alt_ids'])); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
if (isset($data['opening_hours'])) { |
|
161
|
|
|
$result->setOpeningHours($this->buildOpeningHours($data['opening_hours'])); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
return $result; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @param mixed[] $data |
|
169
|
|
|
* |
|
170
|
|
|
* @return Photo[] |
|
171
|
|
|
*/ |
|
172
|
|
|
private function buildPhotos(array $data) |
|
173
|
|
|
{ |
|
174
|
|
|
$photos = []; |
|
175
|
|
|
|
|
176
|
|
|
foreach ($data as $photo) { |
|
177
|
|
|
$photos[] = $this->buildPhoto($photo); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return $photos; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @param mixed[] $data |
|
185
|
|
|
* |
|
186
|
|
|
* @return Photo |
|
187
|
|
|
*/ |
|
188
|
|
View Code Duplication |
private function buildPhoto(array $data) |
|
|
|
|
|
|
189
|
|
|
{ |
|
190
|
|
|
$photo = new Photo(); |
|
191
|
|
|
$photo->setReference($data['photo_reference']); |
|
192
|
|
|
$photo->setWidth($data['width']); |
|
193
|
|
|
$photo->setHeight($data['height']); |
|
194
|
|
|
$photo->setHtmlAttributions($data['html_attributions']); |
|
195
|
|
|
|
|
196
|
|
|
return $photo; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @param mixed[] $data |
|
201
|
|
|
* |
|
202
|
|
|
* @return AlternatePlaceId[] |
|
203
|
|
|
*/ |
|
204
|
|
|
private function buildAlternativePlaceIds(array $data) |
|
205
|
|
|
{ |
|
206
|
|
|
$alternativePlaceIds = []; |
|
207
|
|
|
|
|
208
|
|
|
foreach ($data as $alternativePlaceId) { |
|
209
|
|
|
$alternativePlaceIds[] = $this->buildAlternativePlaceId($alternativePlaceId); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
return $alternativePlaceIds; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @param mixed[] $data |
|
217
|
|
|
* |
|
218
|
|
|
* @return AlternatePlaceId |
|
219
|
|
|
*/ |
|
220
|
|
View Code Duplication |
private function buildAlternativePlaceId(array $data) |
|
|
|
|
|
|
221
|
|
|
{ |
|
222
|
|
|
$alternativePlaceId = new AlternatePlaceId(); |
|
223
|
|
|
$alternativePlaceId->setPlaceId($data['place_id']); |
|
224
|
|
|
$alternativePlaceId->setScope($data['scope']); |
|
225
|
|
|
|
|
226
|
|
|
return $alternativePlaceId; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* @param mixed[] $data |
|
231
|
|
|
* |
|
232
|
|
|
* @return Geometry |
|
233
|
|
|
*/ |
|
234
|
|
|
private function buildGeometry(array $data) |
|
235
|
|
|
{ |
|
236
|
|
|
$geometry = new Geometry(); |
|
237
|
|
|
$geometry->setLocation($this->buildCoordinate($data['location'])); |
|
238
|
|
|
|
|
239
|
|
|
return $geometry; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* @param array $data |
|
244
|
|
|
* |
|
245
|
|
|
* @return OpeningHours |
|
246
|
|
|
*/ |
|
247
|
|
|
private function buildOpeningHours(array $data) |
|
248
|
|
|
{ |
|
249
|
|
|
$openingHours = new OpeningHours(); |
|
250
|
|
|
$openingHours->setOpenNow($data['open_now']); |
|
251
|
|
|
|
|
252
|
|
|
return $openingHours; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* @param mixed[] $data |
|
257
|
|
|
* |
|
258
|
|
|
* @return Coordinate |
|
259
|
|
|
*/ |
|
260
|
|
|
private function buildCoordinate(array $data) |
|
261
|
|
|
{ |
|
262
|
|
|
return new Coordinate($data['lat'], $data['lng']); |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
|
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.