PHP code example of czproject / type-system
1. Go to this page and download the library: Download czproject/type-system library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
czproject / type-system example snippets
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);
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);
try {
Types::string()->castValue($value); // or Cast::string($value)
} catch (CzProject\TypeSystem\SorryNonConvertableValue $e) {
var_dump($e->getValue());
var_dump($e->getType());
}
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);