ValidatorChain
A library to perform several validations in a chain., (*1)
Installation
Install ValidationChain with Composer, (*2)
Create or update your composer.json, (*3)
{
"require": {
"pklink/validation-chain": "0.*"
}
}
And run Composer, (*4)
php composer.phar install
Finally include Composers autoloader, (*5)
include __DIR__ . '/vendor/autoload.php';
Basic Usage
Create an instance of Chain with the value you like to check, (*6)
$chain = \Validation\Chain('check me');
Now, run some tests in a chain, (*7)
$chain
->isString() // return instance of Chain
->isInteger() // return instance of Chain
->isArray() // return instance of Chain
;
If you want to know if all validations run fine use the isValid-method, (*8)
$chain->isValid(); // return false
Alternatively you can use this in one statement:, (*9)
(new \Validation\Chain('check me'))
->isString()
->isInteger()
->isArray()
->isValid()
Reset chain
Use reset() to reset your chain., (*10)
$chain = new \Validator\Chain('value');
$chain->isInteger()->isString()->isValid(); // returns false
$chain->isValid(); // returns false
$chain->isString()->isValid(); // returns false
$chain->reset()->isString()->isValid(); // returns true
Options
You can set different options with instantiation or the setter for the appropriate option., (*11)
$chain = new \Validator\Chain('value', ['option' => 'value']);
throwExceptionOnFailure
If this option is set to true then Chain will throw a Validator\Exception if a validation failed., (*12)
Default is set to false, (*13)
$chain = new \Validator\Chain('value', ['throwExceptionOnFailure' => true]);
try {
$chain
->isString() // everything fine
->minimumLengthOf(2) // everything fine
->isArray() // \Validator\Exception will be thrown
->isArray(); // will not perform
} catch (\Validator\Excetion $e) {
echo 'validation failed!';
}
The setter for this option is throwExceptionOnFailure(), (*14)
$chain->throwExceptionOnFailure(true);
$chain->throwExceptionOnFailure(false);
$chain->throwExceptionOnFailure(); // set this option to true
stopValidationOnFailure
If this option is set to true then Chain will not perform any further validation., (*15)
Default is set to true, (*16)
$chain = new \Validator\Chain('value', ['stopValidationOnFailure' => true]);
$chain
->isString() // everthing fine
->isArray() // validation fail
->isInteger(); // will not perform
The setter for this option is stopValidationOnFailure(), (*17)
$chain->stopValidationOnFailure(true);
$chain->stopValidationOnFailure(false);
$chain->stopValidationOnFailure(); // set this option to true
Listener for failures
You can add Closures to get notifications on failures., (*18)
$chain = new \Validator\Chain('value');
$chain->addValidationFailureListener(function() {
echo 'failure';
});
$chain->isInteger();
This example will output: failure, (*19)
If you like you can use the failed \Validator\Rule in your listener, (*20)
$chain->addValidationFailureListener(function(\Validation\Rule $rule) {
echo get_class($rule);
});
Rules
hasKey()
hasKeys()
isArray()
isInteger()
isInt()
Alias for isInteger(), (*21)
isNull()
isNull()
isNumeric()
isObject()
isScalar()
isString()
lengthOf( int $length )
maximumLengthOf( int $length )
minumumLengthOf( int $length )
Add your own rule
Run Tests
You can use PHPUnit from the vendor-folder., (*22)
php composer.phar install --dev
php vendor/bin/phpunit tests/
or with code-coverage-report, (*23)
php composer.phar install --dev
php vendor/bin/phpunit --coverage-html output tests/
License
This package is licensed under the MIT License. See the LICENSE file for details., (*24)