From 822fd269f88adc7fc425ae079296b302b9899459 Mon Sep 17 00:00:00 2001 From: Alexandre Date: Mon, 7 Feb 2011 12:22:30 -0200 Subject: [PATCH] Better README, fixed bugs on AllOf and AlphaException --- README.md | 79 +++++++++++-------- .../Validation/Exceptions/AlphaException.php | 2 +- library/Respect/Validation/Rules/AllOf.php | 2 +- library/Respect/Validation/package.xml | 6 +- library/SplClassLoader.php | 16 ++-- .../Respect/Validation/ValidatorTest.php | 53 ++++++++++--- 6 files changed, 100 insertions(+), 58 deletions(-) diff --git a/README.md b/README.md index d50af6ce..6b6f3a8a 100644 --- a/README.md +++ b/README.md @@ -16,80 +16,89 @@ Quick Reference Namespace import ---------------- + validate($someNumber); //returns true or false + $number = 123; + v::numeric()->validate($number); //true Chained validation ------------------ //From 1 to 15 non-whitespace alphanumeric characters - $username = 'alganet'; $validUsername = v::alnum() ->noWhitespace() - ->length(1,15) - ->validate($username); + ->length(1,15); - //Date between two ranges using a specific format - $someDate = new DateTime('2010-10-15'); - $validDate = v::date('Y-m-d') - ->between(new DateTime('2009-01-01'), new DateTime('2011-01-01')) - ->validate($someDate); + $validUsername->validate('alganet'); //true Validating object attributes ---------------------------- + + $validUser = v::attribute('username', $validUsername) //reusing! + ->attribute('birthdate', v::date('Y-m-d')); $user = new \stdClass; - $user->name = 'Alexandre'; + $user->username = 'alganet'; $user->birthdate = '1987-07-01'; - $validUser = v::attribute('name', v::notEmpty()) - ->v::attribute('birthdate, v::date('Y-m-d')); + + $validUser->validate($user); //true Validator reuse (works on nested, big validators too!) ------------------------------------------------------ - $idValidator = v::int()->positive(); - $idValidator->validate(123); //true - $idValidator->validate(456); //true - $idValidator->validate('foo'); //false - $idValidator->validate(178); //true + $validUsername->validate('respect'); //true + $validUsername->validate('alexandre gaigalas'); //false + $validUsername->validate('#$%'); //false Cool, informative exceptions ---------------------------- +The following code: + try { - $username = '#some%really*bad screen name'; - $validUsername = v::alnum('_') - ->noWhitespace() - ->length(1,15) - ->assert($username); + $validUsername->assert('really messed up screen#name'); } catch(\InvalidArgumentException $e) { - /* prints: - \-None of 3 required rules passed - |-"really messed up screen#name" does not contain only letters, digits and "_" - |-"really messed up screen#name" contains whitespace - \-"really messed up screen#name" length is not between 1 and 15 - */ echo $e->getFullMessage(); } +Produces this message: + + \-None of 3 required rules passed + |-"really messed up screen#name" does not contain only letters, digits and "_" + |-"really messed up screen#name" contains whitespace + \-"really messed up screen#name" length is not between 1 and 15 + Message finding on nested Exceptions ------------------------------------ - $user = array("id" => "some %% invalid %% id"); - $post = array("user" => $user); +Consider the following scenario: + + $validBlogPost = v::object() + ->attribute('title', v::string()->length(1,32)) + ->attribute('author', $validUser) //reuse! + ->attribute('date', v::date()) + ->attribute('text', v::string()); + + $blogPost = new \stdClass; + $blogPost->author = clone $validUser; + $blogPost->author->username = '# invalid #'; + +The following code: + try { - v::key("user", v::key("id", v::int()->positive()))->assert($post); + $validBlogPost->assert($blogPost); } catch (\InvalidArgumentException $e) { - /* prints: - "some %% invalid %% id" is not a positive number - */ - echo $e->findRelated('user', 'id', 'positive')->getMainMessage(); + echo $e->findRelated('author', 'username', 'noWhitespace')->getMainMessage(); } +Finds the specific noWhitespace message inside author->username and prints it: + +>"# invalid #" contains whitespace + Using Zend and/or Symfony validators ------------------------------------ diff --git a/library/Respect/Validation/Exceptions/AlphaException.php b/library/Respect/Validation/Exceptions/AlphaException.php index 7aea1490..cd2816b1 100644 --- a/library/Respect/Validation/Exceptions/AlphaException.php +++ b/library/Respect/Validation/Exceptions/AlphaException.php @@ -13,7 +13,7 @@ class AlphaException extends ValidationException public function chooseTemplate($input, $additionalCharacters=null) { - return empty($additionalCharacters) ? static::NORMAL : static::EXTRA; + return empty($additionalCharacters) ? static::STANDARD : static::EXTRA; } } \ No newline at end of file diff --git a/library/Respect/Validation/Rules/AllOf.php b/library/Respect/Validation/Rules/AllOf.php index 07afd6a3..4bfba709 100644 --- a/library/Respect/Validation/Rules/AllOf.php +++ b/library/Respect/Validation/Rules/AllOf.php @@ -24,7 +24,7 @@ class AllOf extends AbstractComposite public function validate($input) { foreach ($this->getRules() as $rule) - if (!$rule->validate()) + if (!$rule->validate($input)) return false; return true; } diff --git a/library/Respect/Validation/package.xml b/library/Respect/Validation/package.xml index bde1f721..ec3096e6 100644 --- a/library/Respect/Validation/package.xml +++ b/library/Respect/Validation/package.xml @@ -11,7 +11,7 @@ yes 2011-02-07 - + 0.1.0 0.1.0 @@ -30,7 +30,7 @@ First Version - + @@ -70,7 +70,7 @@ First Version - + diff --git a/library/SplClassLoader.php b/library/SplClassLoader.php index dc1f63e8..eb4f6839 100644 --- a/library/SplClassLoader.php +++ b/library/SplClassLoader.php @@ -112,7 +112,7 @@ class SplClassLoader { spl_autoload_unregister(array($this, 'loadClass')); } - + /** * Checks if the given class file really exists. * @@ -121,13 +121,15 @@ class SplClassLoader */ public function classFileExists($classFile) { - if ( file_exists($classFile) ) { + if (file_exists($classFile)) { return true; } - - foreach (explode(PATH_SEPARATOR,get_include_path()) as $path) { - if ( $path === '.' ) { continue; } - if ( file_exists($path.DIRECTORY_SEPARATOR.$classFile) ) { + + foreach (explode(PATH_SEPARATOR, get_include_path()) as $path) { + if ($path === '.') { + continue; + } + if (file_exists($path . DIRECTORY_SEPARATOR . $classFile)) { return true; } } @@ -156,7 +158,7 @@ class SplClassLoader $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->_fileExtension; $classFile = ($this->_includePath !== null ? $this->_includePath . DIRECTORY_SEPARATOR : '') . $fileName; - if ( $this->classFileExists($classFile) ) { + if ($this->classFileExists($classFile)) { require $classFile; } } diff --git a/tests/library/Respect/Validation/ValidatorTest.php b/tests/library/Respect/Validation/ValidatorTest.php index 83e8a50f..2e482ba3 100644 --- a/tests/library/Respect/Validation/ValidatorTest.php +++ b/tests/library/Respect/Validation/ValidatorTest.php @@ -219,22 +219,53 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase public function testReadme() { - $username = 'really messed up screen#name'; - $validUsername = v::alnum('_') + $number = 123; + v::numeric()->validate($number); //true + //From 1 to 15 non-whitespace alphanumeric characters + $validUsername = v::alnum() ->noWhitespace() ->length(1, 15); + + $validUsername->validate('alganet'); //true + $validUser = v::attribute('username', $validUsername) //reusing! + ->attribute('birthdate', v::date('Y-m-d')); + + $user = new \stdClass; + $user->username = 'alganet'; + $user->birthdate = '1987-07-01'; + + $validUser->validate($user); //true + + + $validUsername->validate('respect'); //true + $validUsername->validate('alexandre gaigalas'); //false + $validUsername->validate('#$%'); //false + + try { - $validUsername->assert($username); - } catch (\Exception $e) { - //echo $e->getFullMessage(); - } - $user = array("id" => "some %% invalid %% id"); - $post = array("user" => $user); - try { - v::key("user", v::key("id", v::int()->positive()))->assert($post); + $validUsername->assert('really messed up screen#name'); } catch (\InvalidArgumentException $e) { - //echo $e->findRelated('user', 'id', 'positive')->getMainMessage(); + echo $e->getFullMessage(); } + + $validBlogPost = v::object() + ->attribute('title', v::string()->length(1, 32)) + ->attribute('author', $validUser) //reuse! + ->attribute('date', v::date()) + ->attribute('text', v::string()); + + $blogPost = new \stdClass; + $blogPost->author = clone $validUser; + $blogPost->author->username = '# invalid #'; + + try { + $validBlogPost->assert($blogPost); + } catch (\InvalidArgumentException $e) { + echo $e->findRelated('author', 'username', 'noWhitespace')->getMainMessage(); + } + + $validHostName = v::zend('hostname')->assert('google.com'); + $validTime = v::sf('time')->assert('22:00:01'); } }