yalesov/arg-validator

验证函数参数,扩展PHP的类型提示功能。

v2.1.1 2016-07-06 12:16 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:36:42 UTC


README

Build Status

解决PHP中多个函数参数验证/类型提示问题

原始类型提示

function foo(string $foo) {}

混合/高级类型提示

function foo(array|string|null $foo, int/*between 3-10*/ $bar) {}

"X的数组"类型提示

function foo(Array_of_strings $foo) {}

通过数组成员验证/类型提示"伪命名函数参数"

/**
 * @param array $params
 *  - 'foo' => string
 *  - 'bar' => int
 */
function foo(array $params) {}

在类中声明所需的常量(接口风格函数)

class Foo {
  /* const BAR required */
  /* const BAZ required */
}

安装

Composer:

{
  "require": {
    "yalesov/arg-validator": "2.*"
  }
}

使用方法

简单的参数验证

验证$foo是否为整数,如果验证失败则抛出InvalidArgumentException

use Yalesov\ArgValidator\ArgValidator;

function foo($foo)
{
  ArgValidator::assert($foo, 'int');
  // throw InvalidArgumentException if $foo is not int
  // do something
}

验证$foo是否为整数,返回布尔值。

use Yalesov\ArgValidator\ArgValidator;

function foo($foo)
{
  $result = ArgValidator::assert($foo, 'int', false);
  // $result = false if invalid
  // do something
}

完整函数签名

public static function assert($arg, $checks, $exception = true)

通过$checks参数指定有效参数类型。如果$arg满足指定的任何一项检查,则认为它是有效的。

  • 标志
    • arrayof:将检查剩余指定类型的数组,而不是纯类型,例如array('arrayOf', 'string', 'int') = 字符串数组,或整型数组。注意:空数组将被视为有效
    • minmax
      • intfloat结合:最小和最大值
      • string结合:最小和最大长度
      • array结合:最小和最大计数
  • 类型
    • int
    • float
    • numeric
    • string
    • array
    • null
    • callable
    • bool
    • resource
    • notEmpty:相当于!empty()
    • (用于in_array检查的标量数组),例如array('foo', 'bar')将检查in_array($arg, array('foo', 'bar'))注意:ArgValidator::assert($arg, array('foo', 'bar'))将被解释为针对foobar的instanceof检查。要指定in_array检查,将其包裹在另一个数组中:ArgValidator::assert($arg, array(array('foo', 'bar')))
    • (一个字符串):假定是instanceof检查,应该是类/接口的完全限定名称

"命名参数"验证

use Yalesov\ArgValidator\ArgValidator;

function foo(array $params)
{
  ArgValidator::arrayAssert($params, array(
    'foo' => 'float',
    'bar' => array('string', 'notSet'),
    'baz' => array('int', 'string', 'min' => 2),
  ));
  // $params['foo'] should be a float
  // $params['bar'] should be a string, if set, or not set at all
  // $params['baz'] can be either an int (>=2), or a string (strlen >= 2)
}

完整函数签名

public static function arrayAssert(array $arg, array $checks, $exception = true)

有效的参数类型与上述相同,但增加了notSet类型。

检查类常量

namespace Foo;

use Yalesov\ArgValidator\ArgValidator;

class FooClass {
  const FOO = 'foo';
  const BAR = 2;
}

ArgValidator::assertClassConstant('Foo\FooClass', array('FOO', 'BAR'));
// \Foo\FooClass must have the constants 'FOO' and 'BAR' set

完整函数签名

public static function assertClassConstant($className, $constants, $exception = true)

$className应该是一个完全限定的类名;$constants应该是一个字符串数组,每个成员都是一个常量名称。

ArgValidator::assertClassConstant()将检查以下内容:

  1. $className存在
  2. 类已声明了在$constants中指定的所需常量

其他

为了集中处理参数验证的异常处理,ArgValidator还提供了两个辅助函数

断言类存在,如果不存在则抛出InvalidArgumentException

public static function assertClass($className, $exception = true)

抛出一个关于给定变量名称未设置的InvalidArgumentException

public static function throwIssetException($argName)

注意:此函数实际上并不执行isset检查。我找不到一种方法来抽象isset检查,而不在最初设置变量(以便将其作为调用此函数的参数)。