Better README, fixed bugs on AllOf and AlphaException

This commit is contained in:
Alexandre 2011-02-07 12:22:30 -02:00
parent 58c16154e4
commit 822fd269f8
6 changed files with 100 additions and 58 deletions

View file

@ -16,80 +16,89 @@ Quick Reference
Namespace import
----------------
<?php
use Respect\Validation\Validator as v;
Simple validation
-----------------
v::numeric()->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
------------------------------------

View file

@ -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;
}
}

View file

@ -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;
}

View file

@ -11,7 +11,7 @@
<active>yes</active>
</lead>
<date>2011-02-07</date>
<time>08:52:08</time>
<time>12:22:20</time>
<version>
<release>0.1.0</release>
<api>0.1.0</api>
@ -30,7 +30,7 @@ First Version
<file baseinstalldir="Respect/Validation" md5sum="0224a04fb381c031061f45f181210d2a" name="Exceptions/AbstractRelatedException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="880d68d6eaebb1c83959059d8670ae6e" name="Exceptions/AllOfException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="66e980dca43faa593338b7201eacd585" name="Exceptions/AlnumException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="84fd2eb26a09ec7fe5e89922b8ee37f9" name="Exceptions/AlphaException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="ef45bc934e3a6577b98e5afcc737cca9" name="Exceptions/AlphaException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="ce14ae85b017b59c0e2bd095e94d92db" name="Exceptions/ArrException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="f251baefd5069adf990a9846b0b1a896" name="Exceptions/AtLeastException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="bb280e31c71ca0d24577d85bc7c17af2" name="Exceptions/AttributeException.php" role="php" />
@ -70,7 +70,7 @@ First Version
<file baseinstalldir="Respect/Validation" md5sum="2753b5b068425432e786c4e30df464fe" name="Rules/AbstractComposite.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="07aa083d1394817149cf4a6db5ea629f" name="Rules/AbstractRelated.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="cc6a9b9c7c6c43ab1daef9a8f67f67ca" name="Rules/AbstractRule.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="1ac46f8a36089242442a4a82799716b8" name="Rules/AllOf.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="6a1e115625f44ff71bfe39fa198e40e3" name="Rules/AllOf.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="a431d0ef3c154cb2c5fe7a07bbd56acf" name="Rules/Alnum.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="c74366a999c22fa514abd1a5d8ace61c" name="Rules/Alpha.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="25f54c2c1f3d756afb759261c164f8b6" name="Rules/Arr.php" role="php" />

View file

@ -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;
}
}

View file

@ -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');
}
}