tomwright/validator

There is no license information available for the latest version (1.0.9) of this package.

Quick and easy validation library.

This package's canonical repository appears to be gone and the package has been frozen as a result.

1.0.9 2017-02-07 11:01 UTC

This package is not auto-updated.

Last update: 2019-04-19 15:46:52 UTC


README

Build Status Total Downloads Latest Stable Version Latest Unstable Version License

A simple Validator library to save on tonnes of if... else blocks.

Quick Usage

This is a quick example on how to check if a user is 18 or not.

$userOver18 = ValidatorTranslator::create('User Age', 17)
    ->isInt(18)
    ->validate()
    ->hasPassed();

if (! $userOver18) {
    echo 'User is not over 18.';
}

For a full list of Constraints see below.

Constraints

Every Constraint has a static create() method available to you, which can be used to create a new instance of that Constraint. You can then call the available filters on it.

ArrayConstraint

Used to determine if a variable is an array or not.

BoolConstraint

Used to determine if a variable is a bool or not.

Filters

  • requiresTrue()
  • requiresFalse()

EmailConstraint

Used to determine whether or not a variable is a valid email address.

FloatConstraint

Used to determine if a variable is a float or not.

Filters

  • setMinValue()
  • setMaxValue()

IntConstraint

Used to determine if a variable is a int or not.

Filters

  • setMinValue()
  • setMaxValue()

NotNullConstraint

Used to determine if a variable is NOT null or not.

NullConstraint

Used to determine if a variable is null or not.

NumericConstraint

Used to determine if a variable is numeric or not.

Filters

  • setMinValue()
  • setMaxValue()

ObjectConstraint

Used to determine if a variable is an object or not.

Filters

  • setRequiredClass - used to specify a class that is needed to pass.

StringConstraint

Used to determine if a variable is a string or not.

Filters

  • setMinLength
  • setMaxLength

ConstraintGroups

ConstraintGroups allow you to use groups of Constraints to validate a variable. In order for a ConstraintGroup to pass, all of its Constraints have to pass.

To add a Constraint to a ConstraintGroup you use $constraintGroup->addConstraint($constraint));.

There is another method of adding Constraints as soon as you create a ConstraintGroup though...

$constraintGroup = ConstraintGroup::create([
    StringConstraint::create()->setMinLength(5),
    EmailConstraint::create(),
]);

ConstraintGroupTranslator

Creating all of these ConstraintGroups can take quite a bit of typing, so to help with that the ConstraintGroupTranslater was created.

The following code achieves exactly the same as the above and can be used in the same way due to the fact that ConstraintGroupTranslater extends ConstraintGroup.

$constraintGroup = ConstraintGroupTranslater::create()->isString(5)->isEmail();

Available Methods

  • isArray() - Ensures the variable is an array
  • isBool($requiredValue = null) - Ensures the variable is a boolean
  • isTrue() - Ensures the variable is a boolean and is true
  • isFalse() - Ensures the variable is a boolean and is false
  • isEmail() - Ensures the variable is a valid email address
  • isUrl() - Ensures the variable is a valid url
  • isInt($minValue = null, $maxValue = null) - Ensures the variable is an int and between $minValue and $maxValue
  • isFloat($minValue = null, $maxValue = null) - Ensures the variable is a float and between $minValue and $maxValue
  • isNumeric($minValue = null, $maxValue = null) - Ensures the variable is numeric and between $minValue and $maxValue
  • notNull() - Ensures the variable is NOT null
  • isNull() - Ensures the variable is null
  • isObject($requiredClass = null) - Ensures the variable is an object of type $requiredClass
  • isString($minLength = null, $maxLength = null) - Ensures the variable is a string with a length between $minLength and $maxLength

Validator

A Validator is made up of 1 or more ConstraintGroups. In order for the Validator to pass, at least 1 of the ConstraintGroups need to pass.

You should create your ConstraintGroups as detailed above, and then add them to the Validator by doing the following.

$validator = Validator::create($validatorName, $valueToValidate, $throwExceptionsOnFailure = false)
    ->addConstraintGroup($constraintGroup);

When you have all of the ConstraintGroups set up, just call validate() on the Validator.

Available Methods

  • validate() - Processes the validation
  • getRequirements() - Returns an array of requirements, defined by the ConstraintGroups
  • getRequirementsString() - Returns a string of requirements, defined by the ConstraintGroups
  • addConstraintGroup() - Adds a ConstraintGroup to the Validator
  • hasPassed() - Returns whether or not the validation passed
  • hasFailed() - Returns whether or not the validation failed

ValidatorTranslator

To simplify things even further you have the ValidatorTranslator.

$valid = ValidatorTranslator::create('age', 23)
    ->inList([1, 2, 3])
    ->alt()
    ->inList([21, 22, 23])
    ->validate()
    ->hasPassed();

Will give the same result as doing:

$groupOne = ConstraintGroupTranslator::create()
    ->inList([1, 2, 3]);
$groupTwo = ConstraintGroupTranslator::create()
    ->inList([21, 22, 23]);
$valid = Validator::create('age', 23)
    ->addConstraintGroup($groupOne)
    ->addConstraintGroup($groupTwo)
    ->validate()
    ->hasPassed();

The methods available to you here will typically be the same as when using the ConstraintGroupTranslator.

Using alt() or alternatively() will effectively say "I am done with this ConstraintGroup. Create me a new one.".