Skip to content

Commit

Permalink
Merge pull request #37 from rekalogika:chore/simplify-remembering-mapper
Browse files Browse the repository at this point in the history
chore: Simplify remembering mapper.
  • Loading branch information
priyadi committed Feb 21, 2024
2 parents 99012f7 + 11fa0fb commit ff5ecdb
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* perf(`ObjectToObjectTransformer`): Prevent delegating to `MainTransformer` if
the current value in a dynamic property is a scalar.
* feat(`PresetMappingFactory`): Add `fromObjectCache()` and `fromObjectCacheReversed()`.
* chore: Simplify remembering mapper.

## 1.0.0

Expand Down
16 changes: 3 additions & 13 deletions tests/Fixtures/RememberingMapper/RememberingMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace Rekalogika\Mapper\Tests\Fixtures\RememberingMapper;

use Rekalogika\Mapper\Context\Context;
use Rekalogika\Mapper\Exception\UnexpectedValueException;
use Rekalogika\Mapper\MapperInterface;
use Rekalogika\Mapper\ObjectCache\ObjectCacheFactoryInterface;
use Rekalogika\Mapper\Transformer\Context\PresetMapping;
Expand All @@ -37,29 +36,20 @@ public function reset(): void
$this->presetMapping = new PresetMapping();
}

/** @psalm-suppress InvalidReturnType */
public function map(object $source, object|string $target, ?Context $context = null): object
{
$objectCache = $this->objectCacheFactory->createObjectCache();

if ($context === null) {
$context = Context::create();
}

$context ??= Context::create();
$context = $context->with($objectCache, $this->presetMapping);

$result = $this->decorated->map($source, $target, $context);

if (is_object($target)) {
$target = $target::class;
}

if (!$result instanceof $target) {
throw new UnexpectedValueException(sprintf('Expected instance of "%s", got "%s"', $target, get_class($result)));
}

$newPresetMapping = PresetMappingFactory::fromObjectCacheReversed($objectCache);
$this->presetMapping->mergeFrom($newPresetMapping);

/** @psalm-suppress InvalidReturnStatement */
return $result;
}
}

0 comments on commit ff5ecdb

Please sign in to comment.