These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* |
||
6 | * This file is part of the Geocoder package. |
||
7 | * For the full copyright and license information, please view the LICENSE |
||
8 | * file that was distributed with this source code. |
||
9 | * |
||
10 | * @license MIT License |
||
11 | */ |
||
12 | |||
13 | namespace Geocoder\Provider\Nominatim; |
||
14 | |||
15 | use Geocoder\Collection; |
||
16 | use Geocoder\Exception\InvalidServerResponse; |
||
17 | use Geocoder\Exception\UnsupportedOperation; |
||
18 | use Geocoder\Location; |
||
19 | use Geocoder\Model\AddressBuilder; |
||
20 | use Geocoder\Model\AddressCollection; |
||
21 | use Geocoder\Query\GeocodeQuery; |
||
22 | use Geocoder\Query\ReverseQuery; |
||
23 | use Geocoder\Http\Provider\AbstractHttpProvider; |
||
24 | use Geocoder\Provider\Provider; |
||
25 | use Http\Client\HttpClient; |
||
26 | |||
27 | /** |
||
28 | * @author Niklas Närhinen <[email protected]> |
||
29 | */ |
||
30 | final class Nominatim extends AbstractHttpProvider implements Provider |
||
31 | { |
||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | private $rootUrl; |
||
36 | |||
37 | /** |
||
38 | * @param HttpClient $client |
||
39 | * @param string|null $locale |
||
0 ignored issues
–
show
|
|||
40 | * |
||
41 | * @return Nominatim |
||
42 | */ |
||
43 | 27 | public static function withOpenStreetMapServer(HttpClient $client) |
|
44 | { |
||
45 | 27 | return new self($client, 'https://nominatim.openstreetmap.org'); |
|
46 | } |
||
47 | |||
48 | /** |
||
49 | * @param HttpClient $client an HTTP adapter |
||
50 | * @param string $rootUrl Root URL of the nominatim server |
||
51 | */ |
||
52 | 27 | public function __construct(HttpClient $client, $rootUrl) |
|
53 | { |
||
54 | 27 | parent::__construct($client); |
|
55 | |||
56 | 27 | $this->rootUrl = rtrim($rootUrl, '/'); |
|
57 | 27 | } |
|
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | 18 | public function geocodeQuery(GeocodeQuery $query): Collection |
|
63 | { |
||
64 | 18 | $address = $query->getText(); |
|
65 | // This API does not support IPv6 |
||
66 | 18 | if (filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
|
67 | 2 | throw new UnsupportedOperation('The Nominatim provider does not support IPv6 addresses.'); |
|
68 | } |
||
69 | |||
70 | 16 | if ('127.0.0.1' === $address) { |
|
71 | 1 | return new AddressCollection([$this->getLocationForLocalhost()]); |
|
72 | } |
||
73 | |||
74 | 15 | $url = sprintf($this->getGeocodeEndpointUrl(), urlencode($address), $query->getLimit()); |
|
75 | 15 | $content = $this->executeQuery($url, $query->getLocale()); |
|
76 | |||
77 | 5 | $doc = new \DOMDocument(); |
|
78 | 5 | if (!@$doc->loadXML($content) || null === $doc->getElementsByTagName('searchresults')->item(0)) { |
|
79 | 2 | throw InvalidServerResponse::create($url); |
|
80 | } |
||
81 | |||
82 | 3 | $searchResult = $doc->getElementsByTagName('searchresults')->item(0); |
|
83 | 3 | $places = $searchResult->getElementsByTagName('place'); |
|
84 | |||
85 | 3 | if (null === $places || 0 === $places->length) { |
|
86 | 1 | return new AddressCollection([]); |
|
87 | } |
||
88 | |||
89 | 2 | $results = []; |
|
90 | 2 | foreach ($places as $place) { |
|
91 | 2 | $results[] = $this->xmlResultToArray($place, $place); |
|
92 | } |
||
93 | |||
94 | 2 | return new AddressCollection($results); |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * {@inheritdoc} |
||
99 | */ |
||
100 | 9 | public function reverseQuery(ReverseQuery $query): Collection |
|
101 | { |
||
102 | 9 | $coordinates = $query->getCoordinates(); |
|
103 | 9 | $longitude = $coordinates->getLongitude(); |
|
104 | 9 | $latitude = $coordinates->getLatitude(); |
|
105 | 9 | $url = sprintf($this->getReverseEndpointUrl(), $latitude, $longitude, $query->getData('zoom', 18)); |
|
106 | 9 | $content = $this->executeQuery($url, $query->getLocale()); |
|
107 | |||
108 | 4 | $doc = new \DOMDocument(); |
|
109 | 4 | if (!@$doc->loadXML($content) || $doc->getElementsByTagName('error')->length > 0) { |
|
110 | 2 | return new AddressCollection([]); |
|
111 | } |
||
112 | |||
113 | 2 | $searchResult = $doc->getElementsByTagName('reversegeocode')->item(0); |
|
114 | 2 | $addressParts = $searchResult->getElementsByTagName('addressparts')->item(0); |
|
115 | 2 | $result = $searchResult->getElementsByTagName('result')->item(0); |
|
116 | |||
117 | 2 | return new AddressCollection([$this->xmlResultToArray($result, $addressParts)]); |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * @param \DOMElement $resultNode |
||
122 | * @param \DOMElement $addressNode |
||
123 | * |
||
124 | * @return Location |
||
125 | */ |
||
126 | 4 | private function xmlResultToArray(\DOMElement $resultNode, \DOMElement $addressNode): Location |
|
127 | { |
||
128 | 4 | $builder = new AddressBuilder($this->getName()); |
|
129 | |||
130 | 4 | foreach (['state', 'county'] as $i => $tagName) { |
|
131 | 4 | if (null !== ($adminLevel = $this->getNodeValue($addressNode->getElementsByTagName($tagName)))) { |
|
132 | 4 | $builder->addAdminLevel($i + 1, $adminLevel, ''); |
|
133 | } |
||
134 | } |
||
135 | |||
136 | // get the first postal-code when there are many |
||
137 | 4 | $postalCode = $this->getNodeValue($addressNode->getElementsByTagName('postcode')); |
|
138 | 4 | if (!empty($postalCode)) { |
|
139 | 3 | $postalCode = current(explode(';', $postalCode)); |
|
140 | } |
||
141 | 4 | $builder->setPostalCode($postalCode); |
|
142 | 4 | $builder->setStreetName($this->getNodeValue($addressNode->getElementsByTagName('road')) ?: $this->getNodeValue($addressNode->getElementsByTagName('pedestrian'))); |
|
143 | 4 | $builder->setStreetNumber($this->getNodeValue($addressNode->getElementsByTagName('house_number'))); |
|
144 | 4 | $builder->setLocality($this->getNodeValue($addressNode->getElementsByTagName('city'))); |
|
145 | 4 | $builder->setSubLocality($this->getNodeValue($addressNode->getElementsByTagName('suburb'))); |
|
146 | 4 | $builder->setCountry($this->getNodeValue($addressNode->getElementsByTagName('country'))); |
|
147 | |||
148 | 4 | $countryCode = $this->getNodeValue($addressNode->getElementsByTagName('country_code')); |
|
149 | 4 | if (!empty($countryCode)) { |
|
150 | 4 | $countryCode = strtoupper($countryCode); |
|
151 | } |
||
152 | 4 | $builder->setCountryCode($countryCode); |
|
153 | 4 | $builder->setCoordinates($resultNode->getAttribute('lat'), $resultNode->getAttribute('lon')); |
|
154 | |||
155 | 4 | $boundsAttr = $resultNode->getAttribute('boundingbox'); |
|
156 | 4 | if ($boundsAttr) { |
|
157 | 4 | $bounds = []; |
|
158 | 4 | list($bounds['south'], $bounds['north'], $bounds['west'], $bounds['east']) = explode(',', $boundsAttr); |
|
159 | 4 | $builder->setBounds($bounds['south'], $bounds['north'], $bounds['west'], $bounds['east']); |
|
160 | } |
||
161 | |||
162 | 4 | return $builder->build(); |
|
163 | } |
||
164 | |||
165 | /** |
||
166 | * {@inheritdoc} |
||
167 | */ |
||
168 | 5 | public function getName(): string |
|
169 | { |
||
170 | 5 | return 'nominatim'; |
|
171 | } |
||
172 | |||
173 | /** |
||
174 | * @param string $url |
||
175 | * @param string|null $locale |
||
176 | * |
||
177 | * @return string |
||
178 | */ |
||
179 | 24 | private function executeQuery(string $url, string $locale = null): string |
|
180 | { |
||
181 | 24 | if (null !== $locale) { |
|
182 | 4 | $url = sprintf('%s&accept-language=%s', $url, $locale); |
|
183 | } |
||
184 | |||
185 | 24 | return $this->getUrlContents($url); |
|
186 | } |
||
187 | |||
188 | 15 | private function getGeocodeEndpointUrl(): string |
|
189 | { |
||
190 | 15 | return $this->rootUrl.'/search?q=%s&format=xml&addressdetails=1&limit=%d'; |
|
191 | } |
||
192 | |||
193 | 9 | private function getReverseEndpointUrl(): string |
|
194 | { |
||
195 | 9 | return $this->rootUrl.'/reverse?format=xml&lat=%F&lon=%F&addressdetails=1&zoom=%d'; |
|
196 | } |
||
197 | |||
198 | 4 | private function getNodeValue(\DOMNodeList $element) |
|
199 | { |
||
200 | 4 | return $element->length ? $element->item(0)->nodeValue : null; |
|
201 | } |
||
202 | } |
||
203 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.