Skip to content

Commit

Permalink
refactor: Move more mapping logic to ObjectMappingResolver.
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Jan 16, 2024
1 parent fa874c2 commit 7ecc984
Show file tree
Hide file tree
Showing 10 changed files with 357 additions and 243 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* feat: Uses transformer class names as service IDs for easier decoration.
* refactor: Move mapping logic in `ObjectToObjectTransformer` to its own
service.
* refactor: Move more mapping logic to `ObjectMappingResolver`.

## 0.5.12

Expand Down
5 changes: 2 additions & 3 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,6 @@
$services
->set(ObjectToObjectTransformer::class)
->args([
'$propertyListExtractor' => service('rekalogika.mapper.property_info'),
'$propertyTypeExtractor' => service('rekalogika.mapper.property_info'),
'$propertyInitializableExtractor' => service('rekalogika.mapper.property_info'),
'$propertyAccessor' => service('property_accessor'),
'$typeResolver' => service('rekalogika.mapper.type_resolver'),
'$objectMappingResolver' => service('rekalogika.mapper.object_mapping_resolver'),
Expand Down Expand Up @@ -194,6 +191,8 @@
->args([
service('rekalogika.mapper.property_info'),
service('rekalogika.mapper.property_info'),
service('rekalogika.mapper.property_info'),
service('rekalogika.mapper.property_info'),
]);

# transformer registry
Expand Down
26 changes: 4 additions & 22 deletions src/MapperFactory/MapperFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,10 @@
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\PropertyInfo\Extractor\PhpStanExtractor;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyInfoCacheExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyInitializableExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;
Expand Down Expand Up @@ -183,21 +181,6 @@ private function getPropertyInfoExtractor(): PropertyInfoExtractorInterface&Prop
return $this->propertyInfoExtractor;
}

private function getPropertyInitializableExtractor(): PropertyInitializableExtractorInterface
{
return $this->getPropertyInfoExtractor();
}

private function getPropertyAccessExtractor(): PropertyAccessExtractorInterface
{
return $this->getPropertyInfoExtractor();
}

private function getPropertyListExtractor(): PropertyListExtractorInterface
{
return $this->getPropertyInfoExtractor();
}

//
// concrete services
//
Expand Down Expand Up @@ -275,9 +258,6 @@ protected function getObjectToObjectTransformer(): TransformerInterface
{
if (null === $this->objectToObjectTransformer) {
$this->objectToObjectTransformer = new ObjectToObjectTransformer(
$this->getPropertyListExtractor(),
$this->getPropertyInfoExtractor(),
$this->getPropertyInitializableExtractor(),
$this->getPropertyAccessor(),
$this->getTypeResolver(),
$this->getObjectMappingResolver(),
Expand Down Expand Up @@ -411,8 +391,10 @@ protected function getObjectMappingResolver(): ObjectMappingResolverInterface
{
if (null === $this->objectMappingResolver) {
$this->objectMappingResolver = new ObjectMappingResolver(
$this->getPropertyAccessExtractor(),
$this->getPropertyListExtractor(),
$this->getPropertyInfoExtractor(),
$this->getPropertyInfoExtractor(),
$this->getPropertyInfoExtractor(),
$this->getPropertyInfoExtractor(),
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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 ConstructorMapping
{
/**
* @var array<int,Type> $targetTypes
*/
private array $targetTypes;

/**
* @param array<array-key,Type> $targetTypes
*/
public function __construct(
private ?string $sourceProperty,
private string $targetProperty,
array $targetTypes,
) {
$this->targetTypes = array_values($targetTypes);
}

public function getSourceProperty(): ?string
{
return $this->sourceProperty;
}

public function getTargetProperty(): string
{
return $this->targetProperty;
}

/**
* @return array<int,Type>
*/
public function getTargetTypes(): array
{
return $this->targetTypes;
}
}
38 changes: 24 additions & 14 deletions src/Transformer/ObjectMappingResolver/Contracts/ObjectMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,45 @@

namespace Rekalogika\Mapper\Transformer\ObjectMappingResolver\Contracts;

use Symfony\Component\PropertyInfo\Type;

final class ObjectMapping
{
/**
* @param array<int,ObjectMappingEntry> $propertyMapping
* @param class-string $sourceClass
* @param class-string $targetClass
* @param array<int,PropertyMapping> $propertyMapping
* @param array<int,ConstructorMapping> $constructorMapping
*/
public function __construct(
private Type $sourceType,
private Type $targetType,
private string $sourceClass,
private string $targetClass,
private array $propertyMapping,
private array $constructorMapping,
) {
}

public function getSourceType(): Type
/**
* @return array<int,PropertyMapping>
*/
public function getPropertyMapping(): array
{
return $this->propertyMapping;
}

/**
* @return array<int,ConstructorMapping>
*/
public function getConstructorMapping(): array
{
return $this->sourceType;
return $this->constructorMapping;
}

public function getTargetType(): Type
public function getSourceClass(): string
{
return $this->targetType;
return $this->sourceClass;
}

/**
* @return \Traversable<int,ObjectMappingEntry>
*/
public function getPropertyMapping(): \Traversable
public function getTargetClass(): string
{
yield from $this->propertyMapping;
return $this->targetClass;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
namespace Rekalogika\Mapper\Transformer\ObjectMappingResolver\Contracts;

use Rekalogika\Mapper\Context\Context;
use Symfony\Component\PropertyInfo\Type;

interface ObjectMappingResolverInterface
{
/**
* @param class-string $sourceClass
* @param class-string $targetClass
*/
public function resolveObjectMapping(
Type $sourceType,
Type $targetType,
string $sourceClass,
string $targetClass,
Context $context
): ObjectMapping;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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 PropertyMapping
{
/**
* @var array<int,Type> $targetTypes
*/
private array $targetTypes;

/**
* @param array<array-key,Type> $targetTypes
*/
public function __construct(
private string $sourceProperty,
private string $targetProperty,
array $targetTypes,
) {
$this->targetTypes = array_values($targetTypes);
}

public function getSourceProperty(): string
{
return $this->sourceProperty;
}

public function getTargetProperty(): string
{
return $this->targetProperty;
}

/**
* @return array<int,Type>
*/
public function getTargetTypes(): array
{
return $this->targetTypes;
}
}
Loading

0 comments on commit 7ecc984

Please sign in to comment.