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 various date formats to DateTimeImmutable. Supports ISO strings, Unix timestamps, and relative expressions like "tomorrow".

Documentation

Code

<?php

declare(strict_types=1);

use function Flow\Types\DSL\type_datetime;

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

echo 'From string: ' . type_datetime()->cast('2024-06-15')->format('Y-m-d') . "\n";
echo 'From datetime string: ' . type_datetime()->cast('2024-12-25 10:30:00')->format('Y-m-d H:i:s') . "\n";
echo 'From timestamp: ' . type_datetime()->cast(1704067200)->format('Y-m-d H:i:s') . "\n";
Contributors

Built in the open.

Join us on GitHub
scroll back to top