5
5
namespace PBaszak \UltraMapper \Mapper \Domain \Service ;
6
6
7
7
use PBaszak \UltraMapper \Blueprint \Application \Model \Assets \ClassBlueprint ;
8
- use PBaszak \UltraMapper \Blueprint \Application \Model \Assets \ParameterBlueprint ;
9
8
use PBaszak \UltraMapper \Blueprint \Application \Model \Assets \PropertyBlueprint ;
10
9
use PBaszak \UltraMapper \Blueprint \Application \Model \Blueprint ;
11
- use PBaszak \UltraMapper \Mapper \Application \Attribute \TargetProperty ;
12
10
use PBaszak \UltraMapper \Mapper \Domain \Contract \MatcherInterface ;
11
+ use PBaszak \UltraMapper \Mapper \Domain \Exception \PropertyNotMatchedException ;
12
+ use PBaszak \UltraMapper \Mapper \Domain \Service \Matcher \SameNameStrategy ;
13
+ use PBaszak \UltraMapper \Mapper \Domain \Service \Matcher \TargetPropertyAttributeStrategy ;
13
14
use Symfony \Component \Uid \Uuid ;
14
15
15
16
class Matcher implements MatcherInterface
16
17
{
18
+ /** @var class-string<Matcher\MatchingStrategyInterface>[] */
19
+ protected const MATCHING_STRATEGIES = [
20
+ TargetPropertyAttributeStrategy::class,
21
+ SameNameStrategy::class,
22
+ ];
23
+
17
24
public function matchBlueprints (string $ processType , Blueprint $ origin , Blueprint $ source , Blueprint $ target ): void
18
25
{
19
26
$ this ->addLinks ($ origin , $ source , $ target );
@@ -36,28 +43,20 @@ protected function matchClassBlueprints(string $processType, ClassBlueprint $ori
36
43
37
44
protected function matchProperties (string $ processType , PropertyBlueprint $ originProperty , ClassBlueprint $ source , ClassBlueprint $ target ): void
38
45
{
39
- }
46
+ foreach ($ source ->properties as $ sourceProperty ) {
47
+ foreach ($ target ->properties as $ targetProperty ) {
48
+ foreach ($ this ::MATCHING_STRATEGIES as $ strategy ) {
49
+ $ strategyInstance = new $ strategy ();
50
+ if ($ strategyInstance ->confirmPropertiesMatching ($ processType , $ originProperty , $ sourceProperty , $ targetProperty )) {
51
+ $ this ->addLinks ($ originProperty , $ sourceProperty , $ targetProperty );
40
52
41
- protected function searchForPropertyWithSameName (PropertyBlueprint $ originProperty , ClassBlueprint $ blueprint ): ?PropertyBlueprint
42
- {
43
- /** @var PropertyBlueprint $property */
44
- foreach ($ blueprint ->properties ->assets as $ property ) {
45
- if ($ property ->originName === $ originProperty ->originName ) {
46
- return $ property ;
53
+ return ;
54
+ }
55
+ }
47
56
}
48
57
}
49
58
50
- return null ;
51
- }
52
-
53
- protected function searchForPropertyBasedOnTargetPropertyAttribute (PropertyBlueprint $ originProperty , ClassBlueprint $ blueprint ): ?PropertyBlueprint
54
- {
55
- return null ;
56
- }
57
-
58
- protected function hasTargetPropertyAttribute (PropertyBlueprint |ParameterBlueprint $ blueprint ): bool
59
- {
60
- return isset ($ blueprint ->attributes ->assets [TargetProperty::class]) && count ($ blueprint ->attributes ->assets [TargetProperty::class]) > 0 ;
59
+ throw new PropertyNotMatchedException ($ originProperty ->getPath (), sprintf ('Property "%s" from origin class "%s" could not be matched with any property from source and target classes. ' , $ originProperty ->originName , $ originProperty ->parent ->name ), sprintf ('Check Your classes: origin:"%s", source:"%s" and target:"%s" for properties with the same name or with the same attributes. Use #[TargetProperty] attribute to match properties if the names cannot be same. ' , $ originProperty ->parent ->name , $ source ->name , $ target ->name ));
61
60
}
62
61
63
62
protected function addLinks (
0 commit comments