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