-
-
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.
refactor: spin off
resolveTargetClass()
to separate class (#220)
- Loading branch information
Showing
6 changed files
with
170 additions
and
58 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
38 changes: 38 additions & 0 deletions
38
src/Transformer/MetadataUtil/TargetClassResolver/CachingTargetClassResolver.php
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,38 @@ | ||
<?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\MetadataUtil\TargetClassResolver; | ||
|
||
use Rekalogika\Mapper\Transformer\MetadataUtil\TargetClassResolverInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class CachingTargetClassResolver implements TargetClassResolverInterface | ||
{ | ||
/** | ||
* @var array<class-string,array<class-string,class-string>> | ||
*/ | ||
private array $cache = []; | ||
|
||
public function __construct( | ||
private readonly TargetClassResolverInterface $decorated, | ||
) {} | ||
|
||
#[\Override] | ||
public function resolveTargetClass(string $sourceClass, string $targetClass): string | ||
{ | ||
return $this->cache[$sourceClass][$targetClass] | ||
??= $this->decorated->resolveTargetClass($sourceClass, $targetClass); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/Transformer/MetadataUtil/TargetClassResolver/TargetClassResolver.php
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,75 @@ | ||
<?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\MetadataUtil\TargetClassResolver; | ||
|
||
use Rekalogika\Mapper\Attribute\InheritanceMap; | ||
use Rekalogika\Mapper\Transformer\Exception\SourceClassNotInInheritanceMapException; | ||
use Rekalogika\Mapper\Transformer\MetadataUtil\TargetClassResolverInterface; | ||
use Rekalogika\Mapper\Util\ClassUtil; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final readonly class TargetClassResolver implements TargetClassResolverInterface | ||
{ | ||
public function resolveTargetClass( | ||
string $sourceClass, | ||
string $targetClass, | ||
): string { | ||
$sourceReflection = new \ReflectionClass($sourceClass); | ||
$targetReflection = new \ReflectionClass($targetClass); | ||
|
||
$targetAttributes = $targetReflection->getAttributes(InheritanceMap::class); | ||
|
||
if ($targetAttributes !== []) { | ||
// if the target has an InheritanceMap, we try to resolve the target | ||
// class using the InheritanceMap | ||
|
||
$inheritanceMap = $targetAttributes[0]->newInstance(); | ||
|
||
$resolvedTargetClass = $inheritanceMap->getTargetClassFromSourceClass($sourceClass); | ||
|
||
if ($resolvedTargetClass === null) { | ||
throw new SourceClassNotInInheritanceMapException($sourceClass, $targetClass); | ||
} | ||
|
||
return $resolvedTargetClass; | ||
} elseif ($targetReflection->isAbstract() || $targetReflection->isInterface()) { | ||
// if target doesn't have an inheritance map, but is also abstract | ||
// or an interface, we try to find the InheritanceMap from the | ||
// source | ||
|
||
$sourceClasses = ClassUtil::getAllClassesFromObject($sourceClass); | ||
|
||
foreach ($sourceClasses as $currentSourceClass) { | ||
$sourceReflection = new \ReflectionClass($currentSourceClass); | ||
$sourceAttributes = $sourceReflection->getAttributes(InheritanceMap::class); | ||
|
||
if ($sourceAttributes !== []) { | ||
$inheritanceMap = $sourceAttributes[0]->newInstance(); | ||
|
||
$resolvedTargetClass = $inheritanceMap->getSourceClassFromTargetClass($sourceClass); | ||
|
||
if ($resolvedTargetClass === null) { | ||
throw new SourceClassNotInInheritanceMapException($currentSourceClass, $targetClass); | ||
} | ||
|
||
return $resolvedTargetClass; | ||
} | ||
} | ||
} | ||
|
||
return $targetClass; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Transformer/MetadataUtil/TargetClassResolverInterface.php
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,36 @@ | ||
<?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\MetadataUtil; | ||
|
||
/** | ||
* Resolves the target type hint to the actual class name. Especially useful | ||
* for resolving abstract classes or interfaces to concrete classes. | ||
* | ||
* @internal | ||
*/ | ||
interface TargetClassResolverInterface | ||
{ | ||
/** | ||
* Resolves the target type hint to the actual class name. Especially useful | ||
* for resolving abstract classes or interfaces to concrete classes. | ||
* | ||
* @param class-string $sourceClass | ||
* @param class-string $targetClass | ||
* @return class-string | ||
*/ | ||
public function resolveTargetClass( | ||
string $sourceClass, | ||
string $targetClass, | ||
): string; | ||
} |
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