mascame/katina

基于传入规则的测试助手类,用于验证任何类型的输出。适用于测试API响应或随时间变化的数据。

v2.0.0 2016-07-08 11:58 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:25:39 UTC


README

Latest Version Travis Software License

测试助手类,用于根据传入的规则验证任何类型的输出。适用于测试API响应或随时间变化的数据。

ptrofimov/matchmaker包的现代版本。

安装

composer require mascame/katina --dev

基本用法

$data = [
    'name' => 'John Doe',
    'count' => 123,
];

$validator = new \Mascame\Katina\Validator(['name' => ':string', 'count' => ':int']);

$validator->check($data); // true

高级用法

$data = [
    'name' => 'John Doe',
    'count' => 145,
    'list' => [
        'emptyList' => false,
        'nested-list' => [
            1, 2, 3
        ]
    ],
    'books' => [
        [
            'type' => 'book',
            'title' => 'Geography book',
            'chapters' => [
                'eu' => ['title' => 'Europe', 'interesting' => true],
                'as' => ['title' => 'America', 'interesting' => false]
            ]
        ],
        [
            'type' => 'book',
            'title' => 'Foreign languages book',
            'chapters' => [
                'de' => ['title' => 'Deutsch']
            ]
        ]
    ]
];

$requiredFields = [
    'name' => ':string',
    'count' => ':int',
    'list' => [
        'emptyList' => ':bool',
        'nested-list' => [
            ':int'
        ]
    ],
    'books' => [
        '*' => [
            'type' => 'book',
            'title' => ':string contains(book)',
            'chapters' => [
                ':string length(2) {1,3}' => [
                    'title' => ':string',
                    'interesting?' => ':bool',
                ]
            ]
        ]
    ]
];

$optionalFields = [
    'something' => 'boolean'
];

$validator = new \Mascame\Katina\Validator($requiredFields, $optionalFields);

$validator->check($data); // true

添加规则

$data = [
    'my-birthday' => '1980-01-01'
];

$requiredFields = ['my-birthday' => ':birthdayValidator'];

$validator = new \Mascame\Katina\Validator($requiredFields);

// You can add or override rules
\Mascame\Katina\Rules::setRules(['birthdayValidator' => function($value) {
    return ($value == '1980-01-01');
}]);

$validator->check($data); // true

匹配规则

匹配规则是字符串,以':'开头。可以使用空格连接多个匹配器。

匹配器可以是任何可调用的函数(函数名或闭包)。可以添加自己的规则或替换标准规则。

  • 通用

  • empty

  • nonempty

  • required

  • in(a, b, ...)

  • mixed

  • any

  • 类型

  • array

  • bool

  • boolean

  • callable

  • double

  • float

  • int

  • integer

  • long

  • numeric

  • number

  • object

  • real

  • resource

  • scalar

  • string

  • 数字

  • gt(n)

  • gte(n)

  • lt(n)

  • lte(n)

  • negative

  • positive

  • between(a, b)

  • 字符串

  • alnum

  • alpha

  • cntrl

  • digit

  • graph

  • lower

  • print

  • punct

  • space

  • upper

  • xdigit

  • regexp(pattern)

  • email

  • url

  • ip

  • length(n)

  • min(n)

  • max(n)

  • contains(needle)

  • starts(s)

  • ends(s)

  • json

  • date

  • 数组

  • count(n)

  • keys(key1, key2, ...)

  • 对象

  • instance(class)

  • property(name, value)

  • method(name, value)

更多信息请点击此处 click here

键的量词

  • ! - 必须有一个键(默认值)
  • ? - 可选键
      • 任何数量的键
  • {3} - 键的严格数量
  • {1,5} - 范围

对于匹配器(例如:':string'),默认量词是*

作为独立使用匹配器

可以直接使用匹配器

\Mascame\Katina\Matcher::matches(['test' => true], ['test' => ':bool']); // true

贡献

感谢您考虑贡献!您可以在任何时间通过fork项目并提交pull request来贡献。

支持

如果您需要帮助或任何形式的支持,请发送电子邮件至Marc Mascarell,邮箱为marcmascarell@gmail.com

许可证

感谢Petr Trofimov为其包ptrofimov/matchmaker做出的贡献,本包大量基于该包。

MIT