Skip to content

Commit 7a981f9

Browse files
authored
Merge pull request #218 from maxmind/greg/is-anycast
Add support for is_anycast
2 parents 441034b + 0650a70 commit 7a981f9

File tree

7 files changed

+64
-2
lines changed

7 files changed

+64
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ CHANGELOG
1616
if an invalid IP address is passed to them. Previously, they would make
1717
a request to the web service and throw a
1818
`GeoIp2\Exception\InvalidRequestException`.
19+
* The `isAnycast` property was added to `GeoIp2\Record\Traits`. This returns
20+
`true` if the IP address belongs to an [anycast
21+
network](https://en.wikipedia.org/wiki/Anycast). This is available for the
22+
GeoIP2 Country, City Plus, and Insights web services and the GeoIP2 Country,
23+
City, and Enterprise databases.
1924

2025
2.13.0 (2022-08-05)
2126
-------------------

maxmind-db

Submodule maxmind-db updated 63 files

src/Record/Traits.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ class Traits implements \JsonSerializable
7575
*/
7676
public readonly bool $isAnonymousVpn;
7777

78+
/**
79+
* @var bool This is true if the IP address belongs to an [anycast
80+
* network](https://en.wikipedia.org/wiki/Anycast). This property is not
81+
* available from GeoLite databases or web services.
82+
*/
83+
public readonly bool $isAnycast;
84+
7885
/**
7986
* @var bool This is true if the IP address belongs
8087
* to a hosting or VPN provider (see description of isAnonymousVpn property).
@@ -198,6 +205,7 @@ public function __construct(array $record)
198205
$this->ipAddress = $record['ip_address'] ?? null;
199206
$this->isAnonymous = $record['is_anonymous'] ?? false;
200207
$this->isAnonymousVpn = $record['is_anonymous_vpn'] ?? false;
208+
$this->isAnycast = $record['is_anycast'] ?? false;
201209
$this->isHostingProvider = $record['is_hosting_provider'] ?? false;
202210
$this->isLegitimateProxy = $record['is_legitimate_proxy'] ?? false;
203211
$this->isp = $record['isp'] ?? null;
@@ -242,6 +250,9 @@ public function jsonSerialize(): array
242250
if ($this->isAnonymousVpn !== false) {
243251
$js['is_anonymous_vpn'] = $this->isAnonymousVpn;
244252
}
253+
if ($this->isAnycast !== false) {
254+
$js['is_anycast'] = $this->isAnycast;
255+
}
245256
if ($this->isHostingProvider !== false) {
246257
$js['is_hosting_provider'] = $this->isHostingProvider;
247258
}

tests/GeoIp2/Test/Database/ReaderTest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,34 @@ public function testConnectionType(): void
171171
$ipAddress = '1.0.1.1';
172172

173173
$record = $reader->connectionType($ipAddress);
174-
$this->assertSame('Cable/DSL', $record->connectionType);
174+
$this->assertSame('Cellular', $record->connectionType);
175175
$this->assertSame($ipAddress, $record->ipAddress);
176176
$this->assertSame('1.0.1.0/24', $record->network);
177177
$reader->close();
178178
}
179179

180+
public function testCity(): void
181+
{
182+
$reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');
183+
184+
// This IP has is_anycast
185+
$record = $reader->city('214.1.1.0');
186+
$this->assertTrue($record->traits->isAnycast);
187+
188+
$reader->close();
189+
}
190+
191+
public function testCountry(): void
192+
{
193+
$reader = new Reader('maxmind-db/test-data/GeoIP2-Country-Test.mmdb');
194+
195+
// This IP has is_anycast
196+
$record = $reader->country('214.1.1.0');
197+
$this->assertTrue($record->traits->isAnycast);
198+
199+
$reader->close();
200+
}
201+
180202
public function testDomain(): void
181203
{
182204
$reader = new Reader('maxmind-db/test-data/GeoIP2-Domain-Test.mmdb');
@@ -214,6 +236,10 @@ public function testEnterprise(): void
214236
$this->assertSame('310', $record->traits->mobileCountryCode);
215237
$this->assertSame('004', $record->traits->mobileNetworkCode);
216238

239+
// This IP has is_anycast
240+
$record = $reader->enterprise('214.1.1.0');
241+
$this->assertTrue($record->traits->isAnycast);
242+
217243
$reader->close();
218244
}
219245

tests/GeoIp2/Test/Model/CountryTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class CountryTest extends TestCase
3636
],
3737
'traits' => [
3838
'ip_address' => '1.2.3.4',
39+
'is_anycast' => true,
3940
'prefix_len' => 24,
4041
],
4142
];
@@ -188,6 +189,7 @@ public function testJsonSerialize(): void
188189
],
189190
'traits' => [
190191
'ip_address' => '1.2.3.4',
192+
'is_anycast' => true,
191193
'network' => '1.2.3.0/24',
192194
],
193195
];

tests/GeoIp2/Test/Model/InsightsTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public function testFull(): void
7676
'ip_address' => '1.2.3.4',
7777
'is_anonymous' => true,
7878
'is_anonymous_vpn' => true,
79+
'is_anycast' => true,
7980
'is_hosting_provider' => true,
8081
'is_legitimate_proxy' => true,
8182
'is_public_proxy' => true,
@@ -170,6 +171,11 @@ public function testFull(): void
170171
'$model->traits->isAnonymous is true'
171172
);
172173

174+
$this->assertTrue(
175+
$model->traits->isAnycast,
176+
'$model->traits->isAnycast is true'
177+
);
178+
173179
$this->assertTrue(
174180
$model->traits->isHostingProvider,
175181
'$model->traits->isHostingProvider is true'
@@ -255,6 +261,7 @@ public function testFull(): void
255261
'ip_address' => '1.2.3.4',
256262
'is_anonymous' => true,
257263
'is_anonymous_vpn' => true,
264+
'is_anycast' => true,
258265
'is_hosting_provider' => true,
259266
'is_legitimate_proxy' => true,
260267
'is_public_proxy' => true,

tests/GeoIp2/Test/WebService/ClientTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class ClientTest extends TestCase
3232
'maxmind' => ['queries_remaining' => 11],
3333
'traits' => [
3434
'ip_address' => '1.2.3.4',
35+
'is_anycast' => true,
3536
'network' => '1.2.3.0/24',
3637
],
3738
];
@@ -236,6 +237,11 @@ public function testCountry(): void
236237
'registered_country is_in_european_union is false'
237238
);
238239

240+
$this->assertTrue(
241+
$country->traits->isAnycast,
242+
'is_anycast'
243+
);
244+
239245
$this->assertSame(
240246
'1.2.3.0/24',
241247
$country->traits->network,
@@ -255,6 +261,11 @@ public function testInsights(): void
255261
'continent geoname_id is 42'
256262
);
257263

264+
$this->assertTrue(
265+
$record->traits->isAnycast,
266+
'is_anycast'
267+
);
268+
258269
$this->assertSame(
259270
'1.2.3.0/24',
260271
$record->traits->network,

0 commit comments

Comments
 (0)