1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ArgumentResolver; |
4
|
|
|
|
5
|
|
|
use ArgumentResolver\Argument\ArgumentDescription; |
6
|
|
|
use ArgumentResolver\Argument\ArgumentDescriptions; |
7
|
|
|
use ArgumentResolver\Argument\ArgumentDescriptor; |
8
|
|
|
use ArgumentResolver\Exception\ArgumentResolutionException; |
9
|
|
|
use ArgumentResolver\Exception\ResolutionException; |
10
|
|
|
use ArgumentResolver\Resolution\ConstraintResolver; |
11
|
|
|
use ArgumentResolver\Resolution\Resolution; |
12
|
|
|
use ArgumentResolver\Resolution\ResolutionConstraint; |
13
|
|
|
use ArgumentResolver\Resolution\ResolutionConstraintCollection; |
14
|
|
|
use ArgumentResolver\Resolution\Resolutions; |
15
|
|
|
|
16
|
|
|
class ArgumentResolver |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var ArgumentDescriptor |
20
|
|
|
*/ |
21
|
|
|
private $argumentDescriptor; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ConstraintResolver |
25
|
|
|
*/ |
26
|
|
|
private $constraintResolver; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param ArgumentDescriptor $argumentDescriptor |
30
|
|
|
* @param ConstraintResolver $constraintResolver |
31
|
|
|
*/ |
32
|
|
|
public function __construct(ArgumentDescriptor $argumentDescriptor, ConstraintResolver $constraintResolver) |
33
|
|
|
{ |
34
|
|
|
$this->argumentDescriptor = $argumentDescriptor; |
35
|
|
|
$this->constraintResolver = $constraintResolver; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Resolve the arguments needed by the given callable and the order of these |
40
|
|
|
* arguments. |
41
|
|
|
* |
42
|
|
|
* It returns an array with the value of arguments in the right order. |
43
|
|
|
* |
44
|
|
|
* @param mixed $callable |
45
|
|
|
* @param array $availableArguments |
46
|
|
|
* |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public function resolveArguments($callable, array $availableArguments = []) |
50
|
|
|
{ |
51
|
|
|
$descriptions = $this->argumentDescriptor->getDescriptions($callable)->sortByPosition(); |
52
|
|
|
$constraints = $this->constraintResolver->resolveConstraints($descriptions); |
53
|
|
|
|
54
|
|
|
$resolutions = new Resolutions(); |
55
|
|
|
foreach ($descriptions as $description) { |
56
|
|
|
$resolutions->addCollection( |
57
|
|
|
$this->getArgumentResolutions($constraints, $description, $availableArguments) |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->addMissingResolutions($resolutions, $descriptions); |
62
|
|
|
|
63
|
|
|
$arguments = $resolutions->sortByPriority()->toArgumentsArray(); |
64
|
|
|
|
65
|
|
|
return $arguments; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param ResolutionConstraintCollection $constraints |
70
|
|
|
* @param ArgumentDescription $description |
71
|
|
|
* @param array $availableArguments |
72
|
|
|
* |
73
|
|
|
* @return Resolution[] |
74
|
|
|
*/ |
75
|
|
|
private function getArgumentResolutions(ResolutionConstraintCollection $constraints, ArgumentDescription $description, array $availableArguments) |
76
|
|
|
{ |
77
|
|
|
$resolutions = []; |
78
|
|
|
|
79
|
|
|
foreach ($availableArguments as $argumentName => $argumentValue) { |
80
|
|
|
$priority = $this->getArgumentPriority($constraints, $description, $argumentName, $argumentValue); |
81
|
|
|
|
82
|
|
|
if ($priority > 0) { |
83
|
|
|
$resolutions[] = new Resolution($description->getPosition(), $argumentValue, $priority); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $resolutions; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param ResolutionConstraintCollection $constraints |
92
|
|
|
* @param ArgumentDescription $description |
93
|
|
|
* @param string $argumentName |
94
|
|
|
* @param mixed $argumentValue |
95
|
|
|
* |
96
|
|
|
* @return int |
97
|
|
|
*/ |
98
|
|
|
private function getArgumentPriority(ResolutionConstraintCollection $constraints, ArgumentDescription $description, $argumentName, $argumentValue) |
99
|
|
|
{ |
100
|
|
|
$priority = 0; |
101
|
|
|
if ($description->getName() === $argumentName) { |
102
|
|
|
$priority++; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if ($description->isScalar()) { |
106
|
|
|
return $priority; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if ($description->getType() === $this->argumentDescriptor->getValueType($argumentValue)) { |
110
|
|
|
$priority += 2; |
111
|
|
|
} elseif ($constraints->hasConstraint(ResolutionConstraint::STRICT_MATCHING, [ |
112
|
|
|
'type' => $description->getType(), |
113
|
|
|
])) { |
114
|
|
|
throw new ResolutionException(sprintf( |
115
|
|
|
'Strict matching for type "%s" can\'t be resolved', |
116
|
|
|
$description->getType() |
117
|
|
|
)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $priority; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param Resolutions $resolutions |
125
|
|
|
* @param ArgumentDescriptions $descriptions |
126
|
|
|
* |
127
|
|
|
* @throws ResolutionException |
128
|
|
|
*/ |
129
|
|
|
private function addMissingResolutions(Resolutions $resolutions, ArgumentDescriptions $descriptions) |
130
|
|
|
{ |
131
|
|
|
$missingResolutionPositions = $resolutions->getMissingResolutionPositions($descriptions->count()); |
132
|
|
|
|
133
|
|
|
foreach ($missingResolutionPositions as $position) { |
134
|
|
|
$description = $descriptions->getByPosition($position); |
135
|
|
|
if ($description->isRequired()) { |
136
|
|
|
throw new ArgumentResolutionException( |
137
|
|
|
sprintf( |
138
|
|
|
'Argument at position %d is required and wasn\'t resolved', |
139
|
|
|
$description->getPosition() |
140
|
|
|
), |
141
|
|
|
$description |
|
|
|
|
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$resolutions->add(new Resolution($description->getPosition(), $description->getDefaultValue(), 0)); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: