https://github.com/czproject/type-system
https://github.com/czproject/type-system
Last synced: 6 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/czproject/type-system
- Owner: czproject
- License: other
- Created: 2024-11-15T10:44:22.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-08-22T10:45:04.000Z (11 months ago)
- Last Synced: 2025-10-10T02:25:00.437Z (10 months ago)
- Language: PHP
- Size: 13.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- Funding: .github/funding.yml
- License: license.md
Awesome Lists containing this project
README
# CzProject\TypeSystem
[](https://github.com/czproject/type-system/actions)
[](https://packagist.org/packages/czproject/type-system)
[](https://github.com/czproject/type-system/releases)
[](https://github.com/czproject/type-system/blob/master/license.md)
Abstraction for PHP type system.
## 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/