-
Notifications
You must be signed in to change notification settings - Fork 1
/
DynamicSourceToStaticTargetPathFinder.php
95 lines (85 loc) · 3.03 KB
/
DynamicSourceToStaticTargetPathFinder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/**
* This file is part of the opportus/object-mapper project.
*
* Copyright (c) 2018-2022 Clément Cazaud <clement.cazaud@gmail.com>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/
namespace Opportus\ObjectMapper\PathFinder;
use Opportus\ObjectMapper\Exception\InvalidArgumentException;
use Opportus\ObjectMapper\Route\RouteInterface;
use Opportus\ObjectMapper\SourceInterface;
use Opportus\ObjectMapper\TargetInterface;
use ReflectionParameter;
use ReflectionProperty;
/**
* The default dynamic source to static target path finder.
*
* This behavior consists of guessing the dynamic point of the source to
* connect to each static point of the target following the rules below.
*
* A target point can be:
*
* - A public property (`PropertyStaticTargetPoint`)
* - A parameter of a public setter or a public constructor
* (`ParameterStaticTargetPoint`)
*
* The connectable source point can be:
*
* - A dynamically defined property having for name the same as the target
* point (`PropertyDynamicSourcePoint`)
*
* @package Opportus\ObjectMapper\PathFinder
* @author Clément Cazaud <clement.cazaud@gmail.com>
* @license https://github.com/opportus/object-mapper/blob/master/LICENSE MIT
*/
class DynamicSourceToStaticTargetPathFinder extends StaticPathFinder
{
/**
* {@inheritdoc}
*/
protected function getReferencePointRoute(
SourceInterface $source,
TargetInterface $target,
$referencePoint
): ?RouteInterface {
if (false === \is_object($referencePoint)
|| (
!$referencePoint instanceof ReflectionProperty
&& !$referencePoint instanceof ReflectionParameter
)
) {
$message = \sprintf(
'The argument must be an object of type % or %s, got an argument of type %s.',
ReflectionProperty::class,
ReflectionParameter::class,
\is_object($referencePoint) ?
\get_class($referencePoint) : \gettype($referencePoint)
);
throw new InvalidArgumentException(3, $message);
}
$targetPointReflection = $referencePoint;
$sourceClassReflection = $source->getClassReflection();
$sourceObjectReflection = $source->getObjectReflection();
if ($sourceClassReflection
->hasProperty($targetPointReflection->getName()) ||
!$sourceObjectReflection
->hasProperty($targetPointReflection->getName())
) {
return null;
}
$sourcePointFqn = \sprintf(
'%s::$%s',
$sourceClassReflection->getName(),
$targetPointReflection->getName()
);
$targetPointFqn = $this
->getPointFqnFromReflection($targetPointReflection);
return $this->routeBuilder
->setDynamicSourcePoint($sourcePointFqn)
->setStaticTargetPoint($targetPointFqn)
->getRoute();
}
}