-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAsnService.php
More file actions
103 lines (97 loc) · 2.68 KB
/
Copy pathAsnService.php
File metadata and controls
103 lines (97 loc) · 2.68 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
<?php
declare(strict_types=1);
namespace ArrayAccess\RdapClient\Services;
use function explode;
use function is_numeric;
use function is_string;
use function preg_match;
use function reset;
use function str_contains;
use function strtolower;
use function trim;
class AsnService extends AbstractRdapService
{
public const MAX_INTEGER = 4294967296;
/**
* @inheritDoc
*/
protected function normalizeSource(string $target) : string
{
if (str_contains($target, '.')) {
$this->throwInvalidTarget($target);
}
if (is_numeric($target)) {
return $target;
}
$explode = explode('-', $target);
// xx-xxx
if (count($explode) !== 2) {
$this->throwInvalidTarget($target);
}
foreach ($explode as $item) {
if (str_contains($item, '.')
|| !is_numeric($item)
|| ((int)$item) < 0
|| ((int)$item) > self::MAX_INTEGER
) {
$this->throwInvalidTarget($target);
}
}
return $target;
}
/**
* @inheritDoc
*/
public function normalize(string $target) : ?string
{
$target = trim($target);
if ($target === '') {
return null;
}
$target = strtolower($target);
if (!preg_match('~^(?:asn?)?([0-9]+)$~', $target, $match)) {
return null;
}
$integer = ((int) ($match[1]));
return $integer > 0 && $integer <= self::MAX_INTEGER ? $match[1] : null;
}
/**
* @inheritDoc
*/
public function getRdapURL(string|int $target): ?string
{
$target = $this->normalize((string) $target);
if ($target === null) {
return null;
}
if ($target < 0 || $target > self::MAX_INTEGER) {
return null;
}
foreach ($this->services as $service) {
$urls = $service[1]??[];
$url = reset($urls);
if (!$url) {
continue;
}
foreach ($service[0] as $number) {
if (!is_string($number)) {
continue;
}
if (!str_contains($number, '-')) {
if (!is_numeric($number)) {
continue;
}
if ($target === $number) {
return $url;
}
continue;
}
[$start, $end] = explode('-', $number);
if ($start <= $target && $end >= $target) {
return $url;
}
}
}
return null;
}
}