Code

< 40 %
40-60 %
> 60 %
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Geocoder package.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT License
11
 */
12
13
namespace Geocoder\Provider\HostIp;
14
15
use Geocoder\Model\AddressCollection;
16
17
/**
18
 * @author Oleg Andreyev <[email protected]>
19
 */
20
final class HostIpXml extends AbstractHostIp
21
{
22
    /**
23
     * @var string
24
     */
25
    public const ENDPOINT_URL = 'http://api.hostip.info/get_xml.php?ip=%s&position=true';
26
27 4
    public function getName(): string
28
    {
29 4
        return 'host_ip_xml';
30
    }
31
32 2
    public function getEndpointURL(): string
33
    {
34 2
        return self::ENDPOINT_URL;
35
    }
36
37 2
    protected function executeQuery(string $url): AddressCollection
38
    {
39 2
        $content = $this->getUrlContents($url);
40 2
        $xml = \simplexml_load_string($content);
41
42 2
        $hostIp = $xml->xpath('/HostipLookupResultSet/gml:featureMember/Hostip');
43 2
        if (empty($hostIp[0])) {
44
            return new AddressCollection([]);
45
        }
46 2
        $hostIp = $hostIp[0];
47
48 2
        $city = (string) $hostIp->xpath('.//gml:name')[0];
49 2
        $countryName = (string) $hostIp->xpath('.//countryName')[0];
50 2
        $countryCode = (string) $hostIp->xpath('.//countryAbbrev')[0];
51 2
        $coords = $hostIp->xpath('.//ipLocation/gml:pointProperty/gml:Point/gml:coordinates');
52
53 2
        if (empty($coords)) {
54 1
            list($lng, $lat) = [null, null];
55
        } else {
56 1
            list($lng, $lat) = explode(',', (string) $coords[0], 2);
57
        }
58
59 2
        return $this->prepareAddressCollection([
60 2
            'lat' => $lat,
61 2
            'lng' => $lng,
62 2
            'city' => $city,
63 2
            'country_name' => $countryName,
64 2
            'country_code' => $countryCode,
65 2
        ]);
66
    }
67
}
68