|
| 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\PickPoint; |
| 14 | + |
| 15 | +use Geocoder\Collection; |
| 16 | +use Geocoder\Exception\InvalidServerResponse; |
| 17 | +use Geocoder\Exception\InvalidCredentials; |
| 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 Vladimir Kalinkin <[email protected]> |
| 29 | + */ |
| 30 | +final class PickPoint extends AbstractHttpProvider implements Provider |
| 31 | +{ |
| 32 | + /** |
| 33 | + * @var string |
| 34 | + */ |
| 35 | + const BASE_API_URL = 'https://api.pickpoint.io/v1'; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var string |
| 39 | + */ |
| 40 | + private $apiKey; |
| 41 | + |
| 42 | + /** |
| 43 | + * @param HttpClient $client an HTTP adapter |
| 44 | + * @param string $apiKey an API key |
| 45 | + */ |
| 46 | + public function __construct(HttpClient $client, string $apiKey) |
| 47 | + { |
| 48 | + if (empty($apiKey)) { |
| 49 | + throw new InvalidCredentials('No API key provided.'); |
| 50 | + } |
| 51 | + |
| 52 | + $this->apiKey = $apiKey; |
| 53 | + parent::__construct($client); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * {@inheritdoc} |
| 58 | + */ |
| 59 | + public function geocodeQuery(GeocodeQuery $query): Collection |
| 60 | + { |
| 61 | + $address = $query->getText(); |
| 62 | + |
| 63 | + $url = sprintf($this->getGeocodeEndpointUrl(), urlencode($address), $query->getLimit()); |
| 64 | + $content = $this->executeQuery($url, $query->getLocale()); |
| 65 | + |
| 66 | + $doc = new \DOMDocument(); |
| 67 | + if (!@$doc->loadXML($content) || null === $doc->getElementsByTagName('searchresults')->item(0)) { |
| 68 | + throw InvalidServerResponse::create($url); |
| 69 | + } |
| 70 | + |
| 71 | + $searchResult = $doc->getElementsByTagName('searchresults')->item(0); |
| 72 | + $places = $searchResult->getElementsByTagName('place'); |
| 73 | + |
| 74 | + if (null === $places || 0 === $places->length) { |
| 75 | + return new AddressCollection([]); |
| 76 | + } |
| 77 | + |
| 78 | + $results = []; |
| 79 | + foreach ($places as $place) { |
| 80 | + $results[] = $this->xmlResultToArray($place, $place); |
| 81 | + } |
| 82 | + |
| 83 | + return new AddressCollection($results); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * {@inheritdoc} |
| 88 | + */ |
| 89 | + public function reverseQuery(ReverseQuery $query): Collection |
| 90 | + { |
| 91 | + $coordinates = $query->getCoordinates(); |
| 92 | + $longitude = $coordinates->getLongitude(); |
| 93 | + $latitude = $coordinates->getLatitude(); |
| 94 | + $url = sprintf($this->getReverseEndpointUrl(), $latitude, $longitude, $query->getData('zoom', 18)); |
| 95 | + $content = $this->executeQuery($url, $query->getLocale()); |
| 96 | + |
| 97 | + $doc = new \DOMDocument(); |
| 98 | + if (!@$doc->loadXML($content) || $doc->getElementsByTagName('error')->length > 0) { |
| 99 | + return new AddressCollection([]); |
| 100 | + } |
| 101 | + |
| 102 | + $searchResult = $doc->getElementsByTagName('reversegeocode')->item(0); |
| 103 | + $addressParts = $searchResult->getElementsByTagName('addressparts')->item(0); |
| 104 | + $result = $searchResult->getElementsByTagName('result')->item(0); |
| 105 | + |
| 106 | + return new AddressCollection([$this->xmlResultToArray($result, $addressParts)]); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @param \DOMElement $resultNode |
| 111 | + * @param \DOMElement $addressNode |
| 112 | + * |
| 113 | + * @return Location |
| 114 | + */ |
| 115 | + private function xmlResultToArray(\DOMElement $resultNode, \DOMElement $addressNode): Location |
| 116 | + { |
| 117 | + $builder = new AddressBuilder($this->getName()); |
| 118 | + |
| 119 | + foreach (['state', 'county'] as $i => $tagName) { |
| 120 | + if (null !== ($adminLevel = $this->getNodeValue($addressNode->getElementsByTagName($tagName)))) { |
| 121 | + $builder->addAdminLevel($i + 1, $adminLevel, ''); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + // get the first postal-code when there are many |
| 126 | + $postalCode = $this->getNodeValue($addressNode->getElementsByTagName('postcode')); |
| 127 | + if (!empty($postalCode)) { |
| 128 | + $postalCode = current(explode(';', $postalCode)); |
| 129 | + } |
| 130 | + $builder->setPostalCode($postalCode); |
| 131 | + $builder->setStreetName($this->getNodeValue($addressNode->getElementsByTagName('road')) ?: $this->getNodeValue($addressNode->getElementsByTagName('pedestrian'))); |
| 132 | + $builder->setStreetNumber($this->getNodeValue($addressNode->getElementsByTagName('house_number'))); |
| 133 | + $builder->setLocality($this->getNodeValue($addressNode->getElementsByTagName('city'))); |
| 134 | + $builder->setSubLocality($this->getNodeValue($addressNode->getElementsByTagName('suburb'))); |
| 135 | + $builder->setCountry($this->getNodeValue($addressNode->getElementsByTagName('country'))); |
| 136 | + $builder->setCountryCode(strtoupper($this->getNodeValue($addressNode->getElementsByTagName('country_code')))); |
| 137 | + $builder->setCoordinates($resultNode->getAttribute('lat'), $resultNode->getAttribute('lon')); |
| 138 | + |
| 139 | + $boundsAttr = $resultNode->getAttribute('boundingbox'); |
| 140 | + if ($boundsAttr) { |
| 141 | + $bounds = []; |
| 142 | + list($bounds['south'], $bounds['north'], $bounds['west'], $bounds['east']) = explode(',', $boundsAttr); |
| 143 | + $builder->setBounds($bounds['south'], $bounds['north'], $bounds['west'], $bounds['east']); |
| 144 | + } |
| 145 | + |
| 146 | + return $builder->build(); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * {@inheritdoc} |
| 151 | + */ |
| 152 | + public function getName(): string |
| 153 | + { |
| 154 | + return 'pickpoint'; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * @param string $url |
| 159 | + * @param string|null $locale |
| 160 | + * |
| 161 | + * @return string |
| 162 | + */ |
| 163 | + private function executeQuery(string $url, string $locale = null): string |
| 164 | + { |
| 165 | + if (null !== $locale) { |
| 166 | + $url = sprintf('%s&accept-language=%s', $url, $locale); |
| 167 | + } |
| 168 | + |
| 169 | + return $this->getUrlContents($url); |
| 170 | + } |
| 171 | + |
| 172 | + private function getGeocodeEndpointUrl(): string |
| 173 | + { |
| 174 | + return self::BASE_API_URL.'/forward?q=%s&format=xml&addressdetails=1&limit=%d&key='.$this->apiKey; |
| 175 | + } |
| 176 | + |
| 177 | + private function getReverseEndpointUrl(): string |
| 178 | + { |
| 179 | + return self::BASE_API_URL.'/reverse?format=xml&lat=%F&lon=%F&addressdetails=1&zoom=%d&key='.$this->apiKey; |
| 180 | + } |
| 181 | + |
| 182 | + private function getNodeValue(\DOMNodeList $element) |
| 183 | + { |
| 184 | + return $element->length ? $element->item(0)->nodeValue : null; |
| 185 | + } |
| 186 | +} |
0 commit comments