public function __construct($expression) { if (strpos($expression, '/') === false) { throw new InvalidExpressionException('Invalid subnet expression "' . $expression . '" given.'); } list($lower, $netmask) = explode('/', $expression, 2); if (strpos($netmask, '.') !== false || strpos($netmask, ':') !== false) { throw new InvalidExpressionException('Netmasks may not use the IP address format ("127.0.0.1/255.0.0.0").'); } // check IP format first if (IPv4::isValid($lower)) { $ip = new IPv4($lower); } elseif (IPv6::isValid($lower)) { $ip = new IPv6($lower); } else { throw new InvalidExpressionException('Subnet expression "' . $expression . '" contains an invalid IP.'); } // now we can properly handle the netmask range $netmask = (int) $netmask; if (!$ip::isValidNetmask($netmask)) { throw new InvalidExpressionException('Invalid or out of range netmask given.'); } $this->lower = $ip; $this->netmask = $netmask; }
public function __construct($expression) { $expression = strtolower(trim($expression)); if (IPv4::isValid($expression)) { $ip = new IPv4($expression); } elseif (IPv6::isValid($expression)) { $ip = new IPv6($expression); } else { throw new InvalidExpressionException('Expression must be either a valid IPv4 or IPv6 address.'); } $this->expression = $ip->getCompact(); }
/** * @dataProvider linkLocalProvider */ public function testIsLinkLocal($address, $expected) { $addr = new IPv4($address); $this->assertSame($expected, $addr->isLinkLocal()); }
/** * * @param \IpUtils\Address\IPv4 $ip * @return boolean */ private function isIpv4InUnroutableRange(IPv4 $ip) { foreach ($this->unrouteableRanges as $ipRange) { if ($ip->matches(new Subnet($ipRange))) { return true; } } return false; }
public function validate() { return \IpUtils\Address\IPv4::isValid($this->_definition); }