Skip to content

Commit

Permalink
dx: Clarity.
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Jan 16, 2024
1 parent 62e5260 commit 1997122
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* docs: Add rationale.
* fix: Add variance safeguard.
* dx: Clarity.

## 0.5.12

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

public function findBySourceAndTargetType(
/**
* @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,
): SearchResult {
Expand All @@ -59,7 +79,9 @@ public function findBySourceAndTargetType(
$searchResultEntries = [];

foreach ($mapping as $mappingEntry) {
if ($mappingEntry->isVariantTargetType() || !TypeCheck::isObject($targetType)) {
if ($mappingEntry->isVariantTargetType()) {
// if variant

$searchResultEntry = new SearchResultEntry(
mappingOrder: $mappingEntry->getOrder(),
sourceType: $sourceType,
Expand All @@ -70,6 +92,8 @@ public function findBySourceAndTargetType(

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

if (
TypeCheck::isSomewhatIdentical(
$targetType,
Expand Down Expand Up @@ -116,23 +140,4 @@ public function findBySourceAndTargetTypes(

return new SearchResult($searchResultEntries);
}

/**
* @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);
}
}

0 comments on commit 1997122

Please sign in to comment.