Beispiel #1
0
 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;
 }
Beispiel #2
0
 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();
 }
Beispiel #3
0
 /**
  * @dataProvider  addressProvider
  */
 public function testIsValid($address, $expected)
 {
     $this->assertEquals($expected, IPv4::isValid($address));
     try {
         $addr = new IPv4($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.');
         }
     }
 }
Beispiel #4
0
 public function validate()
 {
     return \IpUtils\Address\IPv4::isValid($this->_definition);
 }