|
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\Location; |
|
10
|
|
|
use CodeblogPro\GeoLocationAddress\LocationInterface; |
|
11
|
|
|
use CodeblogPro\GeoLocationAddress\Region; |
|
12
|
|
|
use Psr\Http\Message\RequestInterface; |
|
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
14
|
|
|
use GuzzleHttp; |
|
15
|
|
|
|
|
16
|
|
|
class SypexGeo 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
|
|
|
'Accept' => 'application/json' |
|
25
|
|
|
] |
|
26
|
|
|
); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
protected function getLocationByResponse( |
|
30
|
|
|
ResponseInterface $response, |
|
31
|
|
|
LanguageInterface $language |
|
32
|
|
|
): LocationInterface { |
|
33
|
|
|
$responseContentJson = $response->getBody()->getContents(); |
|
34
|
|
|
$responseContent = $this->getContentFromJson($responseContentJson); |
|
35
|
|
|
$coordinates = null; |
|
36
|
|
|
|
|
37
|
|
|
if ((isset($responseContent->city->lat) && isset($responseContent->city->lon)) |
|
38
|
|
|
|| (isset($responseContent->region->lat) && isset($responseContent->region->lon)) |
|
39
|
|
|
|| (isset($responseContent->country->lat) && isset($responseContent->country->lon)) |
|
40
|
|
|
) { |
|
41
|
|
|
$coordinates = new Coordinates( |
|
42
|
|
|
(float)($responseContent->city->lat ?? $responseContent->region->lat ?? $responseContent->country->lat), |
|
43
|
|
|
(float)($responseContent->city->lon ?? $responseContent->region->lon ?? $responseContent->country->lon), |
|
44
|
|
|
); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$languagePostfix = 'name_' . $language->getCode(); |
|
48
|
|
|
|
|
49
|
|
|
return new Location( |
|
50
|
|
|
$coordinates, |
|
51
|
|
|
new Country($responseContent->country->$languagePostfix ?? '', $responseContent->country->iso ?? ''), |
|
52
|
|
|
new Region($responseContent->region->$languagePostfix ?? '', $responseContent->region->iso ?? ''), |
|
53
|
|
|
$responseContent->city->$languagePostfix ?? '', |
|
54
|
|
|
'', |
|
55
|
|
|
$responseContent->city->post ?? '', |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|