Skip to content
Search
Examples

Types

Description

Type assertions validate values at runtime and throw InvalidArgumentException if the value doesn't match the expected type.
The assert() method returns the validated value with proper type narrowing, enabling static analysis tools like
PHPStan to understand the resulting type. This provides IDE autocompletion and catches type errors during analysis.

<?php

$value = takeFromUser(); // $value is type mixed here

$stringValue = type_string()->assert($value); 

// $valueString is type string here, it's called types narrowing 

Read more about types narrowing


Assert UUID strings and convert to the Uuid value object. Validates RFC 4122 format and provides safe UUID handling.

Documentation

Code

<?php

declare(strict_types=1);

use function Flow\Types\DSL\type_uuid;
use Flow\Types\Value\Uuid;

require __DIR__ . '/vendor/autoload.php';

echo 'UUID: ' . type_uuid()->assert(new Uuid('550e8400-e29b-41d4-a716-446655440000'))->toString() . "\n";
echo 'Uppercase: ' . type_uuid()->assert(new Uuid('550E8400-E29B-41D4-A716-446655440000'))->toString() . "\n";
Contributors

Built in the open.

Join us on GitHub
scroll back to top