|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of rekalogika/mapper package. |
| 7 | + * |
| 8 | + * (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE file |
| 11 | + * that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Rekalogika\Mapper\Transformer\Context; |
| 15 | + |
| 16 | +use Rekalogika\Mapper\ObjectCache\ObjectCache; |
| 17 | +use Rekalogika\Mapper\Transformer\Exception\PresetMappingNotFound; |
| 18 | +use Rekalogika\Mapper\Transformer\Model\SplObjectStorageWrapper; |
| 19 | + |
| 20 | +final readonly class PresetMapping |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var \WeakMap<object,\ArrayObject<class-string,object>> |
| 24 | + */ |
| 25 | + private \WeakMap $mappings; |
| 26 | + |
| 27 | + /** |
| 28 | + * @param iterable<object,iterable<class-string,object>> $mappings |
| 29 | + */ |
| 30 | + public function __construct(iterable $mappings) |
| 31 | + { |
| 32 | + /** |
| 33 | + * @var \WeakMap<object,\ArrayObject<class-string,object>> |
| 34 | + */ |
| 35 | + $weakMap = new \WeakMap(); |
| 36 | + |
| 37 | + foreach ($mappings as $source => $classToTargetMapping) { |
| 38 | + $classToTargetMappingArray = []; |
| 39 | + |
| 40 | + foreach ($classToTargetMapping as $class => $target) { |
| 41 | + $classToTargetMappingArray[$class] = $target; |
| 42 | + } |
| 43 | + |
| 44 | + $weakMap[$source] = new \ArrayObject($classToTargetMappingArray); |
| 45 | + } |
| 46 | + |
| 47 | + $this->mappings = $weakMap; |
| 48 | + } |
| 49 | + |
| 50 | + public static function fromObjectCache(ObjectCache $objectCache): self |
| 51 | + { |
| 52 | + $objectCacheWeakMap = $objectCache->getInternalMapping(); |
| 53 | + |
| 54 | + /** @var SplObjectStorageWrapper<object,\ArrayObject<class-string,object>> */ |
| 55 | + $presetMapping = new SplObjectStorageWrapper(new \SplObjectStorage()); |
| 56 | + |
| 57 | + /** |
| 58 | + * @var object $source |
| 59 | + * @var \ArrayObject<class-string,object> $classToTargetMapping |
| 60 | + */ |
| 61 | + foreach ($objectCacheWeakMap as $source => $classToTargetMapping) { |
| 62 | + $newTargetClass = $source::class; |
| 63 | + /** @var object */ |
| 64 | + $newTarget = $source; |
| 65 | + |
| 66 | + /** |
| 67 | + * @var string $targetClass |
| 68 | + * @var object $target |
| 69 | + */ |
| 70 | + foreach ($classToTargetMapping as $targetClass => $target) { |
| 71 | + if (!class_exists($targetClass)) { |
| 72 | + continue; |
| 73 | + } |
| 74 | + |
| 75 | + $newSource = $target; |
| 76 | + |
| 77 | + if (!$presetMapping->offsetExists($newSource)) { |
| 78 | + /** @var \ArrayObject<class-string,object> */ |
| 79 | + $arrayObject = new \ArrayObject(); |
| 80 | + $presetMapping->offsetSet($newSource, $arrayObject); |
| 81 | + } |
| 82 | + |
| 83 | + $presetMapping->offsetGet($newSource)?->offsetSet($newTargetClass, $newTarget); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return new self($presetMapping); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @template T of object |
| 92 | + * @param object $source |
| 93 | + * @param class-string<T> $targetClass |
| 94 | + * @return T |
| 95 | + * @throws PresetMappingNotFound |
| 96 | + */ |
| 97 | + public function findResult(object $source, string $targetClass): object |
| 98 | + { |
| 99 | + $mappings = $this->mappings[$source] ?? null; |
| 100 | + |
| 101 | + if (null === $mappings) { |
| 102 | + throw new PresetMappingNotFound(); |
| 103 | + } |
| 104 | + |
| 105 | + $result = $mappings[$targetClass] ?? null; |
| 106 | + |
| 107 | + if (null === $result) { |
| 108 | + throw new PresetMappingNotFound(); |
| 109 | + } |
| 110 | + |
| 111 | + if (!($result instanceof $targetClass)) { |
| 112 | + throw new PresetMappingNotFound(); |
| 113 | + } |
| 114 | + |
| 115 | + return $result; |
| 116 | + } |
| 117 | +} |
0 commit comments