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