2017 © Pedro Peláez
 

library validator-chain

Perform validation in a chain

image

pklink/validator-chain

Perform validation in a chain

  • Wednesday, January 20, 2016
  • by pklink
  • Repository
  • 1 Watchers
  • 0 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

ValidatorChain Build Status

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)

The Versions

20/01 2016

dev-master

9999999-dev

Perform validation in a chain

  Sources   Download

MIT

The Requires

 

The Development Requires

validation chain

20/01 2016

1.0.0

1.0.0.0

Perform validation in a chain

  Sources   Download

MIT

The Requires

 

The Development Requires

validation chain

04/12 2014

0.2.1

0.2.1.0

Perform validation in a chain

  Sources   Download

MIT

The Requires

 

The Development Requires

validation chain

17/03 2013

0.2

0.2.0.0

Perform validation in a chain

  Sources   Download

MIT

The Requires

 

The Development Requires

validation chain

15/03 2013

0.1

0.1.0.0

Perform validation in a chain

  Sources   Download

MIT

The Requires

 

The Development Requires

validation chain