-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathIpAddress.php
More file actions
222 lines (188 loc) · 7.14 KB
/
Copy pathIpAddress.php
File metadata and controls
222 lines (188 loc) · 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
declare(strict_types=1);
namespace MaxMind\MinFraud\Model;
use GeoIp2\Model\Insights;
use GeoIp2\Record\Anonymizer;
use GeoIp2\Record\City;
use GeoIp2\Record\Continent;
use GeoIp2\Record\Country;
use GeoIp2\Record\Postal;
use GeoIp2\Record\RepresentedCountry;
use GeoIp2\Record\Subdivision;
use GeoIp2\Record\Traits;
/**
* Model containing GeoIP data and the risk for the IP address.
*/
class IpAddress implements \JsonSerializable
{
/**
* @var Anonymizer data for the anonymizer status of the requested IP
* address. This object contains information about whether
* the IP address belongs to an anonymous network.
*/
public readonly Anonymizer $anonymizer;
/**
* @var City city data for the requested IP address
*/
public readonly City $city;
/**
* @var Continent continent data for the requested IP address
*/
public readonly Continent $continent;
/**
* @var Country Country data for the requested IP address. This object
* represents the country where MaxMind believes the end
* user is located.
*/
public readonly Country $country;
/**
* @var Country Registered country data for the requested IP address.
* This record represents the country where the ISP has
* registered a given IP block and may differ from the
* user's country.
*/
public readonly Country $registeredCountry;
/**
* @var GeoIp2Location location data for the requested IP address
*/
public readonly GeoIp2Location $location;
/**
* @var Subdivision An object representing the most specific subdivision
* returned. If the response did not contain any
* subdivisions, this method returns an empty
* \GeoIp2\Record\Subdivision object.
*/
public readonly Subdivision $mostSpecificSubdivision;
/**
* @var Postal postal data for the requested IP address
*/
public readonly Postal $postal;
/**
* @var RepresentedCountry Represented country data for the requested IP
* address. The represented country is used for
* things like military bases. It is only present
* when the represented country differs from the
* country.
*/
public readonly RepresentedCountry $representedCountry;
/**
* @var float|null This field contains the risk associated with the IP
* address. The value ranges from 0.01 to 99. A higher
* score indicates a higher risk.
*/
public readonly ?float $risk;
/**
* @var array<IpRiskReason> This array contains
* \MaxMind\MinFraud\Model\IpRiskReason objects
* identifying the reasons why the IP address
* received the associated risk. This will be an
* empty array if there are no reasons.
*/
public readonly array $riskReasons;
/**
* @var array<Subdivision> An array of \GeoIp2\Record\Subdivision objects representing
* the country subdivisions for the requested IP address. The
* number and type of subdivisions varies by country, but a
* subdivision is typically a state, province, county, etc.
* Subdivisions are ordered from most general (largest) to most
* specific (smallest). If the response did not contain any
* subdivisions, this method returns an empty array.
*/
public readonly array $subdivisions;
/**
* @var Traits data for the traits of the requested IP
* address
*/
public readonly Traits $traits;
/**
* @param array<string, mixed>|null $response
* @param list<string> $locales
*/
public function __construct(?array $response, array $locales = ['en'])
{
if ($response === null) {
$response = [];
}
$insights = new Insights($response, $locales);
$this->city = $insights->city;
$this->continent = $insights->continent;
$this->country = $insights->country;
$this->mostSpecificSubdivision = $insights->mostSpecificSubdivision;
$this->postal = $insights->postal;
$this->registeredCountry = $insights->registeredCountry;
$this->representedCountry = $insights->representedCountry;
$this->subdivisions = $insights->subdivisions;
$this->traits = $insights->traits;
$this->anonymizer = $insights->anonymizer;
$this->location = new GeoIp2Location($response['location'] ?? []);
$this->risk = $response['risk'] ?? null;
$riskReasons = [];
if (isset($response['risk_reasons'])) {
foreach ($response['risk_reasons'] as $reason) {
$riskReasons[] = new IpRiskReason($reason);
}
}
$this->riskReasons = $riskReasons;
}
/**
* @return array<string, mixed>
*/
public function jsonSerialize(): ?array
{
$js = [];
$anonymizer = $this->anonymizer->jsonSerialize();
if (!empty($anonymizer)) {
$js['anonymizer'] = $anonymizer;
}
$city = $this->city->jsonSerialize();
if (!empty($city)) {
$js['city'] = $city;
}
$continent = $this->continent->jsonSerialize();
if (!empty($continent)) {
$js['continent'] = $continent;
}
$country = $this->country->jsonSerialize();
if (!empty($country)) {
$js['country'] = $country;
}
$location = $this->location->jsonSerialize();
if (!empty($location)) {
$js['location'] = $location;
}
$registeredCountry = $this->registeredCountry->jsonSerialize();
if (!empty($registeredCountry)) {
$js['registered_country'] = $registeredCountry;
}
$representedCountry = $this->representedCountry->jsonSerialize();
if (!empty($representedCountry)) {
$js['represented_country'] = $representedCountry;
}
$traits = $this->traits->jsonSerialize();
if (!empty($traits)) {
$js['traits'] = $traits;
}
$postal = $this->postal->jsonSerialize();
if (!empty($postal)) {
$js['postal'] = $postal;
}
if ($this->risk !== null) {
$js['risk'] = $this->risk;
}
if (!empty($this->riskReasons)) {
$reasons = [];
foreach ($this->riskReasons as $reason) {
$reasons[] = $reason->jsonSerialize();
}
$js['risk_reasons'] = $reasons;
}
$subdivisions = [];
foreach ($this->subdivisions as $sub) {
$subdivisions[] = $sub->jsonSerialize();
}
if (!empty($subdivisions)) {
$js['subdivisions'] = $subdivisions;
}
return $js;
}
}