Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add alias for MapperBuilder #21

Merged
merged 4 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .github/workflows/quality-assurance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ jobs:
- name: Warming up cache
run: php tests/App/bin/console cache:warmup --env=test

- name: TODO TMP
run: |
pwd
ls -al var/cache/test
ls -al var/cache/test/default

- name: Running PHPStan
run: php vendor/bin/phpstan

Expand Down
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}
],
"require": {
"php": "~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0",
"php": "~8.1.0 || ~8.2.0 || ~8.3.0",
"cuyz/valinor": "^1.0",
"symfony/config": "^5.4 || ^6.4 || ^7.0",
"symfony/dependency-injection": "^5.4 || ^6.4 || ^7.0",
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
15 changes: 14 additions & 1 deletion tests/App/src/Cache/CacheSpy.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use function array_filter;
use function count;
use function method_exists;

final class CacheSpy implements CacheInterface
{
Expand All @@ -25,7 +26,19 @@ public function hasCachedClassDefinition(string $className): bool
{
$definitions = array_filter(
$this->calls,
static fn (mixed $value): bool => $value instanceof ClassDefinition && $value->name() === $className
static function (mixed $value) use ($className): bool {
if (! $value instanceof ClassDefinition) {
return false;
}

// @phpstan-ignore-next-line
if (method_exists($value, 'name')) {
return $value->name() === $className;
}

// @phpstan-ignore-next-line
return $value->name === $className;
}
);

return count($definitions) > 0;
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);
}
}
Loading