stan-angeloff / guard
Safe-conducts for your PHP. Kills up to 99.9% of bad arguments.
Installs: 5
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/stan-angeloff/guard
Requires
- php: >=5.4
Requires (Dev)
- phpunit/phpunit: 3.8.*@dev
- satooshi/php-coveralls: dev-master
This package is not auto-updated.
Last update: 2020-05-15 16:47:31 UTC
README
Safe-conducts for your PHP. Kills up to 99.9% of bad arguments.
WIP.
function add($a, $b)
{
\Guard\Conditions::requires($a, 'a')->isNumeric();
\Guard\Conditions::requires($b, 'b')->isNumeric();
return $a + $b;
}
add(1, 2);
add('string', 2);
# => \Guard\Exception\ConditionEvaluationException(
# 'The condition "isNumeric" for argument "a" failed to evaluate.',
# \Guard\Exception\InvalidArgumentException(
# 'The value "\'string\'" is not numeric.'
# )
# )
Installing using Composer
# Run this in your terminal to get the latest Composer version: $ curl -sS https://getcomposer.org/installer | php # ...or if you don't have cURL: $ php -r "eval('?>'.file_get_contents('https://getcomposer.org/installer'));" # Add Guard as a dependency: $ php composer.phar require 'stan-angeloff/guard'
Usage
After installing, you need to require Composer's autoloader file:
require 'vendor/autoload.php';
To enforce conditions on your arguments, use:
\Guard\Conditions::requires($value, $argumentName)
->condition1()
->condition2('option, e.g., interface name');
where $value is the argument value and $argumentName is the argument name (see example above). Conditions can be chained.
Conditions
-
instanceOf($className)Throws an exception if the value is not an object or an instance of the expected
$className. -
isNotNull()Throws an exception if the value is
NULL(empty values are allowed, e.g., an empty string''). -
isNumeric()Throws an exception if the value is not numeric.
-
typeOf($internalType)Throws an exception if the type of the value is not the expected one.
See gettype for a full list of possible types.