From 34a28851c15949706383130549107d3e2e5cd510 Mon Sep 17 00:00:00 2001 From: Daniel Subiabre Date: Tue, 24 Dec 2024 10:47:51 +0100 Subject: [PATCH 1/3] Specify cache directory for auto mapper --- config/services.yaml | 1 + src/Mapping/AutoMapper.php | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/config/services.yaml b/config/services.yaml index 654922d..8a6bc90 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -69,6 +69,7 @@ services: App\Mapping\AutoMapper: arguments: + - '%kernel.cache_dir%' - !tagged 'app.mapping.map_provider' # add more service definitions when explicit configuration is needed diff --git a/src/Mapping/AutoMapper.php b/src/Mapping/AutoMapper.php index 42bcf0b..cf186b7 100644 --- a/src/Mapping/AutoMapper.php +++ b/src/Mapping/AutoMapper.php @@ -7,13 +7,17 @@ class AutoMapper implements AutoMapperInterface { + public const CACHE_DIR = 'automapper'; + private AutoMapperInterface $innerMapper; public function __construct( - iterable $mapProviders, + ?string $cacheDirectory = null, + iterable $mapProviders = [], ) { $this->innerMapper = InnerMapper::create( - providers: $mapProviders + cacheDirectory: \sprintf('%s%s%s', $cacheDirectory, \DIRECTORY_SEPARATOR, self::CACHE_DIR), + providers: $mapProviders, ); } From ee31334a741344821305afd606362c6bd253cf92 Mon Sep 17 00:00:00 2001 From: Daniel Subiabre Date: Tue, 24 Dec 2024 10:50:13 +0100 Subject: [PATCH 2/3] Add default mapping context --- src/Mapping/AutoMapper.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Mapping/AutoMapper.php b/src/Mapping/AutoMapper.php index cf186b7..09a5c3c 100644 --- a/src/Mapping/AutoMapper.php +++ b/src/Mapping/AutoMapper.php @@ -9,6 +9,10 @@ class AutoMapper implements AutoMapperInterface { public const CACHE_DIR = 'automapper'; + public const DEFAULT_CONTEXT = [ + 'skip_null_values' => true + ]; + private AutoMapperInterface $innerMapper; public function __construct( @@ -23,6 +27,11 @@ public function __construct( public function map(array|object $source, string|array|object $target, array $context = []): array|object|null { + $context = [ + ...self::DEFAULT_CONTEXT, + ...$context, + ]; + return $this->innerMapper->map($source, $target, $context); } } From 9ab5167902a2ac6baa17625310faece4783a4d40 Mon Sep 17 00:00:00 2001 From: Daniel Subiabre Date: Tue, 24 Dec 2024 10:55:28 +0100 Subject: [PATCH 3/3] Add missing trailing comma to default context array --- src/Mapping/AutoMapper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Mapping/AutoMapper.php b/src/Mapping/AutoMapper.php index 09a5c3c..c9c10fc 100644 --- a/src/Mapping/AutoMapper.php +++ b/src/Mapping/AutoMapper.php @@ -10,7 +10,7 @@ class AutoMapper implements AutoMapperInterface public const CACHE_DIR = 'automapper'; public const DEFAULT_CONTEXT = [ - 'skip_null_values' => true + 'skip_null_values' => true, ]; private AutoMapperInterface $innerMapper;