-
-
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: Move mapping logic in ObjectToObjectTransformer` to its own…
… service.
- Loading branch information
Showing
8 changed files
with
286 additions
and
89 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
47 changes: 47 additions & 0 deletions
47
src/Transformer/ObjectMappingResolver/Contracts/ObjectMapping.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,47 @@ | ||
<?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\ObjectMappingResolver\Contracts; | ||
|
||
use Symfony\Component\PropertyInfo\Type; | ||
|
||
final class ObjectMapping | ||
{ | ||
/** | ||
* @param array<int,ObjectMappingEntry> $propertyMapping | ||
*/ | ||
public function __construct( | ||
private Type $sourceType, | ||
private Type $targetType, | ||
private array $propertyMapping, | ||
) { | ||
} | ||
|
||
public function getSourceType(): Type | ||
{ | ||
return $this->sourceType; | ||
} | ||
|
||
public function getTargetType(): Type | ||
{ | ||
return $this->targetType; | ||
} | ||
|
||
/** | ||
* @return \Traversable<int,ObjectMappingEntry> | ||
*/ | ||
public function getPropertyMapping(): \Traversable | ||
{ | ||
yield from $this->propertyMapping; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Transformer/ObjectMappingResolver/Contracts/ObjectMappingEntry.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,33 @@ | ||
<?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\ObjectMappingResolver\Contracts; | ||
|
||
final class ObjectMappingEntry | ||
{ | ||
public function __construct( | ||
private string $sourcePath, | ||
private string $targetPath, | ||
) { | ||
} | ||
|
||
public function getSourcePath(): string | ||
{ | ||
return $this->sourcePath; | ||
} | ||
|
||
public function getTargetPath(): string | ||
{ | ||
return $this->targetPath; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/Transformer/ObjectMappingResolver/Contracts/ObjectMappingResolverInterface.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,26 @@ | ||
<?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\ObjectMappingResolver\Contracts; | ||
|
||
use Rekalogika\Mapper\Context\Context; | ||
use Symfony\Component\PropertyInfo\Type; | ||
|
||
interface ObjectMappingResolverInterface | ||
{ | ||
public function resolveObjectMapping( | ||
Type $sourceType, | ||
Type $targetType, | ||
Context $context | ||
): ObjectMapping; | ||
} |
121 changes: 121 additions & 0 deletions
121
src/Transformer/ObjectMappingResolver/ObjectMappingResolver.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,121 @@ | ||
<?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\ObjectMappingResolver; | ||
|
||
use Rekalogika\Mapper\Context\Context; | ||
use Rekalogika\Mapper\Exception\InvalidArgumentException; | ||
use Rekalogika\Mapper\Transformer\ObjectMappingResolver\Contracts\ObjectMapping; | ||
use Rekalogika\Mapper\Transformer\ObjectMappingResolver\Contracts\ObjectMappingEntry; | ||
use Rekalogika\Mapper\Transformer\ObjectMappingResolver\Contracts\ObjectMappingResolverInterface; | ||
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface; | ||
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface; | ||
use Symfony\Component\PropertyInfo\Type; | ||
|
||
final class ObjectMappingResolver implements ObjectMappingResolverInterface | ||
{ | ||
public function __construct( | ||
private PropertyAccessExtractorInterface $propertyAccessExtractor, | ||
private PropertyListExtractorInterface $propertyListExtractor, | ||
) { | ||
} | ||
|
||
public function resolveObjectMapping( | ||
Type $sourceType, | ||
Type $targetType, | ||
Context $context | ||
): ObjectMapping { | ||
$sourceProperties = $this->listSourceAttributes($sourceType, $context); | ||
$writableTargetProperties = $this | ||
->listTargetWritableAttributes($targetType, $context); | ||
|
||
$propertiesToMap = array_intersect($sourceProperties, $writableTargetProperties); | ||
|
||
$results = []; | ||
|
||
foreach ($propertiesToMap as $property) { | ||
$results[] = new ObjectMappingEntry( | ||
$property, | ||
$property, | ||
); | ||
} | ||
|
||
return new ObjectMapping( | ||
$sourceType, | ||
$targetType, | ||
$results, | ||
); | ||
} | ||
|
||
/** | ||
* @return array<int,string> | ||
* @todo cache result | ||
*/ | ||
protected function listSourceAttributes( | ||
Type $sourceType, | ||
Context $context | ||
): array { | ||
$class = $sourceType->getClassName(); | ||
|
||
if (null === $class) { | ||
throw new InvalidArgumentException('Cannot get class name from source type.', context: $context); | ||
} | ||
|
||
$attributes = $this->propertyListExtractor->getProperties($class); | ||
|
||
if (null === $attributes) { | ||
throw new InvalidArgumentException(sprintf('Cannot get properties from source class "%s".', $class), context: $context); | ||
} | ||
|
||
$readableAttributes = []; | ||
|
||
foreach ($attributes as $attribute) { | ||
if ($this->propertyAccessExtractor->isReadable($class, $attribute)) { | ||
$readableAttributes[] = $attribute; | ||
} | ||
} | ||
|
||
return $readableAttributes; | ||
} | ||
|
||
/** | ||
* @return array<int,string> | ||
* @todo cache result | ||
*/ | ||
protected function listTargetWritableAttributes( | ||
Type $targetType, | ||
Context $context | ||
): array { | ||
$class = $targetType->getClassName(); | ||
|
||
if (null === $class) { | ||
throw new InvalidArgumentException('Cannot get class name from source type.', context: $context); | ||
} | ||
|
||
$attributes = $this->propertyListExtractor->getProperties($class); | ||
|
||
if (null === $attributes) { | ||
throw new InvalidArgumentException(sprintf('Cannot get properties from target class "%s".', $class), context: $context); | ||
} | ||
|
||
$writableAttributes = []; | ||
|
||
foreach ($attributes as $attribute) { | ||
if ($this->propertyAccessExtractor->isWritable($class, $attribute)) { | ||
$writableAttributes[] = $attribute; | ||
} | ||
} | ||
|
||
return $writableAttributes; | ||
} | ||
} |
Oops, something went wrong.