-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenApi.php
180 lines (146 loc) · 6.03 KB
/
OpenApi.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
declare(strict_types=1);
/*
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Geocoder\Provider\OpenApi;
use Geocoder\Collection;
use Geocoder\Exception\UnsupportedOperation;
use Geocoder\Http\Provider\AbstractHttpProvider;
use Geocoder\Model\AddressBuilder;
use Geocoder\Model\AddressCollection;
use Geocoder\Provider\Provider;
use Geocoder\Provider\OpenApi\Model\OpenApiAddress;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\ReverseQuery;
use Psr\Http\Client\ClientInterface;
/**
* @author Antoine Corcy <[email protected]>
*/
final class OpenApi extends AbstractHttpProvider implements Provider
{
/**
* @var string
*/
public const GEOCODE_ENDPOINT_URL = 'https://geocoding.openapi.it/geocode';
/**
* @var string
*/
public const REVERSE_ENDPOINT_URL = 'https://geocoding.openapi.it/reverse';
/**
* @var string
*/
private $toponym;
/**
* @var string|null
*/
private $apiKey;
/**
* @param ClientInterface $client an HTTP adapter
* @param string $toponym toponym biasing only for reverse geocoding (optional)
* @param string|null $apiKey API Key
*/
public function __construct(ClientInterface $client, Guzzle $guzzle, string $apiKey = null)
{
parent::__construct($client);
$this->toponym = $toponym;
$this->apiKey = $apiKey;
}
public function geocodeQuery(GeocodeQuery $query): Collection
{
$address = $query->getText();
// This API doesn't handle IPs
if (filter_var($address, FILTER_VALIDATE_IP)) {
throw new UnsupportedOperation('The OpenApi provider does not support IP addresses, only street addresses.');
}
$url = sprintf(self::GEOCODE_ENDPOINT_URL, urlencode($address));
return $this->executeQuery($url, $query->getLimit(), $query->getLocale());
}
public function reverseQuery(ReverseQuery $query): Collection
{
$coordinates = $query->getCoordinates();
$longitude = $coordinates->getLongitude();
$latitude = $coordinates->getLatitude();
$url = sprintf(self::REVERSE_ENDPOINT_URL, $longitude, $latitude);
if (null !== $toponym = $query->getData('toponym', $this->toponym)) {
$url = sprintf('%s&kind=%s', $url, $toponym);
}
return $this->executeQuery($url, $query->getLimit(), $query->getLocale());
}
public function getName(): string
{
return 'yandex';
}
/**
* @param string $locale
*/
private function executeQuery(string $url, int $limit, string $locale = null): AddressCollection
{
if (null !== $locale) {
$url = sprintf('%s&lang=%s', $url, str_replace('_', '-', $locale));
}
if (null !== $this->apiKey) {
$url = sprintf('%s&apikey=%s', $url, $this->apiKey);
}
$url = sprintf('%s&results=%d', $url, $limit);
$content = $this->getUrlContents($url);
$json = json_decode($content, true);
if (empty($json) || isset($json['error'])
|| (isset($json['response']) && '0' === $json['response']['GeoObjectCollection']['metaDataProperty']['GeocoderResponseMetaData']['found'])
) {
return new AddressCollection([]);
}
$data = $json['response']['GeoObjectCollection']['featureMember'];
$locations = [];
foreach ($data as $item) {
$builder = new AddressBuilder($this->getName());
$bounds = null;
$flatArray = ['pos' => ' '];
array_walk_recursive(
$item['GeoObject'],
/**
* @param string $value
*/
function ($value, $key) use (&$flatArray) {
$flatArray[$key] = $value;
}
);
if (!empty($flatArray['lowerCorner']) && !empty($flatArray['upperCorner'])) {
$lowerCorner = explode(' ', $flatArray['lowerCorner']);
$upperCorner = explode(' ', $flatArray['upperCorner']);
$builder->setBounds(
(float) $lowerCorner[1],
(float) $lowerCorner[0],
(float) $upperCorner[1],
(float) $upperCorner[0]
);
}
$coordinates = explode(' ', $flatArray['pos']);
$builder->setCoordinates((float) $coordinates[1], (float) $coordinates[0]);
foreach (['AdministrativeAreaName', 'SubAdministrativeAreaName'] as $i => $name) {
if (isset($flatArray[$name])) {
$builder->addAdminLevel($i + 1, $flatArray[$name], null);
}
}
$builder->setStreetNumber(isset($flatArray['PremiseNumber']) ? $flatArray['PremiseNumber'] : null);
$builder->setStreetName(isset($flatArray['ThoroughfareName']) ? $flatArray['ThoroughfareName'] : null);
$builder->setSubLocality(isset($flatArray['DependentLocalityName']) ? $flatArray['DependentLocalityName'] : null);
$builder->setLocality(isset($flatArray['LocalityName']) ? $flatArray['LocalityName'] : null);
$builder->setCountry(isset($flatArray['CountryName']) ? $flatArray['CountryName'] : null);
$builder->setCountryCode(isset($flatArray['CountryNameCode']) ? $flatArray['CountryNameCode'] : null);
/** @var OpenApiAddress $location */
$location = $builder->build(OpenApiAddress::class);
$location = $location->withPrecision(isset($flatArray['precision']) ? $flatArray['precision'] : null);
$location = $location->withName(isset($flatArray['name']) ? $flatArray['name'] : null);
$location = $location->withKind($flatArray['kind'] ?? null);
$locations[] = $location;
}
return new AddressCollection($locations);
}
protected function getUrlContents($url, $data) {
}
}