Skip to content

About

Symfony Bundle for Type Lang Mapper

Resources

Code of conduct

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

PHP 8.1+ Latest Stable Version Latest Unstable Version License MIT


Symfony integration of the TypeLang mapper: the bundle registers the mappers as services and configures them from config/packages/type_lang_mapper.yaml.

Full documentation is available at typelang.dev.

Installation

Install the package via Composer:

composer require type-lang/mapper-symfony

With Symfony Flex the bundle is enabled automatically. Otherwise add it to config/bundles.php:

return [
    // ...
    TypeLang\Mapper\Bridge\Symfony\TypeLangMapperBundle::class => [
        'all' => true,
    ],
];

Requirements:

  • PHP 8.1+
  • Symfony LTS 6.4+

Usage

The default mapper is autowired by MapperInterface, DenormalizerInterface or NormalizerInterface:

use TypeLang\Mapper\DenormalizerInterface;

final class CreateUserController
{
    public function __construct(
        private readonly DenormalizerInterface $mapper,
    ) {}

    public function __invoke(Request $request): Response
    {
        $user = $this->mapper->denormalize(
            value: $request->toArray(),
            type: UserRequestDTO::class,
        );
        // ...
    }
}

A named mapper takes the settings of the default one and overrides some of them:

# config/packages/type_lang_mapper.yaml
type_lang_mapper:
  naming: snake_case
  types:
    - App\Mapper\UuidTypeBuilder
  mappers:
    api:
      strict: true
      types:
        - App\Mapper\MoneyTypeBuilder

It is passed by its service id, the interfaces are always autowired to the default mapper:

use Symfony\Component\DependencyInjection\Attribute\Autowire;

final class ImportController
{
    public function __construct(
        // type_lang_mapper.mapper: snake_case
        private readonly DenormalizerInterface $mapper,
        // type_lang_mapper.mapper.api: snake_case, strict
        #[Autowire(service: 'type_lang_mapper.mapper.api')]
        private readonly DenormalizerInterface $apiMapper,
    ) {}
}

An extension is listed in the config or marked with an attribute:

use TypeLang\Mapper\Bridge\Symfony\Attribute\AsMapperTypeBuilder;

// the default mapper and all named ones
#[AsMapperTypeBuilder]
final class UuidTypeBuilder implements TypeBuilderInterface {}

// the "api" mapper only
#[AsMapperTypeBuilder(mappers: ['api'], priority: 10)]
final class MoneyTypeBuilder implements TypeBuilderInterface {}

About

Symfony Bundle for Type Lang Mapper

Resources

Code of conduct

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages