Skip to content
This repository has been archived by the owner on Mar 26, 2020. It is now read-only.
/ php-guard Public archive

Safe-conducts for your PHP. Kills up to 99.9% of bad arguments.

Notifications You must be signed in to change notification settings

StanAngeloff/php-guard

Repository files navigation

Guard

Build Status Coverage Status

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.


LICENSE.AGPL-3.0

About

Safe-conducts for your PHP. Kills up to 99.9% of bad arguments.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages