mini\Converter\ConverterRegistryInterface::register()
Method
public
abstract
Signature
public abstract function register(mini\Converter\ConverterInterface|Closure $converter, ?string $targetName = NULL, ?string $sourceName = NULL): void
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$converter |
mini\Converter\ConverterInterface|Closure |
required | Documentation missing |
$targetName |
?string |
NULL
|
Documentation missing |
$sourceName |
?string |
NULL
|
Documentation missing |
Returns
void
Documentation
Register a converter
Accepts either a ConverterInterface implementation or a typed closure. Closures are automatically wrapped in ClosureConverter.
Closure requirements:
- Exactly one typed parameter (may be union type: string|array)
- Typed return value (single type only, no unions or null) - unless $targetName is specified
- No null in input or output types
Examples:
// Simple converter
$registry->register(function(string $text): ResponseInterface {
return new Response(200, ['Content-Type' => 'text/plain'], $text);
});
// Union input type
$registry->register(function(string|array $data): ResponseInterface {
if (is_string($data)) {
return new Response(200, ['Content-Type' => 'text/plain'], $data);
}
$json = json_encode($data);
return new Response(200, ['Content-Type' => 'application/json'], $json);
});
// Named target (bypasses return type validation for closures)
$registry->register(fn(\BackedEnum $e) => $e->value, 'sql-value');
// Named source and target (for bidirectional conversions like database values)
$registry->register(fn(string $s): \DateTimeImmutable => new \DateTimeImmutable($s), null, 'sql-value');