Skip to content

Commit 99012f7

Browse files
authored
Merge pull request #36 from rekalogika:feat/fromobjectcache
feat(`PresetMappingFactory`): Add `fromObjectCache()` and `fromObjectCacheReversed()`.
2 parents 129ee56 + dbb8911 commit 99012f7

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* feat: Mapping to existing values in a dynamic property.
2222
* perf(`ObjectToObjectTransformer`): Prevent delegating to `MainTransformer` if
2323
the current value in a dynamic property is a scalar.
24+
* feat(`PresetMappingFactory`): Add `fromObjectCache()` and `fromObjectCacheReversed()`.
2425

2526
## 1.0.0
2627

src/Transformer/Context/PresetMappingFactory.php

+26
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,32 @@ public static function fromObjectCache(ObjectCache $objectCache): PresetMapping
3030
/** @var SplObjectStorageWrapper<object,\ArrayObject<class-string,object>> */
3131
$presetMapping = new SplObjectStorageWrapper(new \SplObjectStorage());
3232

33+
/**
34+
* @var object $source
35+
* @var \ArrayObject<class-string,object> $classToTargetMapping
36+
*/
37+
foreach ($objectCacheWeakMap as $source => $classToTargetMapping) {
38+
foreach ($classToTargetMapping as $targetClass => $target) {
39+
if (!$presetMapping->offsetExists($source)) {
40+
/** @var \ArrayObject<class-string,object> */
41+
$arrayObject = new \ArrayObject();
42+
$presetMapping->offsetSet($source, $arrayObject);
43+
}
44+
45+
$presetMapping->offsetGet($source)?->offsetSet($targetClass, $target);
46+
}
47+
}
48+
49+
return new PresetMapping($presetMapping);
50+
}
51+
52+
public static function fromObjectCacheReversed(ObjectCache $objectCache): PresetMapping
53+
{
54+
$objectCacheWeakMap = $objectCache->getInternalMapping();
55+
56+
/** @var SplObjectStorageWrapper<object,\ArrayObject<class-string,object>> */
57+
$presetMapping = new SplObjectStorageWrapper(new \SplObjectStorage());
58+
3359
/**
3460
* @var object $source
3561
* @var \ArrayObject<class-string,object> $classToTargetMapping

tests/Fixtures/RememberingMapper/RememberingMapper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function map(object $source, object|string $target, ?Context $context = n
5757
throw new UnexpectedValueException(sprintf('Expected instance of "%s", got "%s"', $target, get_class($result)));
5858
}
5959

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

6363
return $result;

tests/IntegrationTest/PresetMappingTest.php

+20-3
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,30 @@ public function testFromObjectCache(): void
3636
$objectCache = $this->createObjectCache();
3737

3838
$source = new ObjectWithScalarProperties();
39-
$targetType = TypeFactory::objectOfClass(ObjectWithScalarProperties::class);
39+
$targetType = TypeFactory::objectOfClass(ObjectWithScalarPropertiesDto::class);
4040
$target = new ObjectWithScalarPropertiesDto();
4141

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

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

46+
$result = $presetMapping->findResult($source, ObjectWithScalarPropertiesDto::class);
47+
48+
$this->assertSame($target, $result);
49+
}
50+
51+
public function testFromObjectCacheReversed(): void
52+
{
53+
$objectCache = $this->createObjectCache();
54+
55+
$source = new ObjectWithScalarProperties();
56+
$targetType = TypeFactory::objectOfClass(ObjectWithScalarProperties::class);
57+
$target = new ObjectWithScalarPropertiesDto();
58+
59+
$objectCache->saveTarget($source, $targetType, $target);
60+
61+
$presetMapping = PresetMappingFactory::fromObjectCacheReversed($objectCache);
62+
4663
$result = $presetMapping->findResult($target, $source::class);
4764

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

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

61-
$presetMapping = PresetMappingFactory::fromObjectCache($objectCache);
78+
$presetMapping = PresetMappingFactory::fromObjectCacheReversed($objectCache);
6279

6380
$source2 = new ObjectWithScalarProperties();
6481
$targetType2 = TypeFactory::objectOfClass(ObjectWithScalarProperties::class);
6582
$target2 = new ObjectWithScalarPropertiesDto();
6683

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

69-
$presetMapping2 = PresetMappingFactory::fromObjectCache($objectCache);
86+
$presetMapping2 = PresetMappingFactory::fromObjectCacheReversed($objectCache);
7087

7188
$presetMapping->mergeFrom($presetMapping2);
7289

0 commit comments

Comments
 (0)