tomwright / validator
Quick and easy validation library.
This package's canonical repository appears to be gone and the package has been frozen as a result. Email us for help if needed.
Installs: 498
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/tomwright/validator
Requires
README
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
setMinLengthsetMaxLength
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 anarrayisBool($requiredValue = null)- Ensures the variable is abooleanisTrue()- Ensures the variable is abooleanand istrueisFalse()- Ensures the variable is abooleanand isfalseisEmail()- Ensures the variable is a valid email addressisUrl()- Ensures the variable is a valid urlisInt($minValue = null, $maxValue = null)- Ensures the variable is anintand between$minValueand$maxValueisFloat($minValue = null, $maxValue = null)- Ensures the variable is afloatand between$minValueand$maxValueisNumeric($minValue = null, $maxValue = null)- Ensures the variable isnumericand between$minValueand$maxValuenotNull()- Ensures the variable isNOT nullisNull()- Ensures the variable isnullisObject($requiredClass = null)- Ensures the variable is an object of type$requiredClassisString($minLength = null, $maxLength = null)- Ensures the variable is astringwith a length between$minLengthand$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 validationgetRequirements()- Returns an array of requirements, defined by theConstraintGroupsgetRequirementsString()- Returns a string of requirements, defined by theConstraintGroupsaddConstraintGroup()- Adds aConstraintGroupto theValidatorhasPassed()- Returns whether or not the validation passedhasFailed()- 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.".