From e5ae2c9a5f523752a65541aec6adc6ddbf716556 Mon Sep 17 00:00:00 2001 From: peterkinamo Date: Fri, 3 Aug 2018 13:02:21 +0200 Subject: [PATCH] ipv4 and ipv6 rule and tests added --- src/Valitron/Validator.php | 24 ++++++++++++++++++++++++ tests/Valitron/ValidateTest.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index eff6d43..574f185 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -493,6 +493,30 @@ class Validator return filter_var($value, \FILTER_VALIDATE_IP) !== false; } + /** + * Validate that a field is a valid IP v4 address + * + * @param string $field + * @param mixed $value + * @return bool + */ + protected function validateIpv4($field, $value) + { + return filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4) !== false; + } + + /** + * Validate that a field is a valid IP v6 address + * + * @param string $field + * @param mixed $value + * @return bool + */ + protected function validateIpv6($field, $value) + { + return filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6) !== false; + } + /** * Validate that a field is a valid e-mail address * diff --git a/tests/Valitron/ValidateTest.php b/tests/Valitron/ValidateTest.php index d77634b..52aba0d 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -645,6 +645,34 @@ class ValidateTest extends BaseTestCase $this->assertFalse($v->validate()); } + public function testIpv4Valid() + { + $v = new Validator(array('ip' => '127.0.0.1')); + $v->rule('ipv4', 'ip'); + $this->assertTrue($v->validate()); + } + + public function testIpv4Invalid() + { + $v = new Validator(array('ip' => 'FE80::0202:B3FF:FE1E:8329')); + $v->rule('ipv4', 'ip'); + $this->assertFalse($v->validate()); + } + + public function testIpv6Valid() + { + $v = new Validator(array('ip' => 'FE80::0202:B3FF:FE1E:8329')); + $v->rule('ipv6', 'ip'); + $this->assertTrue($v->validate()); + } + + public function testIpv6Invalid() + { + $v = new Validator(array('ip' => '127.0.0.1')); + $v->rule('ipv6', 'ip'); + $this->assertFalse($v->validate()); + } + public function testEmailValid() { $v = new Validator(array('name' => 'Chester Tester', 'email' => 'chester@tester.com'));