|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Arthem\GoogleApi\Infrastructure\Place\Hydrator; |
|
4
|
|
|
|
|
5
|
|
|
use Arthem\GoogleApi\Domain\Place\Place; |
|
6
|
|
|
use Arthem\GoogleApi\Domain\Place\VO\AddressComponent; |
|
7
|
|
|
use Arthem\GoogleApi\Domain\Place\VO\FormattedAddress; |
|
8
|
|
|
use Arthem\GoogleApi\Domain\Place\VO\FormattedPhoneNumber; |
|
9
|
|
|
use Arthem\GoogleApi\Domain\Place\VO\Icon; |
|
10
|
|
|
use Arthem\GoogleApi\Domain\Place\VO\InternationalPhoneNumber; |
|
11
|
|
|
use Arthem\GoogleApi\Domain\Place\VO\Location; |
|
12
|
|
|
use Arthem\GoogleApi\Domain\Place\VO\OpeningHours; |
|
13
|
|
|
use Arthem\GoogleApi\Domain\Place\VO\Photo; |
|
14
|
|
|
use Arthem\GoogleApi\Domain\Place\VO\PlaceCollection; |
|
15
|
|
|
use Arthem\GoogleApi\Domain\Place\VO\PlaceId; |
|
16
|
|
|
use Arthem\GoogleApi\Domain\Place\VO\PlaceName; |
|
17
|
|
|
use Arthem\GoogleApi\Domain\Place\VO\Type; |
|
18
|
|
|
use Arthem\GoogleApi\Domain\Place\VO\TypeCollection; |
|
19
|
|
|
use Arthem\GoogleApi\Domain\Place\VO\Url; |
|
20
|
|
|
use Arthem\GoogleApi\Domain\Place\VO\Vicinity; |
|
21
|
|
|
use Arthem\GoogleApi\Domain\Place\VO\Website; |
|
22
|
|
|
use Arthem\GooglePlaces\Infrastructure\Place\Exception\ResponseErrorException; |
|
23
|
|
|
|
|
24
|
|
|
class PlaceHydrator |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @param array $placesData |
|
28
|
|
|
* |
|
29
|
|
|
* @return PlaceCollection |
|
30
|
|
|
* |
|
31
|
|
|
* @throws ResponseErrorException When data is malformed |
|
32
|
|
|
*/ |
|
33
|
|
|
public function hydratePlaces(array $placesData) |
|
34
|
|
|
{ |
|
35
|
|
|
$places = new PlaceCollection(); |
|
36
|
|
|
|
|
37
|
|
|
foreach ($placesData as $placeData) { |
|
38
|
|
|
if (!is_array($placeData)) { |
|
39
|
|
|
throw new ResponseErrorException( |
|
40
|
|
|
sprintf( |
|
41
|
|
|
'place data is expected to be array, got %s', |
|
42
|
|
|
gettype( |
|
43
|
|
|
$placeData |
|
44
|
|
|
) |
|
45
|
|
|
) |
|
46
|
|
|
); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$places->addPlace($this->hydratePlace($placeData)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $places; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param array $data |
|
57
|
|
|
* |
|
58
|
|
|
* @return Place |
|
59
|
|
|
*/ |
|
60
|
|
|
public function hydratePlace(array $data) |
|
61
|
|
|
{ |
|
62
|
|
|
$place = new Place(); |
|
63
|
|
|
|
|
64
|
|
|
$this->setPlaceId($place, $data); |
|
65
|
|
|
$this->setName($place, $data); |
|
66
|
|
|
$this->setLocation($place, $data); |
|
67
|
|
|
$this->setTypes($place, $data); |
|
68
|
|
|
$this->setVicinity($place, $data); |
|
69
|
|
|
$this->setFormattedAddress($place, $data); |
|
70
|
|
|
$this->setFormattedPhoneNumber($place, $data); |
|
71
|
|
|
$this->setIcon($place, $data); |
|
72
|
|
|
$this->setInternationalPhoneNumber($place, $data); |
|
73
|
|
|
$this->setUrl($place, $data); |
|
74
|
|
|
$this->setWebsite($place, $data); |
|
75
|
|
|
$this->setAddressComponents($place, $data); |
|
76
|
|
|
$this->setPhotos($place, $data); |
|
77
|
|
|
$this->setOpeningHours($place, $data); |
|
78
|
|
|
|
|
79
|
|
|
return $place; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param Place $place |
|
84
|
|
|
* @param array $data |
|
85
|
|
|
*/ |
|
86
|
|
|
private function setPlaceId(Place $place, array $data) |
|
87
|
|
|
{ |
|
88
|
|
|
if (!empty($data['place_id'])) { |
|
89
|
|
|
$place->setId(new PlaceId($data['place_id'])); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param Place $place |
|
95
|
|
|
* @param array $data |
|
96
|
|
|
*/ |
|
97
|
|
|
private function setName(Place $place, array $data) |
|
98
|
|
|
{ |
|
99
|
|
|
if (!empty($data['name'])) { |
|
100
|
|
|
$place->setName(new PlaceName($data['name'])); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param Place $place |
|
106
|
|
|
* @param array $data |
|
107
|
|
|
*/ |
|
108
|
|
|
private function setLocation(Place $place, array $data) |
|
109
|
|
|
{ |
|
110
|
|
|
if (!empty($data['geometry']['location'])) { |
|
111
|
|
|
$location = $data['geometry']['location']; |
|
112
|
|
|
$place->setLocation(new Location($location['lat'], $location['lng'])); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param Place $place |
|
118
|
|
|
* @param array $data |
|
119
|
|
|
*/ |
|
120
|
|
|
private function setTypes(Place $place, array $data) |
|
121
|
|
|
{ |
|
122
|
|
|
if (!empty($data['types'])) { |
|
123
|
|
|
$types = []; |
|
124
|
|
|
foreach ($data['types'] as $typeData) { |
|
125
|
|
|
$types[] = new Type($typeData); |
|
126
|
|
|
} |
|
127
|
|
|
$place->setTypes(new TypeCollection($types)); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param Place $place |
|
133
|
|
|
* @param array $data |
|
134
|
|
|
*/ |
|
135
|
|
|
private function setAddressComponents(Place $place, array $data) |
|
136
|
|
|
{ |
|
137
|
|
|
if (empty($data['address_components'])) { |
|
138
|
|
|
return; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$components = $place->getAddressComponents(); |
|
142
|
|
|
foreach ($data['address_components'] as $componentData) { |
|
143
|
|
|
$components->addComponent( |
|
144
|
|
|
new AddressComponent( |
|
145
|
|
|
$componentData['long_name'], |
|
146
|
|
|
$componentData['short_name'], |
|
147
|
|
|
$componentData['types'] |
|
148
|
|
|
) |
|
149
|
|
|
); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param Place $place |
|
155
|
|
|
* @param array $data |
|
156
|
|
|
*/ |
|
157
|
|
|
private function setPhotos(Place $place, array $data) |
|
158
|
|
|
{ |
|
159
|
|
|
if (empty($data['photos'])) { |
|
160
|
|
|
return; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
$photos = $place->getPhotos(); |
|
164
|
|
|
foreach ($data['photos'] as $photoData) { |
|
165
|
|
|
$photos->addPhoto( |
|
166
|
|
|
new Photo( |
|
167
|
|
|
$photoData['photo_reference'], |
|
168
|
|
|
$photoData['width'], |
|
169
|
|
|
$photoData['height'] |
|
170
|
|
|
) |
|
171
|
|
|
); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @param Place $place |
|
177
|
|
|
* @param array $data |
|
178
|
|
|
*/ |
|
179
|
|
|
private function setOpeningHours(Place $place, array $data) |
|
180
|
|
|
{ |
|
181
|
|
|
if (empty($data['opening_hours'])) { |
|
182
|
|
|
return; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
$weekdayText = $data['opening_hours']['weekday_text']; |
|
186
|
|
|
|
|
187
|
|
|
$place->setOpeningHours(new OpeningHours($weekdayText)); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @param Place $place |
|
192
|
|
|
* @param array $data |
|
193
|
|
|
*/ |
|
194
|
|
|
private function setVicinity(Place $place, array $data) |
|
195
|
|
|
{ |
|
196
|
|
|
if (!empty($data['vicinity'])) { |
|
197
|
|
|
$place->setVicinity(new Vicinity($data['vicinity'])); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @param Place $place |
|
203
|
|
|
* @param array $data |
|
204
|
|
|
*/ |
|
205
|
|
|
private function setFormattedAddress(Place $place, array $data) |
|
206
|
|
|
{ |
|
207
|
|
|
if (!empty($data['formatted_address'])) { |
|
208
|
|
|
$place->setFormattedAddress(new FormattedAddress($data['formatted_address'])); |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @param Place $place |
|
214
|
|
|
* @param array $data |
|
215
|
|
|
*/ |
|
216
|
|
|
private function setFormattedPhoneNumber(Place $place, array $data) |
|
217
|
|
|
{ |
|
218
|
|
|
if (!empty($data['formatted_phone_number'])) { |
|
219
|
|
|
$place->setFormattedPhoneNumber(new FormattedPhoneNumber($data['formatted_phone_number'])); |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @param Place $place |
|
225
|
|
|
* @param array $data |
|
226
|
|
|
*/ |
|
227
|
|
|
private function setIcon(Place $place, array $data) |
|
228
|
|
|
{ |
|
229
|
|
|
if (!empty($data['icon'])) { |
|
230
|
|
|
$place->setIcon(new Icon($data['icon'])); |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* @param Place $place |
|
236
|
|
|
* @param array $data |
|
237
|
|
|
*/ |
|
238
|
|
|
private function setInternationalPhoneNumber(Place $place, array $data) |
|
239
|
|
|
{ |
|
240
|
|
|
if (!empty($data['international_phone_number'])) { |
|
241
|
|
|
$place->setInternationalPhoneNumber(new InternationalPhoneNumber($data['international_phone_number'])); |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* @param Place $place |
|
247
|
|
|
* @param array $data |
|
248
|
|
|
*/ |
|
249
|
|
|
private function setUrl(Place $place, array $data) |
|
250
|
|
|
{ |
|
251
|
|
|
if (!empty($data['url'])) { |
|
252
|
|
|
$place->setUrl(new Url($data['url'])); |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* @param Place $place |
|
258
|
|
|
* @param array $data |
|
259
|
|
|
*/ |
|
260
|
|
|
private function setWebsite(Place $place, array $data) |
|
261
|
|
|
{ |
|
262
|
|
|
if (!empty($data['website'])) { |
|
263
|
|
|
$place->setWebsite(new Website($data['website'])); |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
|
|
|