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\GeoPlugin;
14
15
use Geocoder\Collection;
16
use Geocoder\Exception\InvalidServerResponse;
17
use Geocoder\Exception\UnsupportedOperation;
18
use Geocoder\Http\Provider\AbstractHttpProvider;
19
use Geocoder\Model\Address;
20
use Geocoder\Model\AddressCollection;
21
use Geocoder\Provider\Provider;
22
use Geocoder\Query\GeocodeQuery;
23
use Geocoder\Query\ReverseQuery;
24
25
/**
26
 * @author Andrea Cristaudo <[email protected]>
27
 */
28
final class GeoPlugin extends AbstractHttpProvider implements Provider
29
{
30
    /**
31
     * @var string
32
     */
33
    public const GEOCODE_ENDPOINT_URL = 'http://www.geoplugin.net/json.gp?ip=%s';
34
35 16
    public function geocodeQuery(GeocodeQuery $query): Collection
36
    {
37 16
        $address = $query->getText();
38 16
        if (!filter_var($address, FILTER_VALIDATE_IP)) {
39 1
            throw new UnsupportedOperation('The GeoPlugin provider does not support street addresses, only IP addresses.');
40
        }
41
42 15
        if (in_array($address, ['127.0.0.1', '::1'])) {
43 2
            return new AddressCollection([$this->getLocationForLocalhost()]);
44
        }
45
46 13
        $url = sprintf(self::GEOCODE_ENDPOINT_URL, $address);
47
48 13
        return $this->executeQuery($url);
49
    }
50
51 1
    public function reverseQuery(ReverseQuery $query): Collection
52
    {
53 1
        throw new UnsupportedOperation('The GeoPlugin provider is not able to do reverse geocoding.');
54
    }
55
56 5
    public function getName(): string
57
    {
58 5
        return 'geo_plugin';
59
    }
60
61 13
    private function executeQuery(string $url): AddressCollection
62
    {
63 13
        $content = $this->getUrlContents($url);
64 3
        $json = json_decode($content, true);
65
66 3
        if (!is_array($json) || !count($json)) {
67
            throw InvalidServerResponse::create($url);
68
        }
69
70 3
        if (!array_key_exists('geoplugin_status', $json) || (200 !== $json['geoplugin_status'] && 206 !== $json['geoplugin_status'])) {
71 1
            return new AddressCollection([]);
72
        }
73
74
        // Return empty collection if address was not found
75 2
        if ('' === $json['geoplugin_regionName']
76 2
        && '' === $json['geoplugin_regionCode']
77 2
        && '' === $json['geoplugin_city']
78 2
        && '' === $json['geoplugin_countryName']
79 2
        && '' === $json['geoplugin_countryCode']
80 2
        && '0' === $json['geoplugin_latitude']
81 2
        && '0' === $json['geoplugin_longitude']) {
82
            return new AddressCollection([]);
83
        }
84
85 2
        $data = array_filter($json);
86
87 2
        $adminLevels = [];
88
89 2
        $region = \igorw\get_in($data, ['geoplugin_regionName']);
90 2
        $regionCode = \igorw\get_in($data, ['geoplugin_regionCode']);
91
92 2
        if (null !== $region || null !== $regionCode) {
93 1
            $adminLevels[] = ['name' => $region, 'code' => $regionCode, 'level' => 1];
94
        }
95
96 2
        $results = [];
97 2
        $results[] = Address::createFromArray([
98 2
            'providedBy' => $this->getName(),
99 2
            'locality' => isset($data['geoplugin_city']) ? $data['geoplugin_city'] : null,
100 2
            'country' => isset($data['geoplugin_countryName']) ? $data['geoplugin_countryName'] : null,
101 2
            'countryCode' => isset($data['geoplugin_countryCode']) ? $data['geoplugin_countryCode'] : null,
102 2
            'adminLevels' => $adminLevels,
103 2
            'latitude' => isset($data['geoplugin_latitude']) ? $data['geoplugin_latitude'] : null,
104 2
            'longitude' => isset($data['geoplugin_longitude']) ? $data['geoplugin_longitude'] : null,
105 2
        ]);
106
107 2
        return new AddressCollection($results);
108
    }
109
}
110