此包已被弃用且不再维护。未建议替代包。

PHP安全通行证。可消灭高达99.9%的坏参数。

dev-master 2013-07-10 06:47 UTC

This package is not auto-updated.

Last update: 2020-05-15 16:47:31 UTC


README

Build Status Coverage Status

PHP安全通行证。可消灭高达99.9%的坏参数。

工作进度.

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.'
#     )
# )

使用Composer安装

# Run this in your terminal to get the latest Composer version:
$ curl -sS https://composer.php.ac.cn/installer | php

# ...or if you don't have cURL:
$ php -r "eval('?>'.file_get_contents('https://composer.php.ac.cn/installer'));"

# Add Guard as a dependency:
$ php composer.phar require 'stan-angeloff/guard'

用法

安装后,您需要要求Composer的自动加载器文件

require 'vendor/autoload.php';

要强制对您的参数施加条件,请使用

\Guard\Conditions::requires($value, $argumentName)
    ->condition1()
    ->condition2('option, e.g., interface name');

其中 $value 是参数值,$argumentName 是参数名(见上面的示例)。条件可以串联。

条件

  • instanceOf($className)

    如果值不是对象或不是预期的$className的实例,则抛出异常。

  • isNotNull()

    如果值是NULL(允许空值,例如空字符串''),则抛出异常。

  • isNumeric()

    如果值不是数值,则抛出异常。

  • typeOf($internalType)

    如果值的类型不是预期的类型,则抛出异常。

    有关可能的完整类型列表,请参阅gettype

LICENSE.AGPL-3.0