Download the PHP package yalesov/arg-validator without Composer
On this page you can find all versions of the php package yalesov/arg-validator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download yalesov/arg-validator
More information about yalesov/arg-validator
Files in yalesov/arg-validator
Package arg-validator
Short Description Validate function arguments, extending typehinting capabilities of PHP.
License ISC
Homepage https://github.com/yalesov/php-arg-validator
Informations about the package arg-validator
Yalesov\ArgValidator
Solving several function argument validation/typehinting issues in PHP:
Primitive typehints:
Mixed / advanced typehints:
"array of X" typehints:
Validating / typehinting "pseudo named function paramters" through array members:
Declaring required constants in classes (interface-style function):
Installation
Usage
Simple argument validation
Validates that $foo is an integer, throwing an InvalidArgumentException if validation failed.
Validates that $foo is an integer, return boolean.
Full function signature:
Valid argument types are specified through the $checks parameter. A string, or an array of the following accepted. $arg will be considered valid if it satisfies any one of the specified checks.
- Flags
arrayof: will check for an array of remaining specified types, instead of plain types, e.g.array('arrayOf', 'string', 'int')= an array of string, or an array of int. Note: Empty array will be considered validmin,max- combine with
int,float: min and max value - combine with
string: min and max length - combine with
array: min and max count
- Types
intfloatnumericstringarraynullcallableboolresourcenotEmpty: equivalent to!empty()- (an array of scalars for in_array check), e.g.
array('foo', 'bar')will check forin_array($arg, array('foo', 'bar')). Note:ArgValidator::assert($arg, array('foo', 'bar'))will be interpreted as instanceof checks againstfooandbar. To specify an in_array check, wrap it in another array:ArgValidator::assert($arg, array(array('foo', 'bar'))). - (a string): assumed to be an instanceof check, should be a fully-qualified name of Class/Interface
"Named parameters" validation
Full function signature:
Valid argument types are same as above, except with the addition of a notSet type.
Check class constants
Full function signature:
$className should be a fully-qualified class name; $constants should be an array of strings, each member being the constant name.
ArgValidator::assertClassConstant() will check for:
- the class
$classNameexists - the class has declared the required constants specified in
$constants
Misc
To centralize exception handling on argument validations, ArgValidator also provides two helper functions:
Assert that a class exists, throw InvalidArgumentException otherwise:
Throw an InvalidArgumentException about the given variable name is not set:
Note: this function doesn't actually perform the isset check. I can't find a way to abstract the isset check away, without the variable being set in the first place (in order to act as argument to call this function with).