Skip to content

Commit

Permalink
refactor: Refactor Transformer for clarity.
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Jan 16, 2024
1 parent a0dc5ec commit 1ea5355
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 47 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* test: Reorganize test namespaces.
* fix: Fix path forming.
* fix: Add type checking for variance in `TypeMapping` and `MappingEntry`.
* refactor: Refactor `Transformer` for clarity.

## 0.5.11

Expand Down
103 changes: 56 additions & 47 deletions src/TransformerRegistry/TransformerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,67 +47,76 @@ public function get(string $id): TransformerInterface
return $transformer;
}

public function findBySourceAndTargetType(
Type|MixedType $sourceType,
Type|MixedType $targetType,
): SearchResult {
$mapping = $this->getMappingBySourceAndTargetType(
$sourceType,
$targetType
);

$searchResultEntries = [];

foreach ($mapping as $mappingEntry) {
if ($mappingEntry->isVariantTargetType() || !TypeCheck::isObject($targetType)) {
$searchResultEntry = new SearchResultEntry(
mappingOrder: $mappingEntry->getOrder(),
sourceType: $sourceType,
targetType: $targetType,
transformer: $this->get($mappingEntry->getId()),
transformerServiceId: $mappingEntry->getId(),
variantTargetType: $mappingEntry->isVariantTargetType()
);

$searchResultEntries[] = $searchResultEntry;
} else {
if (
TypeCheck::isSomewhatIdentical(
$targetType,
$mappingEntry->getTargetType()
)
) {
$searchResultEntry = new SearchResultEntry(
mappingOrder: $mappingEntry->getOrder(),
sourceType: $sourceType,
targetType: $targetType,
transformer: $this->get($mappingEntry->getId()),
transformerServiceId: $mappingEntry->getId(),
variantTargetType: $mappingEntry->isVariantTargetType()
);

$searchResultEntries[] = $searchResultEntry;
}
}
}

return new SearchResult($searchResultEntries);
}

public function findBySourceAndTargetTypes(
iterable $sourceTypes,
iterable $targetTypes,
): SearchResult {
/** @var array<int,array{0:Type|MixedType,1:Type|MixedType,2:MappingEntry}> */
$mappingEntries = [];
/** @var array<int,SearchResultEntry> */
$searchResultEntries = [];

foreach ($sourceTypes as $sourceType) {
foreach ($targetTypes as $targetType) {
$mapping = $this->getMappingBySourceAndTargetType(
$sourceType,
$targetType
);

foreach ($mapping as $mappingEntry) {
if ($mappingEntry->isVariantTargetType() || !TypeCheck::isObject($targetType)) {
$mappingEntries[] = [
$sourceType,
$targetType,
$mappingEntry,
];
} else {
if (
TypeCheck::isSomewhatIdentical(
$targetType,
$mappingEntry->getTargetType()
)
) {
$mappingEntries[] = [
$sourceType,
$targetType,
$mappingEntry,
];
}
}
$result = $this->findBySourceAndTargetType($sourceType, $targetType);
foreach ($result as $searchResultEntry) {
$searchResultEntries[] = $searchResultEntry;
}
}
}

usort(
$mappingEntries,
fn (array $a, array $b)
=>
/** @psalm-suppress MixedMethodCall */
$a[2]->getOrder() <=> $b[2]->getOrder()
$searchResultEntries,
fn (SearchResultEntry $a, SearchResultEntry $b)
=> $a->getMappingOrder() <=> $b->getMappingOrder()
);

$result = [];

foreach ($mappingEntries as $mappingEntry) {
$result[] = new SearchResultEntry(
$mappingEntry[2]->getOrder(),
$mappingEntry[0],
$mappingEntry[1],
$this->get($mappingEntry[2]->getId()),
$mappingEntry[2]->getId(),
$mappingEntry[2]->isVariantTargetType()
);
}

return new SearchResult($result);
return new SearchResult($searchResultEntries);
}

/**
Expand Down

0 comments on commit 1ea5355

Please sign in to comment.