|
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\LocationInterface; |
|
9
|
|
|
use CodeblogPro\GeoLocationAddress\Country; |
|
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 DaData 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
|
|
|
] |
|
27
|
|
|
); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
protected function getLocationByResponse( |
|
31
|
|
|
ResponseInterface $response, |
|
32
|
|
|
LanguageInterface $language |
|
33
|
|
|
): LocationInterface { |
|
34
|
|
|
$responseContentJson = $response->getBody()->getContents(); |
|
35
|
|
|
$responseContent = $this->getContentFromJson($responseContentJson); |
|
36
|
|
|
$coordinates = null; |
|
37
|
|
|
|
|
38
|
|
|
if (isset($responseContent->location->data->geo_lat) && isset($responseContent->location->data->geo_lon)) { |
|
39
|
|
|
$coordinates = new Coordinates( |
|
40
|
|
|
(float)$responseContent->location->data->geo_lat, |
|
41
|
|
|
(float)$responseContent->location->data->geo_lon |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return new Location( |
|
46
|
|
|
$coordinates, |
|
47
|
|
|
new Country( |
|
48
|
|
|
$responseContent->location->data->country ?? '', |
|
49
|
|
|
$responseContent->location->data->country_iso_code ?? '' |
|
50
|
|
|
), |
|
51
|
|
|
new Region( |
|
52
|
|
|
$responseContent->location->data->region_with_type ?? '', |
|
53
|
|
|
$responseContent->location->data->region_iso_code ?? '' |
|
54
|
|
|
), |
|
55
|
|
|
$responseContent->location->data->city ?? '', |
|
56
|
|
|
'', |
|
57
|
|
|
$responseContent->location->data->postal_code ?? '', |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|