TypeValidator for asserting types of values
- 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