Skip to content

Commit

Permalink
feat: add alias for MapperBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
romm committed May 27, 2024
1 parent 626f72d commit 23e8c68
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,33 @@ services:

</details>

---

For more granular control, a `MapperBuilder` instance can be injected instead.

```php
use CuyZ\Valinor\Mapper\MapperBuilder;

final class SomeAutowiredService
{
public function __construct(
private MapperBuilder $mapperBuilder,
) {}

public function someMethod(): void
{
$this->mapperBuilder
// …
// Some mapper configuration
// …
->mapper()
->map(SomeDto::class, /* … */);

// …
}
}
````

## Bundle configuration

Global configuration for the bundle can be done in a package configuration file…
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
// @formatter:off
$container->services()
->alias(TreeMapper::class, 'valinor.tree_mapper')
->alias(MapperBuilder::class, 'valinor.mapper_builder')

->set('valinor.tree_mapper', TreeMapper::class)
->factory([service('valinor.mapper_builder'), 'mapper'])
Expand Down
2 changes: 2 additions & 0 deletions tests/App/src/Mapper/MapperContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace CuyZ\ValinorBundle\Tests\App\Mapper;

use CuyZ\Valinor\Mapper\TreeMapper;
use CuyZ\Valinor\MapperBuilder;
use CuyZ\ValinorBundle\Configurator\Attributes\AllowPermissiveTypes;
use CuyZ\ValinorBundle\Configurator\Attributes\AllowSuperfluousKeys;
use CuyZ\ValinorBundle\Configurator\Attributes\EnableFlexibleCasting;
Expand All @@ -14,6 +15,7 @@
final class MapperContainer
{
public function __construct(
public MapperBuilder $mapperBuilder,
public TreeMapper $defaultMapper,
#[EnableFlexibleCasting]
public TreeMapper $mapperWithFlexibleCasting,
Expand Down
18 changes: 18 additions & 0 deletions tests/Integration/MapperBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace CuyZ\ValinorBundle\Tests\Integration;

final class MapperBuilderTest extends IntegrationTestCase
{
public function test_mapper_builder_is_injected_and_works_properly(): void
{
$result = $this->mapperContainer()
->mapperBuilder
->mapper()
->map('string', 'foo');

self::assertSame('foo', $result);
}
}

0 comments on commit 23e8c68

Please sign in to comment.