Validation Closures
, (*1)
Closures useful for validating data. Provides type validation amongst other filters., (*2)
Installation
You can install this class via composer
:, (*3)
composer require yoshi2889/validation-closures
, (*4)
Usage
All closures are exposed as public static methods. For example, to use the Types\string() closure:, (*5)
$closure = \ValidationClosures\Types::string();
$is_string = $closure('This is a string');
// True
echo $is_string ? 'True' : 'False';
In the following documentation, all methods return a value of type boolean
unless stated otherwise., (*6)
Ranges
The Ranges class contains methods to check if values are within a range. It contains the following methods:, (*7)
-
stringWithLengthBetween(int $minimumLength, int $maximumLength)
: Test if a given string has a length within the range
$minimumLength <= length <= $maximumLength
-
intBetween(int $minimum, int $maximum)
: Test if a given int is inside the range of $minimum <= int <= $maximum
-
intBetweenExclusive(int $minimum, int $maximum)
: Test if a given int is inside the range of $minimum < int < $maximum
-
floatBetween(float $minimum, float $maximum)
: Identical to intBetween
, except it tests on floats.
-
floatBetweenExclusive(float $minimum, float $maximum)
: Identical to intBetweenExclusive
, except it tests on floats.
-
enum(...$allowedValues)
: Test if a given value exists inside $allowedValues
, similar to an Enum type in other languages.
-
typeEnum(...$allowedTypes)
: Test if a given value is of a type inside $allowedTypes
.
-
stringOneOf(...$allowedValues)
: Test if a given string exists inside $allowedValues
.
Reflection
The Reflection class allows you to create closures out of all methods found in PHP's ReflectionClass.
It is most useful with the is*
methods. For example, to create a closure for the implementsInterface method:, (*8)
$closure = Reflection::implementsInterface(stdClass::class);
The Reflection class will take care of instantiating a ReflectionClass
object automatically., (*9)
Types
The Types class contains methods to use for type validation. It contains the following methods:, (*10)
-
string()
: Test if a given value is a string.
-
int()
: Test if a given value is an integer.
-
float()
: Test if a given value is a float.
-
boolean()
: Test if a given value is a boolean.
-
array()
: Test if a given value is an array.
-
callable()
: Test if a given value is a callable function/method.
-
object()
: Test if a given value is an instantiated object.
-
numeric()
: Test if a given value is a numeric value. (see is_numeric in the PHP manual for details)
-
instanceof(string $class)
: Test if a given value is an instance of the given class.
Utils
The Utils class contains methods to manipulate closures. It contains the following methods:, (*11)
-
invert(\Closure $closure): \Closure
: Invert a closure. For example, Types::string()
inverted would pass all values which are not strings.
-
merge(\Closure $closure1, \Closure $closure2): \Closure
: Merge two closures together. The resulting closure will return true if either closure
or both closures resolve(s) to true.
-
both(\Closure $closure1, \Closure $closure2): \Closure
: Merge two closures together. The resulting closure will return true only if both
closures resolve to true.
-
validateArray(\Closure $closure, array $values): bool
: Tests if all values in a given array validate with the given closure.
Returns false if 1 or more values do not validate, returns true if all elements validate.
License
This code is released under the MIT License. Please see LICENSE
to read it., (*12)