|
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\Geocoder; |
|
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\Geocoder\Request\GeocoderRequestInterface; |
|
22
|
|
|
use Ivory\GoogleMap\Service\Geocoder\Response\GeocoderResponse; |
|
23
|
|
|
use Ivory\GoogleMap\Service\Geocoder\Response\GeocoderResult; |
|
24
|
|
|
use Ivory\GoogleMap\Service\Utility\Parser; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @author GeLo <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
class GeocoderService extends AbstractParsableService |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @param HttpClient $client |
|
33
|
|
|
* @param MessageFactory $messageFactory |
|
34
|
|
|
* @param Parser|null $parser |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(HttpClient $client, MessageFactory $messageFactory, Parser $parser = null) |
|
37
|
|
|
{ |
|
38
|
|
|
parent::__construct($client, $messageFactory, 'http://maps.googleapis.com/maps/api/geocode', $parser); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param GeocoderRequestInterface $request |
|
43
|
|
|
* |
|
44
|
|
|
* @return GeocoderResponse |
|
45
|
|
|
*/ |
|
46
|
|
View Code Duplication |
public function geocode(GeocoderRequestInterface $request) |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
|
|
$httpRequest = $this->createRequest($request); |
|
49
|
|
|
$httpResponse = $this->getClient()->sendRequest($httpRequest); |
|
50
|
|
|
|
|
51
|
|
|
$data = $this->parse((string) $httpResponse->getBody(), [ |
|
52
|
|
|
'pluralization_rules' => [ |
|
53
|
|
|
'address_component' => 'address_components', |
|
54
|
|
|
'type' => 'types', |
|
55
|
|
|
'result' => 'results', |
|
56
|
|
|
], |
|
57
|
|
|
]); |
|
58
|
|
|
|
|
59
|
|
|
$response = $this->buildResponse($data); |
|
60
|
|
|
$response->setRequest($request); |
|
61
|
|
|
|
|
62
|
|
|
return $response; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param mixed[] $data |
|
67
|
|
|
* |
|
68
|
|
|
* @return GeocoderResponse |
|
69
|
|
|
*/ |
|
70
|
|
View Code Duplication |
private function buildResponse(array $data) |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
|
|
$response = new GeocoderResponse(); |
|
73
|
|
|
$response->setStatus($data['status']); |
|
74
|
|
|
$response->setResults($this->buildResults($data['results'])); |
|
75
|
|
|
|
|
76
|
|
|
return $response; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param mixed[] $data |
|
81
|
|
|
* |
|
82
|
|
|
* @return GeocoderResult[] |
|
83
|
|
|
*/ |
|
84
|
|
|
private function buildResults(array $data) |
|
85
|
|
|
{ |
|
86
|
|
|
$results = []; |
|
87
|
|
|
|
|
88
|
|
|
foreach ($data as $response) { |
|
89
|
|
|
$results[] = $this->buildResult($response); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $results; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param mixed[] $data |
|
97
|
|
|
* |
|
98
|
|
|
* @return GeocoderResult |
|
99
|
|
|
*/ |
|
100
|
|
|
private function buildResult(array $data) |
|
101
|
|
|
{ |
|
102
|
|
|
$result = new GeocoderResult(); |
|
103
|
|
|
$result->setAddressComponents($this->buildAddressComponents($data['address_components'])); |
|
104
|
|
|
$result->setGeometry($this->buildGeometry($data['geometry'])); |
|
105
|
|
|
$result->setPlaceId($data['place_id']); |
|
106
|
|
|
$result->setFormattedAddress($data['formatted_address']); |
|
107
|
|
|
|
|
108
|
|
|
if (isset($data['types'])) { |
|
109
|
|
|
$result->setTypes($data['types']); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if (isset($data['partial_match'])) { |
|
113
|
|
|
$result->setPartialMatch($data['partial_match']); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $result; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param mixed[] $data |
|
121
|
|
|
* |
|
122
|
|
|
* @return AddressComponent[] |
|
123
|
|
|
*/ |
|
124
|
|
|
private function buildAddressComponents(array $data) |
|
125
|
|
|
{ |
|
126
|
|
|
$addressComponents = []; |
|
127
|
|
|
|
|
128
|
|
|
foreach ($data as $addressComponent) { |
|
129
|
|
|
$addressComponents[] = $this->buildAddressComponent($addressComponent); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return $addressComponents; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param mixed[] $data |
|
137
|
|
|
* |
|
138
|
|
|
* @return AddressComponent |
|
139
|
|
|
*/ |
|
140
|
|
View Code Duplication |
private function buildAddressComponent(array $data) |
|
|
|
|
|
|
141
|
|
|
{ |
|
142
|
|
|
$address = new AddressComponent(); |
|
143
|
|
|
$address->setLongName($data['long_name']); |
|
144
|
|
|
$address->setShortName($data['short_name']); |
|
145
|
|
|
$address->setTypes($data['types']); |
|
146
|
|
|
|
|
147
|
|
|
return $address; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @param mixed[] $data |
|
152
|
|
|
* |
|
153
|
|
|
* @return Geometry |
|
154
|
|
|
*/ |
|
155
|
|
|
private function buildGeometry(array $data) |
|
156
|
|
|
{ |
|
157
|
|
|
$geometry = new Geometry(); |
|
158
|
|
|
$geometry->setLocation($this->buildCoordinate($data['location'])); |
|
159
|
|
|
$geometry->setViewport($this->buildBound($data['viewport'])); |
|
160
|
|
|
$geometry->setLocationType($data['location_type']); |
|
161
|
|
|
|
|
162
|
|
|
if (isset($data['bounds'])) { |
|
163
|
|
|
$geometry->setBound($this->buildBound($data['bounds'])); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
return $geometry; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @param mixed[] $data |
|
171
|
|
|
* |
|
172
|
|
|
* @return Bound |
|
173
|
|
|
*/ |
|
174
|
|
|
private function buildBound(array $data) |
|
175
|
|
|
{ |
|
176
|
|
|
return new Bound( |
|
177
|
|
|
$this->buildCoordinate($data['southwest']), |
|
178
|
|
|
$this->buildCoordinate($data['northeast']) |
|
179
|
|
|
); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @param mixed[] $data |
|
184
|
|
|
* |
|
185
|
|
|
* @return Coordinate |
|
186
|
|
|
*/ |
|
187
|
|
|
private function buildCoordinate(array $data) |
|
188
|
|
|
{ |
|
189
|
|
|
return new Coordinate($data['lat'], $data['lng']); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
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.