Skip to content

Commit

Permalink
readwriteinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Jan 17, 2024
1 parent fe9134c commit 1840e0b
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 3 deletions.
2 changes: 2 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@
service('rekalogika.mapper.property_info'),
service('rekalogika.mapper.property_info'),
service('rekalogika.mapper.property_info'),
service('property_info.reflection_extractor'),
service('property_info.reflection_extractor'),
]);

$services
Expand Down
12 changes: 12 additions & 0 deletions src/MapperFactory/MapperFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,16 @@ private function getPhpStanExtractor(): PropertyTypeExtractorInterface
return $this->phpStanExtractor;
}

private function getPropertyReadInfoExtractor(): PropertyReadInfoExtractorInterface
{
return $this->getReflectionExtractor();
}

private function getPropertyWriteInfoExtractor(): PropertyWriteInfoExtractorInterface
{
return $this->getReflectionExtractor();
}

private function getPropertyInfoExtractor(): PropertyInfoExtractorInterface&PropertyInitializableExtractorInterface
{
if ($this->propertyInfoExtractor === null) {
Expand Down Expand Up @@ -396,6 +406,8 @@ protected function getObjectMappingResolver(): ObjectMappingResolverInterface
$this->getPropertyInfoExtractor(),
$this->getPropertyInfoExtractor(),
$this->getPropertyInfoExtractor(),
$this->getPropertyReadInfoExtractor(),
$this->getPropertyWriteInfoExtractor(),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace Rekalogika\Mapper\Transformer\ObjectMappingResolver\Contracts;

use Symfony\Component\PropertyInfo\PropertyReadInfo;
use Symfony\Component\PropertyInfo\PropertyWriteInfo;
use Symfony\Component\PropertyInfo\Type;

final class PropertyMapping
Expand All @@ -29,6 +31,9 @@ public function __construct(
private string $sourceProperty,
private string $targetProperty,
array $targetTypes,
private PropertyReadInfo $sourcePropertyReadInfo,
private PropertyReadInfo $targetPropertyReadInfo,
private PropertyWriteInfo $targetPropertyWriteInfo,
) {
$this->targetTypes = array_values($targetTypes);
}
Expand All @@ -50,4 +55,19 @@ public function getTargetTypes(): array
{
return $this->targetTypes;
}

public function getSourcePropertyReadInfo(): PropertyReadInfo
{
return $this->sourcePropertyReadInfo;
}

public function getTargetPropertyReadInfo(): PropertyReadInfo
{
return $this->targetPropertyReadInfo;
}

public function getTargetPropertyWriteInfo(): PropertyWriteInfo
{
return $this->targetPropertyWriteInfo;
}
}
55 changes: 52 additions & 3 deletions src/Transformer/ObjectMappingResolver/ObjectMappingResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyInitializableExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyReadInfoExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyWriteInfoExtractorInterface;

final class ObjectMappingResolver implements ObjectMappingResolverInterface
{
Expand All @@ -31,6 +33,8 @@ public function __construct(
private PropertyListExtractorInterface $propertyListExtractor,
private PropertyInitializableExtractorInterface $propertyInitializableExtractor,
private PropertyTypeExtractorInterface $propertyTypeExtractor,
private PropertyReadInfoExtractorInterface $propertyReadInfoExtractor,
private PropertyWriteInfoExtractorInterface $propertyWriteInfoExtractor,
) {
}

Expand Down Expand Up @@ -75,10 +79,55 @@ public function resolveObjectMapping(
);
}

$sourcePropertyReadInfo = $this->propertyReadInfoExtractor
->getReadInfo($sourceClass, $sourceProperty);

if (null === $sourcePropertyReadInfo) {
throw new InvalidArgumentException(
sprintf(
'Cannot get read info of source property "%s::$%s".',
$sourceClass,
$sourceProperty
),
context: $context
);
}

$targetPropertyReadInfo = $this->propertyReadInfoExtractor
->getReadInfo($targetClass, $targetProperty);

if (null === $targetPropertyReadInfo) {
throw new InvalidArgumentException(
sprintf(
'Cannot get read info of target property "%s::$%s".',
$targetClass,
$targetProperty
),
context: $context
);
}

$targetPropertyWriteInfo = $this->propertyWriteInfoExtractor
->getWriteInfo($targetClass, $targetProperty);

if (null === $targetPropertyWriteInfo) {
throw new InvalidArgumentException(
sprintf(
'Cannot get write info of target property "%s::$%s".',
$targetClass,
$targetProperty
),
context: $context
);
}

$propertyResults[] = new PropertyMapping(
$sourceProperty,
$targetProperty,
$targetPropertyTypes,
sourceProperty: $sourceProperty,
targetProperty: $targetProperty,
targetTypes: $targetPropertyTypes,
sourcePropertyReadInfo: $sourcePropertyReadInfo,
targetPropertyReadInfo: $targetPropertyReadInfo,
targetPropertyWriteInfo: $targetPropertyWriteInfo,
);
}

Expand Down

0 comments on commit 1840e0b

Please sign in to comment.