Rev 1121 | Rev 1174 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1121 | Rev 1130 | ||
---|---|---|---|
Line 22... | Line 22... | ||
22 | // phpcs:disable PSR1.Files.SideEffects |
22 | // phpcs:disable PSR1.Files.SideEffects |
23 | \defined('INSIDE_OIDPLUS') or die; |
23 | \defined('INSIDE_OIDPLUS') or die; |
24 | // phpcs:enable PSR1.Files.SideEffects |
24 | // phpcs:enable PSR1.Files.SideEffects |
25 | 25 | ||
26 | class OIDplusIpv4 extends OIDplusObject { |
26 | class OIDplusIpv4 extends OIDplusObject { |
- | 27 | /** |
|
- | 28 | * @var string |
|
- | 29 | */ |
|
27 | private $ipv4; |
30 | private $ipv4; |
- | 31 | ||
- | 32 | /** |
|
- | 33 | * @var string |
|
- | 34 | */ |
|
28 | private $bare; |
35 | private $bare; |
- | 36 | ||
- | 37 | /** |
|
- | 38 | * @var int |
|
- | 39 | */ |
|
29 | private $cidr; |
40 | private $cidr; |
30 | 41 | ||
31 | /** |
42 | /** |
32 | * @param $ipv4 |
43 | * @param string $ipv4 |
33 | * @throws OIDplusException |
44 | * @throws OIDplusException |
34 | */ |
45 | */ |
35 | public function __construct($ipv4) { |
46 | public function __construct(string $ipv4) { |
36 | $this->ipv4 = $ipv4; |
47 | $this->ipv4 = $ipv4; |
37 | 48 | ||
38 | if (!empty($ipv4)) { |
49 | if (!empty($ipv4)) { |
39 | if (strpos($ipv4, '/') === false) $ipv4 .= '/32'; |
50 | if (strpos($ipv4, '/') === false) $ipv4 .= '/32'; |
40 | list($bare, $cidr) = explode('/', $ipv4); |
51 | list($bare, $cidr) = explode('/', $ipv4); |
41 | $this->bare = $bare; |
52 | $this->bare = $bare; |
- | 53 | if (is_numeric($cidr)) throw new OIDplusException(_L('Invalid IPv4')); |
|
42 | $this->cidr = $cidr; |
54 | $this->cidr = (int)$cidr; |
43 | if (!ipv4_valid($bare)) throw new OIDplusException(_L('Invalid IPv4')); |
55 | if (!ipv4_valid($bare)) throw new OIDplusException(_L('Invalid IPv4')); |
44 | if (!is_numeric($cidr)) throw new OIDplusException(_L('Invalid IPv4')); |
56 | if (!is_numeric($cidr)) throw new OIDplusException(_L('Invalid IPv4')); |
45 | if ($cidr < 0) throw new OIDplusException(_L('Invalid IPv4')); |
57 | if ($cidr < 0) throw new OIDplusException(_L('Invalid IPv4')); |
46 | if ($cidr > 32) throw new OIDplusException(_L('Invalid IPv4')); |
58 | if ($cidr > 32) throw new OIDplusException(_L('Invalid IPv4')); |
47 | $this->bare = ipv4_normalize($this->bare); |
59 | $this->bare = ipv4_normalize($this->bare); |
Line 50... | Line 62... | ||
50 | } |
62 | } |
51 | 63 | ||
52 | /** |
64 | /** |
53 | * @param string $node_id |
65 | * @param string $node_id |
54 | * @return OIDplusIpv4|null |
66 | * @return OIDplusIpv4|null |
- | 67 | * @throws OIDplusException |
|
55 | */ |
68 | */ |
56 | public static function parse(string $node_id)/*: ?OIDplusIpv4*/ { |
69 | public static function parse(string $node_id)/*: ?OIDplusIpv4*/ { |
57 | @list($namespace, $ipv4) = explode(':', $node_id, 2); |
70 | @list($namespace, $ipv4) = explode(':', $node_id, 2); |
58 | if ($namespace !== self::ns()) return null; |
71 | if ($namespace !== self::ns()) return null; |
59 | return new self($ipv4); |
72 | return new self($ipv4); |
Line 240... | Line 253... | ||
240 | $tmp = ipv4_normalize_range($this->bare . '/' . $cidr); |
253 | $tmp = ipv4_normalize_range($this->bare . '/' . $cidr); |
241 | return self::parse($this->ns() . ':' . $tmp); |
254 | return self::parse($this->ns() . ':' . $tmp); |
242 | } |
255 | } |
243 | 256 | ||
244 | /** |
257 | /** |
245 | * @param $to |
258 | * @param OIDplusObject|string $to |
246 | * @return float|int|mixed|string|null |
259 | * @return float|int|mixed|string|null |
247 | */ |
260 | */ |
248 | public function distance($to) { |
261 | public function distance($to) { |
249 | if (!is_object($to)) $to = OIDplusObject::parse($to); |
262 | if (!is_object($to)) $to = OIDplusObject::parse($to); |
250 | if (!$to) return null; |
263 | if (!$to) return null; |