diff --git a/src/Validators/IPAddress.php b/src/Validators/IPAddress.php index 928ed97..157e8ce 100644 --- a/src/Validators/IPAddress.php +++ b/src/Validators/IPAddress.php @@ -26,10 +26,12 @@ class IPAddress extends Filter * * @param bool $IPv4 Filter only IPv4 addresses * @param bool $IPv6 Filter only IPv6 addresses + * @param bool $no_priv_range Address not in private range * @param string $message Message returned on validation error * @return void */ - public function __construct($IPv4 = true, $IPv6 = false, $message = "The field must be a valid IP Address.") + public function __construct($IPv4 = true, $IPv6 = false, $no_priv_range = false, + $message = "The field must be a valid IP Address.") { parent::__construct($message); $this->filterFlag = FILTER_VALIDATE_IP; @@ -37,6 +39,31 @@ public function __construct($IPv4 = true, $IPv6 = false, $message = "The field m $this->filterOptions = FILTER_FLAG_IPV4; } elseif ($IPv6) { $this->filterOptions = FILTER_FLAG_IPV6; + } elseif ($no_priv_range) { + $this->filterOptions = FILTER_FLAG_NO_PRIV_RANGE; } } + + /** + * Check if value passes the validation + * + * @param string $value Value to check + * @return bool + */ + public function check($value) + { + if (strpos($value, "/")) { + $value = explode("/", $value)[0]; + } + if ($this->filterOptions) { + $filter = filter_var($value, $this->filterFlag, $this->filterOptions); + } else { + $filter = filter_var($value, $this->filterFlag); + } + if ($filter) { + return true; + } + $this->setError($this->message); + return false; + } } diff --git a/tests/Validators/IPAddressTest.php b/tests/Validators/IPAddressTest.php index 9c90e0d..ca255d9 100644 --- a/tests/Validators/IPAddressTest.php +++ b/tests/Validators/IPAddressTest.php @@ -13,6 +13,7 @@ public function testIPAddress() $ip = new \Forms\Validators\IPAddress(); $this->assertTrue($ip->check('8.8.8.8'), "IPAddress did not validate correctly."); + $this->assertTrue($ip->check('10.10.5.45/32'), "IPAddress did not validate correctly."); $this->assertFalse($ip->check('10.10.10.300'), "IPAddress did not validate correctly."); $this->assertEquals("The field must be a valid IP Address.", $ip->getError(),""); }