Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace GeoIp2\Model;
6
 
7
/**
8
 * Model class for the data returned by City Plus web service and City
9
 * database.
10
 *
11
 * See https://dev.maxmind.com/geoip/docs/web-services?lang=en for more
12
 * details.
13
 *
14
 * @property-read \GeoIp2\Record\City $city City data for the requested IP
15
 * address.
16
 * @property-read \GeoIp2\Record\Location $location Location data for the
17
 * requested IP address.
18
 * @property-read \GeoIp2\Record\Postal $postal Postal data for the
19
 * requested IP address.
20
 * @property-read array $subdivisions An array \GeoIp2\Record\Subdivision
21
 * objects representing the country subdivisions for the requested IP
22
 * address. The number and type of subdivisions varies by country, but a
23
 * subdivision is typically a state, province, county, etc. Subdivisions
24
 * are ordered from most general (largest) to most specific (smallest).
25
 * If the response did not contain any subdivisions, this method returns
26
 * an empty array.
27
 * @property-read \GeoIp2\Record\Subdivision $mostSpecificSubdivision An object
28
 * representing the most specific subdivision returned. If the response
29
 * did not contain any subdivisions, this method returns an empty
30
 * \GeoIp2\Record\Subdivision object.
31
 */
32
class City extends Country
33
{
34
    /**
35
     * @ignore
36
     *
37
     * @var \GeoIp2\Record\City
38
     */
39
    protected $city;
40
 
41
    /**
42
     * @ignore
43
     *
44
     * @var \GeoIp2\Record\Location
45
     */
46
    protected $location;
47
 
48
    /**
49
     * @ignore
50
     *
51
     * @var \GeoIp2\Record\Postal
52
     */
53
    protected $postal;
54
 
55
    /**
56
     * @ignore
57
     *
58
     * @var array<\GeoIp2\Record\Subdivision>
59
     */
60
    protected $subdivisions = [];
61
 
62
    /**
63
     * @ignore
64
     */
65
    public function __construct(array $raw, array $locales = ['en'])
66
    {
67
        parent::__construct($raw, $locales);
68
 
69
        $this->city = new \GeoIp2\Record\City($this->get('city'), $locales);
70
        $this->location = new \GeoIp2\Record\Location($this->get('location'));
71
        $this->postal = new \GeoIp2\Record\Postal($this->get('postal'));
72
 
73
        $this->createSubdivisions($raw, $locales);
74
    }
75
 
76
    private function createSubdivisions(array $raw, array $locales): void
77
    {
78
        if (!isset($raw['subdivisions'])) {
79
            return;
80
        }
81
 
82
        foreach ($raw['subdivisions'] as $sub) {
83
            $this->subdivisions[] =
84
                new \GeoIp2\Record\Subdivision($sub, $locales)
85
            ;
86
        }
87
    }
88
 
89
    /**
90
     * @ignore
91
     *
92
     * @return mixed
93
     */
94
    public function __get(string $attr)
95
    {
96
        if ($attr === 'mostSpecificSubdivision') {
97
            return $this->{$attr}();
98
        }
99
 
100
        return parent::__get($attr);
101
    }
102
 
103
    /**
104
     * @ignore
105
     */
106
    public function __isset(string $attr): bool
107
    {
108
        if ($attr === 'mostSpecificSubdivision') {
109
            // We always return a mostSpecificSubdivision, even if it is the
110
            // empty subdivision
111
            return true;
112
        }
113
 
114
        return parent::__isset($attr);
115
    }
116
 
117
    private function mostSpecificSubdivision(): \GeoIp2\Record\Subdivision
118
    {
119
        return empty($this->subdivisions) ?
120
            new \GeoIp2\Record\Subdivision([], $this->locales) :
121
            end($this->subdivisions);
122
    }
123
}