1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodeblogPro\GeoLocation\Application\GeoIpRemoteServices; |
4
|
|
|
|
5
|
|
|
use CodeblogPro\GeoLocation\Application\Interfaces\IpAddressInterface; |
6
|
|
|
use CodeblogPro\GeoLocation\Application\Interfaces\LanguageInterface; |
7
|
|
|
use CodeblogPro\GeoCoordinates\Coordinates; |
8
|
|
|
use CodeblogPro\GeoLocationAddress\Country; |
9
|
|
|
use CodeblogPro\GeoLocationAddress\LocationInterface; |
10
|
|
|
use CodeblogPro\GeoLocationAddress\Location; |
11
|
|
|
use CodeblogPro\GeoLocationAddress\Region; |
12
|
|
|
use Psr\Http\Message\RequestInterface; |
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
use GuzzleHttp; |
15
|
|
|
|
16
|
|
|
class MaxMind extends TemplateOfWorkingWithRemoteServiceApi |
17
|
|
|
{ |
18
|
|
|
protected function prepareRequest(IpAddressInterface $ipAddress): RequestInterface |
19
|
|
|
{ |
20
|
|
|
return new GuzzleHttp\Psr7\Request( |
21
|
|
|
'GET', |
22
|
|
|
$this->options->getUrl() . $ipAddress->getValue(), |
23
|
|
|
[ |
24
|
|
|
'Authorization' => $this->options->getKey(), |
25
|
|
|
'Accept' => 'application/json', |
26
|
|
|
'Content-Type' => 'application/json', |
27
|
|
|
] |
28
|
|
|
); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
protected function getLocationByResponse( |
32
|
|
|
ResponseInterface $response, |
33
|
|
|
LanguageInterface $language |
34
|
|
|
): LocationInterface { |
35
|
|
|
$responseContentJson = $response->getBody()->getContents(); |
36
|
|
|
$responseContent = $this->getContentFromJson($responseContentJson); |
37
|
|
|
$coordinates = null; |
38
|
|
|
|
39
|
|
|
if (isset($responseContent->location->latitude) && isset($responseContent->location->longitude)) { |
40
|
|
|
$coordinates = new Coordinates( |
41
|
|
|
(float)$responseContent->location->latitude, |
42
|
|
|
(float)$responseContent->location->longitude |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$languagePostfix = $language->getCode(); |
47
|
|
|
$firstSubdivision = (empty($responseContent->subdivisions)) ? [] : current($responseContent->subdivisions); |
48
|
|
|
|
49
|
|
|
return new Location( |
50
|
|
|
$coordinates, |
51
|
|
|
new Country( |
52
|
|
|
$responseContent->country->names->$languagePostfix ?? '', |
53
|
|
|
$responseContent->country->iso_code ?? '' |
54
|
|
|
), |
55
|
|
|
new Region( |
56
|
|
|
$firstSubdivision->names->$languagePostfix ?? '', |
57
|
|
|
$this->getRegionIsoCodeByCountryIsoCodeAndRegionCode( |
58
|
|
|
$responseContent->country->iso_code ?? '', |
59
|
|
|
$firstSubdivision->iso_code ?? '' |
60
|
|
|
) |
61
|
|
|
), |
62
|
|
|
$responseContent->city->names->$languagePostfix ?? '', |
63
|
|
|
'', |
64
|
|
|
$responseContent->postal->code ?? '' |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function getRegionIsoCodeByCountryIsoCodeAndRegionCode( |
69
|
|
|
string $countryIsoCode = '', |
70
|
|
|
string $regionCode = '' |
71
|
|
|
): string { |
72
|
|
|
$regionIsoCode = $countryIsoCode; |
73
|
|
|
|
74
|
|
|
if (!empty($regionIsoCode)) { |
75
|
|
|
$regionIsoCode .= '-'; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$regionIsoCode .= $regionCode; |
79
|
|
|
|
80
|
|
|
return $regionIsoCode; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|