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

https://github.com/czproject/type-system


https://github.com/czproject/type-system

Last synced: 6 months ago
JSON representation

Awesome Lists containing this project

README

          

# CzProject\TypeSystem

[![Build Status](https://github.com/czproject/type-system/workflows/Build/badge.svg)](https://github.com/czproject/type-system/actions)
[![Downloads this Month](https://img.shields.io/packagist/dm/czproject/type-system.svg)](https://packagist.org/packages/czproject/type-system)
[![Latest Stable Version](https://poser.pugx.org/czproject/type-system/v/stable)](https://github.com/czproject/type-system/releases)
[![License](https://img.shields.io/badge/license-New%20BSD-blue.svg)](https://github.com/czproject/type-system/blob/master/license.md)

Abstraction for PHP type system.

Donate

## Installation

[Download a latest package](https://github.com/czproject/type-system/releases) or use [Composer](http://getcomposer.org/):

```
composer require czproject/type-system
```

CzProject\TypeSystem requires PHP 7.4 or later.

## Usage

``` php
use CzProject\TypeSystem\Types;

Types::scalar();
Types::integer();
Types::float();
Types::string();
Types::nonEmptyString();
Types::bool();
Types::true();
Types::false();
Types::null();
Types::object(\DateTimeImmutable::class);
Types::union(Types::string(), Types::integer(), Types::bool());
Types::nullable(Types::string());
Types::listOf(Types::string());
```

All types implement interface `Type`.

### Casting

```php
use CzProject\TypeSystem\Types;

$castedValue = Types::scalar()->castValue($value);
$castedValue = Types::integer()->castValue($value);
$castedValue = Types::float()->castValue($value);
$castedValue = Types::string()->castValue($value);
$castedValue = Types::nonEmptyString()->castValue($value);
$castedValue = Types::bool()->castValue($value);
$castedValue = Types::true()->castValue($value);
$castedValue = Types::false()->castValue($value);
$castedValue = Types::null()->castValue($value);
$castedValue = Types::object(\DateTimeImmutable::class)->castValue($value);
$castedValue = Types::union(Types::string(), Types::integer(), Types::bool())->castValue($value);
$castedValue = Types::nullable(Types::string())->castValue($value);
$castedValue = Types::listOf(Types::string())->castValue($value);
```

Or you can use simplified casting helper:

```php
use CzProject\TypeSystem\Cast;

$castedValue = Cast::scalar($value);
$castedValue = Cast::integer($value);
$castedValue = Cast::integerOrNull($value);
$castedValue = Cast::float($value);
$castedValue = Cast::floatOrNull($value);
$castedValue = Cast::string($value);
$castedValue = Cast::stringOrNull($value);
$castedValue = Cast::nonEmptyString($value);
$castedValue = Cast::nonEmptyStringOrNull($value);
$castedValue = Cast::bool($value);
$castedValue = Cast::boolOrNull($value);
```

Every type throws `SorryNonConvertableValue` exception for non convertable values.

```php
try {
Types::string()->castValue($value); // or Cast::string($value)

} catch (CzProject\TypeSystem\SorryNonConvertableValue $e) {
var_dump($e->getValue());
var_dump($e->getType());
}
```

### Value validation

Checks if value is exactly of specific type.

```php
use CzProject\TypeSystem\Types;

$isValid = Types::scalar()->isValueValid($value);
$isValid = Types::integer()->isValueValid($value);
$isValid = Types::float()->isValueValid($value);
$isValid = Types::string()->isValueValid($value);
$isValid = Types::nonEmptyString()->isValueValid($value);
$isValid = Types::bool()->isValueValid($value);
$isValid = Types::true()->isValueValid($value);
$isValid = Types::false()->isValueValid($value);
$isValid = Types::null()->isValueValid($value);
$isValid = Types::object(\DateTimeImmutable::class)->isValueValid($value);
$isValid = Types::union(Types::string(), Types::integer(), Types::bool())->isValueValid($value);
$isValid = Types::nullable(Types::string())->isValueValid($value);
$isValid = Types::listOf(Types::string())->isValueValid($value);
```

------------------------------

License: [New BSD License](license.md)

Author: Jan Pecha, https://www.janpecha.cz/