Repository navigation
Expand file tree
/
Copy pathGeolocator.php
More file actions
167 lines (132 loc) · 4.66 KB
/
Copy pathGeolocator.php
File metadata and controls
167 lines (132 loc) · 4.66 KB
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
<?php declare(strict_types=1);
namespace Fop\Geolocation;
use Fop\Entity\Location;
use Fop\Guzzle\RequestClient;
use Location\Coordinate;
use Rinvex\Country\Country;
use Rinvex\Country\CountryLoader;
final class Geolocator
{
/**
* @var string
*/
private const API_CITY_TO_LOCATION = 'https://nominatim.openstreetmap.org/search.php?q=%s&format=json';
/**
* @var string
*/
private const API_LOCATION_TO_COUNTRY = 'https://nominatim.openstreetmap.org/reverse?format=json&lat=%s&lon=%s';
/**
* @var mixed[]
*/
private $countryJsonByLatitudeAndLongitudeCache = [];
/**
* @var string[]
*/
private $usaStates = [];
/**
* @var RequestClient
*/
private $requestClient;
/**
* @param string[] $usaStates
*/
public function __construct(array $usaStates, RequestClient $requestClient)
{
$this->usaStates = $usaStates;
$this->requestClient = $requestClient;
}
public function createLocationFromCity(string $city): ?Location
{
$url = sprintf(self::API_CITY_TO_LOCATION, $city);
$json = $this->requestClient->requestToJson($url);
if (! isset($json[0]['lat']) || ! isset($json[0]['lat'])) {
return null;
}
$lat = (float) $json[0]['lat'];
$lon = (float) $json[0]['lon'];
$country = $this->resolveCountryByLatitudeAndLongitude($lat, $lon);
return new Location($city, $country, new Coordinate($lat, $lon));
}
public function resolveCountryByLatitudeAndLongitude(float $latitude, float $longitude): string
{
$countryJson = $this->getCountryJsonByLatitudeAndLongitude($latitude, $longitude);
if ($countryJson['address']['country'] === 'USA') {
return $countryJson['address']['state'];
}
$countryCode = $countryJson['address']['country_code'];
if ($countryCode) {
// get English name
$country = CountryLoader::country($countryCode);
if (is_array($country)) {
$country = array_pop($country);
}
return $country->getName();
}
return $countryJson['address']['countrymp'];
}
/**
* @param mixed[] $group
*/
public function resolveCountryByGroup(array $group): ?string
{
// Special case for USA, since there are many federate states
if (isset($group['country']) && $group['country'] === 'US') {
$stateCode = strtolower($group['state']);
if (isset($this->usaStates[$stateCode])) {
return $this->usaStates[$stateCode];
}
// unknown state :(, fallback
}
$countryCode = $this->resolveCountryCodeFromGroup($group);
$countryOrCountries = CountryLoader::country($countryCode);
if (is_array($countryOrCountries)) {
/** @var Country $country */
$country = array_pop($countryOrCountries);
return $country->getName();
}
return $countryOrCountries->getName();
}
/**
* @param mixed[] $venue
*/
public function resolveCountryByVenue(array $venue): string
{
if ($venue['localized_country_name'] !== 'USA') {
return $venue['localized_country_name'];
}
if (isset($venue['state'])) {
$stateCode = strtolower($venue['state']);
if (isset($this->usaStates[$stateCode])) {
return $this->usaStates[$stateCode];
}
}
return $this->resolveCountryByLatitudeAndLongitude($venue['lat'], $venue['lon']);
}
/**
* @return mixed[]
*/
public function getCountryJsonByLatitudeAndLongitude(float $latitude, float $longitude): array
{
$cacheKey = sha1($longitude . $longitude);
if (isset($this->countryJsonByLatitudeAndLongitudeCache[$cacheKey])) {
return $this->countryJsonByLatitudeAndLongitudeCache[$cacheKey];
}
$url = sprintf(self::API_LOCATION_TO_COUNTRY, $latitude, $longitude);
$json = $this->requestClient->requestToJson($url);
$this->countryJsonByLatitudeAndLongitudeCache[$cacheKey] = $json;
return $json;
}
/**
* @see https://stackoverflow.com/a/45826290/1348344
*
* @param mixed[] $group
*/
private function resolveCountryCodeFromGroup(array $group): string
{
if (isset($group['country']) && $group['country'] && $group['country'] !== '-') {
return $group['country'];
}
$countryJson = $this->getCountryJsonByLatitudeAndLongitude($group['latitude'], $group['longitude']);
return $countryJson['address']['country_code'];
}
}