-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Move transformer query logic to
TransformerRegistry
.
- Loading branch information
Showing
7 changed files
with
193 additions
and
68 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
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,87 @@ | ||
<?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\TransformerRegistry; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use Rekalogika\Mapper\Contracts\MixedType; | ||
use Rekalogika\Mapper\Contracts\TransformerInterface; | ||
use Rekalogika\Mapper\Exception\LogicException; | ||
use Rekalogika\Mapper\Mapping\MappingFactoryInterface; | ||
use Rekalogika\Mapper\TypeResolver\TypeResolverInterface; | ||
use Symfony\Component\PropertyInfo\Type; | ||
|
||
class TransformerRegistry implements TransformerRegistryInterface | ||
{ | ||
public function __construct( | ||
private ContainerInterface $transformersLocator, | ||
private TypeResolverInterface $typeResolver, | ||
private MappingFactoryInterface $mappingFactory, | ||
) { | ||
} | ||
|
||
public function get(string $id): TransformerInterface | ||
{ | ||
$transformer = $this->transformersLocator->get($id); | ||
|
||
if (!$transformer instanceof TransformerInterface) { | ||
throw new LogicException(sprintf( | ||
'Transformer with id "%s" must implement %s', | ||
$id, | ||
TransformerInterface::class | ||
)); | ||
} | ||
|
||
return $transformer; | ||
} | ||
|
||
// public function findBySourceAndTargetTypes( | ||
// iterable $sourceTypes, | ||
// iterable $targetTypes, | ||
// ): iterable { | ||
// foreach ($sourceTypes as $sourceType) { | ||
// foreach ($targetTypes as $targetType) { | ||
// yield from $this->findBySourceAndTargetType( | ||
// $sourceType, | ||
// $targetType | ||
// ); | ||
// } | ||
// } | ||
// } | ||
|
||
public function findBySourceAndTargetType( | ||
Type|MixedType $sourceType, | ||
Type|MixedType $targetType, | ||
): iterable { | ||
$mapping = $this->getMappingBySourceAndTargetType($sourceType, $targetType); | ||
|
||
foreach ($mapping as $item) { | ||
$id = $item->getId(); | ||
yield $this->get($id); | ||
} | ||
} | ||
|
||
public function getMappingBySourceAndTargetType( | ||
Type|MixedType $sourceType, | ||
Type|MixedType $targetType, | ||
): array { | ||
$sourceTypeStrings = $this->typeResolver | ||
->getApplicableTypeStrings($sourceType); | ||
|
||
$targetTypeStrings = $this->typeResolver | ||
->getApplicableTypeStrings($targetType); | ||
|
||
return $this->mappingFactory->getMapping() | ||
->getMappingBySourceAndTarget($sourceTypeStrings, $targetTypeStrings); | ||
} | ||
} |
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,54 @@ | ||
<?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\TransformerRegistry; | ||
|
||
use Rekalogika\Mapper\Contracts\MixedType; | ||
use Rekalogika\Mapper\Contracts\TransformerInterface; | ||
use Rekalogika\Mapper\Mapping\MappingEntry; | ||
use Symfony\Component\PropertyInfo\Type; | ||
|
||
interface TransformerRegistryInterface | ||
{ | ||
public function get(string $id): TransformerInterface; | ||
|
||
// /** | ||
// * @param iterable<int,Type|MixedType> $sourceTypes | ||
// * @param iterable<int,Type|MixedType> $targetTypes | ||
// * @return iterable<int,TransformerInterface> | ||
// */ | ||
// public function findBySourceAndTargetTypes( | ||
// iterable $sourceTypes, | ||
// iterable $targetTypes, | ||
// ): iterable; | ||
|
||
/** | ||
* @param Type|MixedType $sourceType | ||
* @param Type|MixedType $targetType | ||
* @return iterable<int,TransformerInterface> | ||
*/ | ||
public function findBySourceAndTargetType( | ||
Type|MixedType $sourceType, | ||
Type|MixedType $targetType, | ||
): iterable; | ||
|
||
/** | ||
* @param Type|MixedType $sourceType | ||
* @param Type|MixedType $targetType | ||
* @return array<int,MappingEntry> | ||
*/ | ||
public function getMappingBySourceAndTargetType( | ||
Type|MixedType $sourceType, | ||
Type|MixedType $targetType, | ||
): array; | ||
} |