-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(
PresetMapping
): Support proxied classes, add tests.
- Loading branch information
Showing
7 changed files
with
227 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/mapper package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Mapper\Transformer\Context; | ||
|
||
use Rekalogika\Mapper\ObjectCache\ObjectCache; | ||
use Rekalogika\Mapper\Transformer\Model\SplObjectStorageWrapper; | ||
use Rekalogika\Mapper\Util\ClassUtil; | ||
|
||
final readonly class PresetMappingFactory | ||
{ | ||
private function __construct() | ||
{ | ||
} | ||
|
||
public static function fromObjectCache(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 | ||
*/ | ||
foreach ($objectCacheWeakMap as $source => $classToTargetMapping) { | ||
$newTargetClass = ClassUtil::determineRealClassFromPossibleProxy($source::class); | ||
/** @var object */ | ||
$newTarget = $source; | ||
|
||
/** | ||
* @var string $targetClass | ||
* @var object $target | ||
*/ | ||
foreach ($classToTargetMapping as $targetClass => $target) { | ||
if (!class_exists($targetClass)) { | ||
continue; | ||
} | ||
|
||
$newSource = $target; | ||
|
||
if (!$presetMapping->offsetExists($newSource)) { | ||
/** @var \ArrayObject<class-string,object> */ | ||
$arrayObject = new \ArrayObject(); | ||
$presetMapping->offsetSet($newSource, $arrayObject); | ||
} | ||
|
||
$presetMapping->offsetGet($newSource)?->offsetSet($newTargetClass, $newTarget); | ||
} | ||
} | ||
|
||
return new PresetMapping($presetMapping); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/mapper package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
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; | ||
use Rekalogika\Mapper\Transformer\Context\PresetMappingFactory; | ||
use Symfony\Contracts\Service\ResetInterface; | ||
|
||
class RememberingMapper implements MapperInterface, ResetInterface | ||
{ | ||
private PresetMapping $presetMapping; | ||
|
||
public function __construct( | ||
private MapperInterface $decorated, | ||
private ObjectCacheFactoryInterface $objectCacheFactory | ||
) { | ||
$this->presetMapping = new PresetMapping(); | ||
} | ||
|
||
public function reset() | ||
Check failure on line 35 in tests/Fixtures/RememberingMapper/RememberingMapper.php GitHub Actions / Symfony 6.4.*, lowest deps, PHP 8.2, ubuntu-latestMissingReturnType
Check failure on line 35 in tests/Fixtures/RememberingMapper/RememberingMapper.php GitHub Actions / Symfony 6.4.*, lowest deps, PHP 8.3, ubuntu-latestMissingReturnType
|
||
{ | ||
$this->presetMapping = new PresetMapping(); | ||
} | ||
|
||
public function map(object $source, object|string $target, ?Context $context = null): object | ||
{ | ||
$objectCache = $this->objectCacheFactory->createObjectCache(); | ||
|
||
if ($context === null) { | ||
$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::fromObjectCache($objectCache); | ||
$this->presetMapping->mergeFrom($newPresetMapping); | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/mapper package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Mapper\Tests\IntegrationTest; | ||
|
||
use Rekalogika\Mapper\Tests\Common\FrameworkTestCase; | ||
use Rekalogika\Mapper\Tests\Fixtures\RememberingMapper\RememberingMapper; | ||
use Rekalogika\Mapper\Tests\Fixtures\Scalar\ObjectWithScalarProperties; | ||
use Rekalogika\Mapper\Tests\Fixtures\Scalar\ObjectWithScalarPropertiesWithNullContents; | ||
use Rekalogika\Mapper\Tests\Fixtures\ScalarDto\ObjectWithFloatPropertiesDto; | ||
use Rekalogika\Mapper\Tests\Fixtures\ScalarDto\ObjectWithIntPropertiesDto; | ||
|
||
class RememberingMapperTest extends FrameworkTestCase | ||
{ | ||
public function testMapping(): void | ||
{ | ||
$mapper = $this->get(RememberingMapper::class); | ||
|
||
$source1 = new ObjectWithScalarProperties(); | ||
$target1 = ObjectWithFloatPropertiesDto::class; | ||
$result1 = $mapper->map($source1, $target1); | ||
|
||
$source2 = new ObjectWithScalarPropertiesWithNullContents(); | ||
$target2 = ObjectWithIntPropertiesDto::class; | ||
$result2 = $mapper->map($source2, $target2); | ||
|
||
$shouldbeSource1 = $mapper->map($result1, $source1::class); | ||
$shouldbeSource2 = $mapper->map($result2, $source2::class); | ||
|
||
$this->assertEquals($source1, $shouldbeSource1); | ||
$this->assertEquals($source2, $shouldbeSource2); | ||
} | ||
} |