Skip to content

Commit

Permalink
Merge pull request #42 from GoteoFoundation/feat/mapper-improvements
Browse files Browse the repository at this point in the history
[feat] Mapper improvements
  • Loading branch information
subiabre authored Dec 24, 2024
2 parents e6b32c7 + 9ab5167 commit 029f825
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 15 additions & 2 deletions src/Mapping/AutoMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,31 @@

class AutoMapper implements AutoMapperInterface
{
public const CACHE_DIR = 'automapper';

public const DEFAULT_CONTEXT = [
'skip_null_values' => true,
];

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,
);
}

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);
}
}

0 comments on commit 029f825

Please sign in to comment.