Skip to content

Commit

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

## 1.0.0

Expand Down
26 changes: 26 additions & 0 deletions src/Transformer/Context/PresetMappingFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,32 @@ public static function fromObjectCache(ObjectCache $objectCache): PresetMapping
/** @var SplObjectStorageWrapper<object,\ArrayObject<class-string,object>> */
$presetMapping = new SplObjectStorageWrapper(new \SplObjectStorage());

/**
* @var object $source
* @var \ArrayObject<class-string,object> $classToTargetMapping
*/
foreach ($objectCacheWeakMap as $source => $classToTargetMapping) {
foreach ($classToTargetMapping as $targetClass => $target) {
if (!$presetMapping->offsetExists($source)) {
/** @var \ArrayObject<class-string,object> */
$arrayObject = new \ArrayObject();
$presetMapping->offsetSet($source, $arrayObject);
}

$presetMapping->offsetGet($source)?->offsetSet($targetClass, $target);
}
}

return new PresetMapping($presetMapping);
}

public static function fromObjectCacheReversed(ObjectCache $objectCache): PresetMapping
{
$objectCacheWeakMap = $objectCache->getInternalMapping();

/** @var SplObjectStorageWrapper<object,\ArrayObject<class-string,object>> */
$presetMapping = new SplObjectStorageWrapper(new \SplObjectStorage());

/**
* @var object $source
* @var \ArrayObject<class-string,object> $classToTargetMapping
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/RememberingMapper/RememberingMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function map(object $source, object|string $target, ?Context $context = n
throw new UnexpectedValueException(sprintf('Expected instance of "%s", got "%s"', $target, get_class($result)));
}

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

return $result;
Expand Down
23 changes: 20 additions & 3 deletions tests/IntegrationTest/PresetMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,30 @@ public function testFromObjectCache(): void
$objectCache = $this->createObjectCache();

$source = new ObjectWithScalarProperties();
$targetType = TypeFactory::objectOfClass(ObjectWithScalarProperties::class);
$targetType = TypeFactory::objectOfClass(ObjectWithScalarPropertiesDto::class);
$target = new ObjectWithScalarPropertiesDto();

$objectCache->saveTarget($source, $targetType, $target);

$presetMapping = PresetMappingFactory::fromObjectCache($objectCache);

$result = $presetMapping->findResult($source, ObjectWithScalarPropertiesDto::class);

$this->assertSame($target, $result);
}

public function testFromObjectCacheReversed(): void
{
$objectCache = $this->createObjectCache();

$source = new ObjectWithScalarProperties();
$targetType = TypeFactory::objectOfClass(ObjectWithScalarProperties::class);
$target = new ObjectWithScalarPropertiesDto();

$objectCache->saveTarget($source, $targetType, $target);

$presetMapping = PresetMappingFactory::fromObjectCacheReversed($objectCache);

$result = $presetMapping->findResult($target, $source::class);

$this->assertSame($source, $result);
Expand All @@ -58,15 +75,15 @@ public function testMerge(): void

$objectCache->saveTarget($source, $targetType, $target);

$presetMapping = PresetMappingFactory::fromObjectCache($objectCache);
$presetMapping = PresetMappingFactory::fromObjectCacheReversed($objectCache);

$source2 = new ObjectWithScalarProperties();
$targetType2 = TypeFactory::objectOfClass(ObjectWithScalarProperties::class);
$target2 = new ObjectWithScalarPropertiesDto();

$objectCache->saveTarget($source2, $targetType2, $target2);

$presetMapping2 = PresetMappingFactory::fromObjectCache($objectCache);
$presetMapping2 = PresetMappingFactory::fromObjectCacheReversed($objectCache);

$presetMapping->mergeFrom($presetMapping2);

Expand Down

0 comments on commit 99012f7

Please sign in to comment.