yoshi2889/validation-closures
最新稳定版本:v0.2
Composer 安装命令:
composer require yoshi2889/validation-closures
包简介
Closures useful for validating data. Provides type validation amongst other filters.
README 文档
README
Closures useful for validating data. Provides type validation amongst other filters.
Installation
You can install this class via composer:
composer require yoshi2889/validation-closures
Usage
All closures are exposed as public static methods. For example, to use the Types\string() closure:
$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.
Ranges
The Ranges class contains methods to check if values are within a range. It contains the following methods:
stringWithLengthBetween(int $minimumLength, int $maximumLength): Test if a given string has a length within the range$minimumLength <= length <= $maximumLengthintBetween(int $minimum, int $maximum): Test if a given int is inside the range of$minimum <= int <= $maximumintBetweenExclusive(int $minimum, int $maximum): Test if a given int is inside the range of$minimum < int < $maximumfloatBetween(float $minimum, float $maximum): Identical tointBetween, except it tests on floats.floatBetweenExclusive(float $minimum, float $maximum): Identical tointBetweenExclusive, 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:
$closure = Reflection::implementsInterface(stdClass::class);
The Reflection class will take care of instantiating a ReflectionClass object automatically.
Types
The Types class contains methods to use for type validation. It contains the following methods:
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:
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.
统计信息
- 总下载量: 2.63k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 6
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2017-06-28