Skip to content
Search
Examples

Types

Description

Type casting converts values from one type to another. The cast() method transforms the input value into the target
type, throwing InvalidArgumentException if conversion is impossible. Unlike assertions which validate existing types,
casting actively transforms values (e.g., string "123" becomes integer 123, or "yes" becomes boolean true).

Casting is ideal for normalizing data from external sources like CSV files, API responses, or user input.


Cast UUID strings to the Uuid value object. Normalizes case and provides safe UUID handling and comparison.

Documentation

Code

<?php

declare(strict_types=1);

use function Flow\Types\DSL\type_uuid;

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

echo 'Lowercase: ' . type_uuid()->cast('550e8400-e29b-41d4-a716-446655440000')->toString() . "\n";
echo 'Uppercase: ' . type_uuid()->cast('6BA7B810-9DAD-11D1-80B4-00C04FD430C8')->toString() . "\n";
Contributors

Built in the open.

Join us on GitHub
scroll back to top