From 0b3704148139ba79943b599aac77f15552496661 Mon Sep 17 00:00:00 2001 From: Alexandre Date: Mon, 21 Feb 2011 00:42:32 -0300 Subject: [PATCH] Not validator for negating rules (v::not(v::string()) means not string). Improvements on the Domain validator. Needs Refactoring. --- .../Validation/Exceptions/NotException.php | 7 +++ library/Respect/Validation/Rules/Domain.php | 8 ++- library/Respect/Validation/Rules/Not.php | 49 +++++++++++++++++++ library/Respect/Validation/package.xml | 10 ++-- .../Respect/Validation/Rules/DomainTest.php | 2 + 5 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 library/Respect/Validation/Exceptions/NotException.php create mode 100644 library/Respect/Validation/Rules/Not.php diff --git a/library/Respect/Validation/Exceptions/NotException.php b/library/Respect/Validation/Exceptions/NotException.php new file mode 100644 index 00000000..b3c91299 --- /dev/null +++ b/library/Respect/Validation/Exceptions/NotException.php @@ -0,0 +1,7 @@ +ip = new Ip(); $this->whitespace = new NoWhitespace(); $this->dot = new Contains('.'); + $this->doubleHyphen = new Not(new Contains('--')); $this->domainLength = new Length(3, null); $this->end = new Tld(); - $this->otherParts = new Alnum('-'); + $this->otherParts = new AllOf( + new Alnum('-'), + new Not(new StartsWith('-')) + ); } public function validate($input) @@ -54,6 +59,7 @@ class Domain extends AbstractComposite $this->collectAssertException($e, $this->whitespace, $input); $this->collectAssertException($e, $this->dot, $input); + $this->collectAssertException($e, $this->doubleHyphen, $input); $this->collectAssertException($e, $this->domainLength, $input); $parts = explode('.', $input); diff --git a/library/Respect/Validation/Rules/Not.php b/library/Respect/Validation/Rules/Not.php new file mode 100644 index 00000000..d4e662ce --- /dev/null +++ b/library/Respect/Validation/Rules/Not.php @@ -0,0 +1,49 @@ +rule = $rule; + } + + public function validate($input) + { + return!$this->rule->validate($input); + } + + public function assert($input) + { + try { + $this->rule->assert($input); + } catch (ValidationException $e) { + return true; + } + $e = $this->rule->reportError($input); + //TODO very very very nasty hack. Need to think of a better solution + $e->setTemplate(str_replace('must', 'must not', $e->getTemplate())); + throw $e; + } + + public function check($input) + { + try { + $this->rule->check($input); + } catch (ValidationException $e) { + return true; + } + $e = $this->rule->reportError($input); + //TODO very very very nasty hack. Need to think of a better solution + $e->setTemplate(str_replace('must', 'must not', $e->getTemplate())); + throw $e; + } + +} \ No newline at end of file diff --git a/library/Respect/Validation/package.xml b/library/Respect/Validation/package.xml index e39c60a4..f76bce02 100644 --- a/library/Respect/Validation/package.xml +++ b/library/Respect/Validation/package.xml @@ -10,8 +10,8 @@ alexandre@gaigalas.net yes - 2011-02-20 - + 2011-02-21 + 0.2 0.2 @@ -61,6 +61,7 @@ + @@ -89,7 +90,7 @@ - + @@ -106,6 +107,7 @@ + @@ -145,7 +147,7 @@ alpha alpha - 2011-02-20 + 2011-02-21 BSD Style . diff --git a/tests/library/Respect/Validation/Rules/DomainTest.php b/tests/library/Respect/Validation/Rules/DomainTest.php index 590ed47e..27a36ff7 100644 --- a/tests/library/Respect/Validation/Rules/DomainTest.php +++ b/tests/library/Respect/Validation/Rules/DomainTest.php @@ -44,6 +44,8 @@ class DomainTest extends \PHPUnit_Framework_TestCase return array( array(null), array('domain.local'), + array('example--invalid.com'), + array('-example-invalid.com'), array('1.2.3.256'), ); }