Skip to content

Commit 56c257b

Browse files
committed
feat: Planned Matcher logic
1 parent e23c078 commit 56c257b

File tree

6 files changed

+45
-9
lines changed

6 files changed

+45
-9
lines changed

src/Mapper/Application/Contract/MapperInterface.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PBaszak\UltraMapper\Mapper\Application\Contract;
66

7+
use PBaszak\UltraMapper\Mapper\Application\Model\Context;
78
use PBaszak\UltraMapper\Mapper\Application\Model\Envelope;
89

910
interface MapperInterface
@@ -21,7 +22,7 @@ interface MapperInterface
2122
* @param string $blueprintClass The class of the blueprint. Defines properties to map.
2223
* @param TypeInterface $from you have to define what type are you passing in the $data
2324
* @param TypeInterface $to you have to define what type you want to get
24-
* @param bool $isCollection if the data is a collection of objects or arrays then set this to true
25+
* @param Context $context if the data is a collection of objects or arrays then set this to true
2526
*
2627
* @return Envelope the mapped data wrapped in the Evelope
2728
*/
@@ -31,6 +32,6 @@ public function map(
3132
string $blueprintClass,
3233
TypeInterface $from,
3334
TypeInterface $to,
34-
bool $isCollection = false
35+
Context $context,
3536
): Envelope;
3637
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PBaszak\UltraMapper\Mapper\Application\Model;
6+
7+
class Context
8+
{
9+
/**
10+
* @param string[] $groups
11+
*/
12+
public function __construct(
13+
/** @var string[] $groups */
14+
public array $groups = ['Default'],
15+
public bool $isCollection = false,
16+
) {
17+
}
18+
}

src/Mapper/Application/Service/Mapper.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PBaszak\UltraMapper\Mapper\Application\Contract\MapperInterface;
1111
use PBaszak\UltraMapper\Mapper\Application\Contract\ModificatorInterface;
1212
use PBaszak\UltraMapper\Mapper\Application\Contract\TypeInterface;
13+
use PBaszak\UltraMapper\Mapper\Application\Model\Context;
1314
use PBaszak\UltraMapper\Mapper\Application\Model\Envelope;
1415
use PBaszak\UltraMapper\Mapper\Domain\Contract\ClassMapperInterface;
1516
use PBaszak\UltraMapper\Mapper\Domain\Resolver\MapperResolver;
@@ -50,9 +51,9 @@ public function map(
5051
string $blueprintClass,
5152
TypeInterface $from,
5253
TypeInterface $to,
53-
bool $isCollection = false
54+
Context $context = new Context(),
5455
): Envelope {
55-
$mapper = $this->getMapper($blueprintClass, $from, $to, $isCollection);
56+
$mapper = $this->getMapper($blueprintClass, $from, $to, $context);
5657

5758
return $mapper->map($data, $output);
5859
}
@@ -64,7 +65,7 @@ protected function getMapper(
6465
string $blueprintClass,
6566
TypeInterface $from,
6667
TypeInterface $to,
67-
bool $isCollection = false
68+
Context $context,
6869
): ClassMapperInterface {
6970
$shortName = $this->mapperResolver->getMapperShortClassName(...func_get_args(), ...$this->modificator->getModifiers());
7071

@@ -80,7 +81,7 @@ protected function getMapper(
8081
$this->modificator->prepareBlueprint($blueprint, $processType, $processUse);
8182
}
8283

83-
(new Matcher())->matchBlueprints($processType, ...$blueprints);
84+
(new Matcher())->matchBlueprints($context, $processType, ...$blueprints);
8485

8586
// modify blueprints
8687

src/Mapper/Domain/Contract/MatcherInterface.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,21 @@
66

77
use PBaszak\UltraMapper\Blueprint\Application\Model\Blueprint;
88
use PBaszak\UltraMapper\Mapper\Application\Contract\MapperInterface;
9+
use PBaszak\UltraMapper\Mapper\Application\Model\Context;
910
use PBaszak\UltraMapper\Mapper\Domain\Model\Process;
1011

1112
interface MatcherInterface
1213
{
1314
public const OPTION_ID = 'id';
14-
public const OPTION_PATH = 'path';
1515
public const OPTION_ORIGIN = MapperInterface::BLUEPRINT_PROCESS_USE;
1616
public const OPTION_SOURCE = MapperInterface::FROM_PROCESS_USE;
1717
public const OPTION_TARGET = MapperInterface::TO_PROCESS_USE;
1818

19-
public function matchBlueprints(Process $processType, Blueprint $origin, Blueprint $source, Blueprint $target): void;
19+
public function matchBlueprints(
20+
Context $context,
21+
Process $processType,
22+
Blueprint $origin,
23+
Blueprint $source,
24+
Blueprint $target
25+
): void;
2026
}

src/Mapper/Domain/Service/Matcher.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PBaszak\UltraMapper\Blueprint\Application\Model\Assets\ClassBlueprint;
88
use PBaszak\UltraMapper\Blueprint\Application\Model\Assets\PropertyBlueprint;
99
use PBaszak\UltraMapper\Blueprint\Application\Model\Blueprint;
10+
use PBaszak\UltraMapper\Mapper\Application\Model\Context;
1011
use PBaszak\UltraMapper\Mapper\Domain\Contract\MatcherInterface;
1112
use PBaszak\UltraMapper\Mapper\Domain\Exception\PropertyNotMatchedException;
1213
use PBaszak\UltraMapper\Mapper\Domain\Model\Process;
@@ -22,7 +23,7 @@ class Matcher implements MatcherInterface
2223
SameNameStrategy::class,
2324
];
2425

25-
public function matchBlueprints(Process $processType, Blueprint $origin, Blueprint $source, Blueprint $target): void
26+
public function matchBlueprints(Context $context, Process $processType, Blueprint $origin, Blueprint $source, Blueprint $target): void
2627
{
2728
$this->addLinks($origin, $source, $target);
2829
$rootBlueprints = array_map(

src/Mapper/Domain/Service/matcher.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Matcher
2+
3+
1. translacja atrybutów z Symfony Serializera / JMS Serializera na atrybuty z Ultra Mapper Serializer
4+
2. sprawdzenie blueprintów pod kątem wystąpienia pętli i zasugerowanie stosowania atrybutu `MaxDepth`, jeśli pętla zostanie wykryta
5+
3. iterowanie rekurencyjne po właściwościach:
6+
1. Wyrzucanie właściwości niezgodnych z grupą `Groups` lub z atrybutem `Ignore`.
7+
2. matchowanie właściwości po nazwach i atrybucie `TargetProperty`
8+
3. sprawdzanie typów właściwości, sprawdzenie czy się matchują, jeśli nie, to exception. Jeśli tak to ekstra. Jeśli są to klasy nie będące prostymi obiektami `SimpleObject`, to iterowanie po ich właściwościach
9+
4. wstępne matchowanie klas z typów. Decyduje zagnieżdżenie z `TargetProperty`. Jeśli klas jest wiele to określany jest procent podobieństwa, gdzie tożsame klasy zamykają temat porównania, a brak tożsamości prowadzi do oparcia się o procentową zgodność i wybierana jest klasa o wyższej zgodności i te są ostatecznie matchowane. W przypadku wielu klas, typu unii, powinien zostać zdefiniowany atrybut `Discriminator`.

0 commit comments

Comments
 (0)