2017 © Pedro Peláez
 

library guard

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

image

stan-angeloff/guard

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

  • Wednesday, July 10, 2013
  • by StanAngeloff
  • Repository
  • 1 Watchers
  • 2 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Guard

Build Status Coverage Status, (*1)

Safe-conducts for your PHP. Kills up to 99.9% of bad arguments., (*2)

WIP., (*3)

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:, (*4)

require 'vendor/autoload.php';

To enforce conditions on your arguments, use:, (*5)

\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., (*6)

Conditions

  • instanceOf($className), (*7)

    Throws an exception if the value is not an object or an instance of the expected $className., (*8)

  • isNotNull(), (*9)

    Throws an exception if the value is NULL (empty values are allowed, e.g., an empty string '')., (*10)

  • isNumeric(), (*11)

    Throws an exception if the value is not numeric., (*12)

  • typeOf($internalType), (*13)

    Throws an exception if the type of the value is not the expected one., (*14)

    See gettype for a full list of possible types., (*15)


LICENSE.AGPL-3.0, (*16)

The Versions

10/07 2013

dev-master

9999999-dev

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

  Sources   Download

AGPLv3

The Requires

  • php >=5.4

 

The Development Requires

guard conditions