phramework/validate

phramework 的验证库


README

phramework 的验证库 https://phramework.github.io/validate/

Coverage Status Build Status StyleCI Stories in Ready

使用方法

使用 composer 安装包

composer require phramework/validate

解析一个整数值

require './vendor/autoload.php';

use \Phramework\Validate\IntegerValidator;

$validator = new IntegerValidator(-1, 1);

$value = $validator->parse('0');

var_dump($value);

上面的示例将输出

int(0)

解析字符串对象

$personalInformationValidator = new ObjectValidator(
    (object) [
        'name' => new StringValidator(2, 30),
        'city' => new StringValidator(2, 30),
        'age' => new IntegerValidator(1, 200),
    ],
    ['name', 'city', 'age'], //required properties
    false //no additional properties allowed
);

$personalInformation = $validationModel->parse((object) [
    'name' => 'Jane Doe',
    'city' => 'Athens',
    'age' => 28
]);

print_r($personalInformation);

上面的示例将输出

stdClass Object
(
    [name] => Jane Doe
    [city] => Athens,
    [age] => 28
)

验证枚举字符串数组

    /*
     * A validator that allows you to pick one or two colors between blue, green and red
     */
    $colorsValidator = new ArrayValidator(
        1, //minItems
        2, //maxItems
        (new StringValidator()) //items
            ->setEnum([
                'blue',
                'green',
                'red',
            ]),
        true //unique items
    );

    /*
     * $parsedOneItem will be validated successfully
     */
    $parsedOneItem = $colorsValidator->parse(['blue']); //will be [blue]


    /*
     * $parsedTwoItems will be validated successfully
     */
    $parsedTwoItems = $colorsValidator->parse(['blue', 'red']); //will be [blue, red]

    /*
     * $resultOfZeroItemsStatus cannot be validated true the validator requires minItems of 1
     */
    $resultOfZeroItemsStatus = $colorsValidator->validate([]);
    $resultOfZeroItemsStatus->getStatus(); // will be false because validation failed
    /** @var \Phramework\Exceptions\IncorrectParameterException $exception in this case */
    $exception = $resultOfZeroItemsStatus->getException();
    $exception->getFailure(); // will be minItems

    /*
     * $resultOfIncorrectItemsStatus cannot be validated true because "yellow" is not an allowed item
     */
    $resultOfIncorrectItemsStatus = $colorsValidator->validate(['yellow']);
    $resultOfIncorrectItemsStatus->getStatus(); // will be false because validation failed
    /** @var \Phramework\Exceptions\IncorrectParameterException $exception in this case */
    $exception = $resultOfIncorrectItemsStatus->getException();
    $exception->getFailure(); // will be items

    /*
     * Following will throw \Phramework\Exceptions\IncorrectParameterException
     * with failure maxItems because validator requires maxItems 2
     */
    $colorsValidator
        ->parse([
            'blue',
            'green',
            'red'
        ]);

查看 wiki 获取更多示例。

开发

安装依赖

composer update

测试和代码风格检查

composer test
composer lint

生成文档

composer doc

许可证

版权 2015-2019 Xenofon Spafaridis

根据 Apache 许可证 2.0 版(“许可证”)授权;除非适用法律要求或书面同意,否则不得使用此文件,除非遵守许可证。您可以在以下位置获取许可证副本:

https://apache.ac.cn/licenses/LICENSE-2.0

除非适用法律要求或书面同意,否则在许可证下分发的软件按“原样”提供,不提供任何明示或暗示的保证或条件。有关许可证的具体语言、权限和限制,请参阅许可证。