mini\convert() Function

Documentation

Convert a value to a target type

Uses the converter registry to find an appropriate converter for the given input and target type. Throws if no suitable converter is found.

The converter system uses reflection-based type matching to find the most specific converter. Resolution order: direct single-type converter, union type converter, parent class converters, interface converters.

Examples:

// Convert exception to error response
$response = convert($exception, ResponseInterface::class);

// Convert array to JSON response
$response = convert(['data' => 'value'], ResponseInterface::class);

// Convert string to text response
$response = convert("Hello World", ResponseInterface::class);

// Custom domain objects
$response = convert($userModel, ResponseInterface::class);

Register converters in bootstrap.php:

$registry = Mini::$mini->get(ConverterRegistryInterface::class);
$registry->register(function(MyModel $m): ResponseInterface { ... });

Signature

function convert(mixed $input, string $targetType): mixed

Parameters

Name Type Default Description
$input mixed required Documentation missing
$targetType string required Documentation missing

Returns

mixed

Source

src/Converter/functions.php:71-74