phpexperts/datatype-validator

易于使用的数据类型验证器(严格和模糊)。

v1.5.2 2019-07-30 18:18 UTC

This package is auto-updated.

Last update: 2024-08-29 05:13:57 UTC


README

TravisCI Maintainability Test Coverage

DataTypeValidator 是 PHP Experts, Inc. 设计的一个易于使用的数据类型验证器项目。

它支持传统、模糊和严格的 PHP 数据类型(例如,"1.0" 可以是浮点数、整数和字符串),以及严格的数据类型验证('1' 仅是字符串,1.0 仅是浮点数等)。

安装

通过 Composer

composer require phpexperts/datatype-validator

使用方法

// 1. Pick a Type Checker (IsAFuzzyDataType or IsAStrictDataType).
//    * IsAFuzzyDataType tries its best to emulate PHP's `==`.
//    * IsAStrictDataType observes PHP's `strict_types=1` rules.
    $validator = new DataValidator(new IsAStrictDataType());

// There are two powerful mechanisms out of the box:
// 2. It is easy to validate any data type dynamically, without a ton of if statements.

    $validator->isType('asdf', 'string'); // true or false.
    $validator->assertIsType(1, 'int'); // null or throws InvalidDataTypeException

// 3. You can also validate arrays:

    $data = [
        'name'     => 'Cheyenne',
        'age'      => 22,
        'birthday' => Carbon::parse('1996-12-04 15:15:15'),
        'daysOld'  => 8194.35,
        'today'    => Carbon::now(),
        'sayHi'    => function () { return 'Hi!'; },
    ];
    
    $rules = [
         'name'     => 'string',
         'age'      => 'int',
         'birthday' => 'Carbon',
         'daysOld'  => 'float',
         'today'    => 'Carbon\Carbon',
         'sayHi'    => 'callable',
     ];

    $validator->validate($data, $rules);

// 4. DataValidator::validate() will return `true` on success or throw 
//    an `InvalidDataTypeException` that contains an array of errors:

    $data = [
        'name'     => 'Cheyenne',
        'age'      => '22',
        'birthday' => '1996-12-04 15:15:15',
    ];
    
    try {
        $validator->validate($data, $rules);
    } catch (InvalidDataTypeException $e) {
        print_r($e->getReasons());

        /* Output:
        array:2 [
          0 => "age is not a valid int"
          1 => "birthday is not a valid Carbon"
        ]
        */
    }

// 5. It can validate objects based on their short name or full name.
    $data = [
        'yesterday' => \Carbon\Carbon::parse('2019-05-11'),
        'tomorrow'  => \Carbon\Carbon::parse('2019-05-13'),
    ];
    
    $validator->validate($data, [
        'yesterday' => '\Carbon\Carbon',
        'tomorrow'  => 'Carbon',
    ]);

基准测试

phpbench run --report=aggregate

用例

PHPExperts\DataTypeValidator\DataTypeValidator
✔ 可以批量验证数据数组
✔ 会返回数据验证逻辑的名称
✔ 会返回包含解释的无效键数组
✔ 会静默忽略不在规则中的数据
✔ 会静默忽略无数据的可空规则
✔ 默认情况下数据不能为空
✔ 任何以 '?' 开头的数据类型都是可空的
✔ 任何以 '[]' 结尾的数据类型都是 X 的数组
✔ 允许空数组
✔ 允许可空数组
✔ 如果给出非字符串规则,则会抛出逻辑异常

PHPExperts\DataTypeValidator\DataTypeValidator: 断言
✔ 断言值是布尔类型
✔ 断言值是整数
✔ 断言值是浮点数
✔ 断言值是字符串
✔ 断言值是数组
✔ 断言值是对象
✔ 断言值是可调用的
✔ 断言值是资源
✔ 断言某个数组
✔ 通过简称断言对象
✔ 通过全名断言对象

PHPExperts\DataTypeValidator\DataTypeValidator: 数据类型检查
✔ 严格验证布尔类型
✔ 严格验证整数
✔ 严格验证浮点数
✔ 严格验证字符串
✔ 严格验证数组
✔ 验证对象
✔ 验证可调用
✔ 验证资源
✔ 通过简称验证对象
✔ 通过全名验证对象
✔ 可以松散验证布尔类型
✔ 可以松散验证整数
✔ 可以松散验证浮点数
✔ 可以松散验证字符串
✔ 可以松散验证数组
✔ 可以验证某个数组的数组

PHPExperts\DataTypeValidator\IsAFuzzyDataType
✔ 对于有效值返回 true
✔ 对于无效值返回 false
✔ 匹配简称类
✔ 匹配特定类
✔ 与某个数组的数组一起工作

PHPExperts\DataTypeValidator\IsAStrictDataType
✔ 对于有效值返回 true
✔ 对于无效值返回 false
✔ 匹配简称类
✔ 匹配特定类
✔ 与某个数组的数组一起工作

测试

phpunit

贡献者

Theodore R. Smith theodore@phpexperts.pro
GPG 指纹:4BF8 2613 1C34 87AC D28F 2AD8 EB24 A91D D612 5690
CEO:PHP Experts, Inc.

许可

MIT 许可证。有关更多信息,请参阅许可文件