https://github.com/mortalflesh/typevalidator
TypeValidator for asserting types of values
https://github.com/mortalflesh/typevalidator
asserting-types generic-types generics typevalidator
Last synced: 6 months ago
JSON representation
TypeValidator for asserting types of values
- Host: GitHub
- URL: https://github.com/mortalflesh/typevalidator
- Owner: MortalFlesh
- License: mit
- Created: 2016-09-04T18:08:18.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2022-01-14T13:44:43.000Z (about 4 years ago)
- Last Synced: 2025-07-07T22:12:25.707Z (7 months ago)
- Topics: asserting-types, generic-types, generics, typevalidator
- Language: PHP
- Size: 41 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
TypeValidator
=============
[](https://packagist.org/packages/mf/type-validator)
[](https://github.com/MortalFlesh/TypeValidator/actions/workflows/tests.yaml)
[](https://coveralls.io/github/MortalFlesh/TypeValidator?branch=master)
[](https://packagist.org/packages/mf/type-validator)
[](https://packagist.org/packages/mf/type-validator)
TypeValidator for asserting types of values
## Table of Contents
- [Requirements](#requirements)
- [Installation](#installation)
- [Usage](#usage)
## Installation:
```
composer require mf/type-validator
```
## Usage
```php
$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
```
### With Custom Exception
```php
$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
```