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 XML documents. Returns true for well-formed XML strings and DOMDocument objects.

Documentation

Code

<?php

declare(strict_types=1);

use function Flow\Types\DSL\type_xml;

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

$validXml = '<?xml version="1.0"?><root><item>value</item></root>';
$invalidXml = '<root><unclosed>';

echo 'Is valid XML valid? ' . (type_xml()->isValid($validXml) ? 'yes' : 'no') . "\n";
echo 'Is invalid XML valid? ' . (type_xml()->isValid($invalidXml) ? 'yes' : 'no') . "\n";
echo 'Is DOMDocument valid? ' . (type_xml()->isValid(new \DOMDocument()) ? 'yes' : 'no') . "\n";
echo 'Is empty string valid? ' . (type_xml()->isValid('') ? 'yes' : 'no') . "\n";
Contributors

Built in the open.

Join us on GitHub
scroll back to top