An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

TypeValidator
=============

[![Latest Stable Version](https://img.shields.io/packagist/v/mf/type-validator.svg)](https://packagist.org/packages/mf/type-validator)
[![Tests and linting](https://github.com/MortalFlesh/TypeValidator/actions/workflows/tests.yaml/badge.svg)](https://github.com/MortalFlesh/TypeValidator/actions/workflows/tests.yaml)
[![Coverage Status](https://coveralls.io/repos/github/MortalFlesh/TypeValidator/badge.svg?branch=master)](https://coveralls.io/github/MortalFlesh/TypeValidator?branch=master)
[![Total Downloads](https://img.shields.io/packagist/dt/mf/type-validator.svg)](https://packagist.org/packages/mf/type-validator)
[![License](https://img.shields.io/packagist/l/mf/type-validator.svg)](https://packagist.org/packages/mf/type-validator)

TypeValidator for asserting types of values

## Table of Contents
- [Requirements](#requirements)
- [Installation](#installation)
- [Usage](#usage)

## Requirements
- PHP 8.0

## 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
```