Skip to content

Commit

Permalink
experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Jan 24, 2024
1 parent e8b1677 commit 8f7ab02
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 82 deletions.
1 change: 0 additions & 1 deletion config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@
->set('rekalogika.mapper.transformer_registry', TransformerRegistry::class)
->args([
'$transformersLocator' => tagged_locator('rekalogika.mapper.transformer'),
'$typeResolver' => service('rekalogika.mapper.type_resolver'),
'$mappingFactory' => service('rekalogika.mapper.mapping_factory'),
]);

Expand Down
1 change: 0 additions & 1 deletion src/MapperFactory/MapperFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,6 @@ protected function getTransformerRegistry(): TransformerRegistryInterface
if (null === $this->transformerRegistry) {
$this->transformerRegistry = new TransformerRegistry(
$this->getTransformersLocator(),
$this->getTypeResolver(),
$this->getMappingFactory(),
);
}
Expand Down
127 changes: 47 additions & 80 deletions src/TransformerRegistry/TransformerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,14 @@

use Psr\Container\ContainerInterface;
use Rekalogika\Mapper\Exception\LogicException;
use Rekalogika\Mapper\Mapping\MappingEntry;
use Rekalogika\Mapper\Mapping\MappingFactoryInterface;
use Rekalogika\Mapper\Transformer\Contracts\MixedType;
use Rekalogika\Mapper\Transformer\Contracts\TransformerInterface;
use Rekalogika\Mapper\TypeResolver\TypeResolverInterface;
use Rekalogika\Mapper\Util\TypeCheck;
use Symfony\Component\PropertyInfo\Type;

class TransformerRegistry implements TransformerRegistryInterface
{
public function __construct(
private ContainerInterface $transformersLocator,
private TypeResolverInterface $typeResolver,
private MappingFactoryInterface $mappingFactory,
) {
}
Expand Down Expand Up @@ -56,96 +51,68 @@ public function get(string $id): TransformerInterface
return $this->transformers[$id] = $transformer;
}

/**
* @todo cache this
* @param Type|MixedType $sourceType
* @param Type|MixedType $targetType
* @return array<int,MappingEntry>
*/
private function getMappingBySourceAndTargetType(
Type|MixedType $sourceType,
Type|MixedType $targetType,
): array {
$sourceTypeStrings = $this->typeResolver
->getAcceptedTransformerInputTypeStrings($sourceType);

$targetTypeStrings = $this->typeResolver
->getAcceptedTransformerOutputTypeStrings($targetType);

return $this->mappingFactory->getMapping()
->getMappingBySourceAndTarget($sourceTypeStrings, $targetTypeStrings);
}

private function findBySourceAndTargetType(
Type|MixedType $sourceType,
Type|MixedType $targetType,
public function findBySourceAndTargetTypes(
array $sourceTypes,
array $targetTypes,
): SearchResult {
$mapping = $this->getMappingBySourceAndTargetType(
$sourceType,
$targetType
);
$mapping = $this->mappingFactory->getMapping();

/** @var array<int,SearchResultEntry> */
$searchResultEntries = [];

foreach ($mapping as $mappingEntry) {
if ($mappingEntry->isVariantTargetType()) {
// if variant

$searchResultEntry = new SearchResultEntry(
mappingOrder: $mappingEntry->getOrder(),
sourceType: $sourceType,
targetType: $targetType,
transformerServiceId: $mappingEntry->getId(),
variantTargetType: $mappingEntry->isVariantTargetType()
);
$mappingSourceType = $mappingEntry->getSourceType();
$mappingTargetType = $mappingEntry->getTargetType();
$mappingIsVariantTarget = $mappingEntry->isVariantTargetType();
$mappingOrder = $mappingEntry->getOrder();

$searchResultEntries[] = $searchResultEntry;
} else {
// if invariant, check if target type is somewhat identical
if (isset($searchResultEntries[$mappingOrder])) {
continue;
}

foreach ($sourceTypes as $sourceType) {
if (
TypeCheck::isSomewhatIdentical(
$targetType,
$mappingEntry->getTargetType()
)
!TypeCheck::isVariableInstanceOf($sourceType, $mappingSourceType)
) {
$searchResultEntry = new SearchResultEntry(
mappingOrder: $mappingEntry->getOrder(),
sourceType: $sourceType,
targetType: $targetType,
transformerServiceId: $mappingEntry->getId(),
variantTargetType: $mappingEntry->isVariantTargetType()
);

$searchResultEntries[] = $searchResultEntry;
continue;
}
}
}

return new SearchResult($searchResultEntries);
}

public function findBySourceAndTargetTypes(
array $sourceTypes,
array $targetTypes,
): SearchResult {
/** @var array<int,SearchResultEntry> */
$searchResultEntries = [];

foreach ($sourceTypes as $sourceType) {
foreach ($targetTypes as $targetType) {
$result = $this->findBySourceAndTargetType($sourceType, $targetType);
foreach ($result as $searchResultEntry) {
$searchResultEntries[] = $searchResultEntry;
foreach ($targetTypes as $targetType) {
if ($mappingIsVariantTarget) {
if (
TypeCheck::isVariableInstanceOf($targetType, $mappingTargetType)
) {
$searchResultEntries[$mappingEntry->getOrder()] =
new SearchResultEntry(
mappingOrder: $mappingEntry->getOrder(),
sourceType: $sourceType,
targetType: $targetType,
transformerServiceId: $mappingEntry->getId(),
variantTargetType: $mappingEntry->isVariantTargetType()
);
}
} else {
if (
TypeCheck::isSomewhatIdentical(
$targetType,
$mappingTargetType
)
) {
$searchResultEntries[$mappingEntry->getOrder()] =
new SearchResultEntry(
mappingOrder: $mappingEntry->getOrder(),
sourceType: $sourceType,
targetType: $targetType,
transformerServiceId: $mappingEntry->getId(),
variantTargetType: $mappingEntry->isVariantTargetType()
);
}
}
}
}
}

usort(
$searchResultEntries,
fn (SearchResultEntry $a, SearchResultEntry $b)
=> $a->getMappingOrder() <=> $b->getMappingOrder()
);
ksort($searchResultEntries);

return new SearchResult($searchResultEntries);
}
Expand Down

0 comments on commit 8f7ab02

Please sign in to comment.