Skip to content

Commit

Permalink
Make auto mapper skip null values by default
Browse files Browse the repository at this point in the history
  • Loading branch information
subiabre committed Dec 13, 2024
1 parent e6b32c7 commit e3230c6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/Mapping/AutoMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

class AutoMapper implements AutoMapperInterface
{
public const SKIP_NULL_VALUES = 'skip_null_values';

private AutoMapperInterface $innerMapper;

public function __construct(
Expand All @@ -19,6 +21,11 @@ public function __construct(

public function map(array|object $source, string|array|object $target, array $context = []): array|object|null
{
$context = [
self::SKIP_NULL_VALUES => true,
...$context
];

return $this->innerMapper->map($source, $target, $context);
}
}
2 changes: 1 addition & 1 deletion src/State/ApiResourceStateProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
public function getEntity(mixed $data, Options $options): object
{
/** @var object */
$entity = $this->autoMapper->map($data, $options->getEntityClass(), ['skip_null_values' => true]);
$entity = $this->autoMapper->map($data, $options->getEntityClass());

return $entity;
}
Expand Down
2 changes: 1 addition & 1 deletion src/State/Project/ProjectStateProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = [])
{
/** @var Project */
$project = $this->autoMapper->map($data, Project::class, ['skip_null_values' => true]);
$project = $this->autoMapper->map($data, Project::class);

if (!isset($data->id)) {
$user = $this->security->getUser();
Expand Down

0 comments on commit e3230c6

Please sign in to comment.