Skip to content
Search
Examples

Types

Description

Type validation checks if a value matches the expected type without throwing exceptions. The isValid() method returns true if the value conforms to the type, false otherwise. This is useful for conditional logic, filtering data, or performing pre-flight checks before processing.

Unlike assertions, validation never throws - it's designed for control flow decisions rather than enforcing contracts.


Validate values matching all of multiple types. Returns true only if the value satisfies every type in the intersection.

Documentation

Code

<?php

declare(strict_types=1);

use function Flow\Types\DSL\type_instance_of;
use function Flow\Types\DSL\type_intersection;

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

$type = type_intersection(
    type_instance_of(\Countable::class),
    type_instance_of(\IteratorAggregate::class)
);

echo 'Is ArrayObject valid? ' . ($type->isValid(new \ArrayObject([1, 2, 3])) ? 'yes' : 'no') . "\n";
echo 'Is simple array valid? ' . ($type->isValid([1, 2, 3]) ? 'yes' : 'no') . "\n";
echo 'Is stdClass valid? ' . ($type->isValid(new \stdClass()) ? 'yes' : 'no') . "\n";
Contributors

Built in the open.

Join us on GitHub
scroll back to top