mf/type-validator

TypeValidator 用于断言值类型

4.1.0 2022-01-14 13:44 UTC

This package is auto-updated.

Last update: 2024-09-14 19:31:17 UTC


README

Latest Stable Version Tests and linting Coverage Status Total Downloads License

TypeValidator 用于断言值类型

目录

要求

  • PHP 8.0

安装

composer require mf/type-validator

使用

$validator = new TypeValidator(
    TypeValidator::TYPE_STRING,
    TypeValidator::TYPE_INT,
    [TypeValidator::TYPE_STRING],
    [TypeValidator::INT]
);

$validator->assertKeyType('string - value');
$validator->assertValueType(1);

$validator->assertValueType('invalid value type');  // throws InvalidArgumentException

使用自定义异常

$validator = new TypeValidator(
    TypeValidator::TYPE_STRING,
    TypeValidator::TYPE_INT,
    [TypeValidator::TYPE_STRING],
    [TypeValidator::INT],
    App\MyCustomException::class
);

$validator->assertKeyType('string - value');
$validator->assertValueType(1);

$validator->assertValueType('invalid value type');  // throws App\MyCustomException