Abstraction for PHP type system.
Download a latest package or use Composer:
composer require czproject/type-system
CzProject\TypeSystem requires PHP 7.4 or later.
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
.
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:
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.
try {
Types::string()->castValue($value); // or Cast::string($value)
} catch (CzProject\TypeSystem\SorryNonConvertableValue $e) {
var_dump($e->getValue());
var_dump($e->getType());
}
Checks if value is exactly of specific type.
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
Author: Jan Pecha, https://www.janpecha.cz/