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 addressProvider */ public function testIsValid($address, $expected) { $this->assertEquals($expected, IPv6::isValid($address)); try { $addr = new IPv6($address); if ($expected === false) { $this->fail('Constructor should fail if invalid IP was given.'); } } catch (\UnexpectedValueException $e) { if ($expected === true) { $this->fail('Constructor should not have thrown up.'); } } }
public function validate() { return \IpUtils\Address\IPv6::isValid($this->_definition); }