Laravel integration of the TypeLang mapper:
the service provider registers the mappers in the container and configures
them from config/type-lang-mapper.php.
Full documentation is available at typelang.dev.
Install the package via Composer:
composer require type-lang/mapper-laravelThe service provider is discovered automatically. Publish the config to change the settings:
php artisan vendor:publish --tag=type-lang-mapper-configRequirements:
- PHP 8.1+
- Laravel 10+
The default mapper is injected by MapperInterface, DenormalizerInterface
or NormalizerInterface:
use TypeLang\Mapper\MapperInterface;
final class CreateUserController
{
public function __invoke(Request $req, MapperInterface $mapper): Response
{
$user = $mapper->denormalize($req->all(), UserRequestDTO::class);
// ...
}
}A named mapper takes the settings of the default one and overrides some of them:
// config/type-lang-mapper.php
return [
'naming' => 'snake_case',
'types' => [App\Mapper\UuidTypeBuilder::class],
'mappers' => [
'api' => [
'strict' => true,
'types' => [App\Mapper\MoneyTypeBuilder::class],
],
],
];app('type-lang.mapper')->denormalize('42', 'int');
// 42
app('type-lang.mapper.api')->denormalize('42', 'int');
// throws InvalidValueException: $: Value must be of type int, but "42" given