|
31 | 31 | use function is_object; |
32 | 32 | use function preg_match; |
33 | 33 | use function rawurldecode; |
| 34 | +use function strpos; |
34 | 35 | use function strtolower; |
35 | 36 | use function substr; |
36 | 37 |
|
@@ -120,6 +121,8 @@ final class HostRecord implements JsonSerializable |
120 | 121 | private ?string $hostAsUnicode = null; |
121 | 122 | private bool $isIpVersionLoaded = false; |
122 | 123 | private ?string $ipVersion = null; |
| 124 | + private bool $isIpValueLoaded = false; |
| 125 | + private ?string $ipValue = null; |
123 | 126 |
|
124 | 127 | private function __construct( |
125 | 128 | public readonly ?string $value, |
@@ -190,6 +193,36 @@ public function ipVersion(): ?string |
190 | 193 | return $this->ipVersion; |
191 | 194 | } |
192 | 195 |
|
| 196 | + public function ipValue(): ?string |
| 197 | + { |
| 198 | + if (!$this->isIpValueLoaded) { |
| 199 | + $this->isIpValueLoaded = true; |
| 200 | + $this->ipValue = (function (): ?string { |
| 201 | + if (HostType::RegisteredName === $this->type) { |
| 202 | + return null; |
| 203 | + } |
| 204 | + |
| 205 | + if (HostType::Ipv4 === $this->type) { |
| 206 | + return $this->value; |
| 207 | + } |
| 208 | + |
| 209 | + $ip = substr((string) $this->value, 1, -1); |
| 210 | + if (HostType::Ipv6 !== $this->type) { |
| 211 | + return substr($ip, (int) strpos($ip, '.') + 1); |
| 212 | + } |
| 213 | + |
| 214 | + $pos = strpos($ip, '%'); |
| 215 | + if (false === $pos) { |
| 216 | + return $ip; |
| 217 | + } |
| 218 | + |
| 219 | + return substr($ip, 0, $pos).'%'.rawurldecode(substr($ip, $pos + 3)); |
| 220 | + })(); |
| 221 | + } |
| 222 | + |
| 223 | + return $this->ipValue; |
| 224 | + } |
| 225 | + |
193 | 226 | public static function isValid(Stringable|string|null $host): bool |
194 | 227 | { |
195 | 228 | try { |
|
0 commit comments