1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ArgumentResolver\Resolution; |
4
|
|
|
|
5
|
|
|
class Resolutions implements \IteratorAggregate |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var Resolution[] |
9
|
|
|
*/ |
10
|
|
|
private $resolutions; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @param Resolution[] $resolutions |
14
|
|
|
*/ |
15
|
|
|
public function __construct(array $resolutions = []) |
16
|
|
|
{ |
17
|
|
|
$this->resolutions = $resolutions; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param Resolution $resolution |
22
|
|
|
*/ |
23
|
|
|
public function add(Resolution $resolution) |
24
|
|
|
{ |
25
|
|
|
$this->resolutions[] = $resolution; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return string[] |
30
|
|
|
*/ |
31
|
|
|
public function argumentNames() |
32
|
|
|
{ |
33
|
|
|
return array_map(function (Resolution $resolution) { |
34
|
|
|
return $resolution->argumentName(); |
35
|
|
|
}, $this->resolutions); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param array $resolutions |
40
|
|
|
*/ |
41
|
|
|
public function addCollection(array $resolutions) |
42
|
|
|
{ |
43
|
|
|
foreach ($resolutions as $resolution) { |
44
|
|
|
$this->add($resolution); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Sort `Resolution`s by priority. |
50
|
|
|
* |
51
|
|
|
* The higher the priority is, the first the `Resolution` will be. |
52
|
|
|
* |
53
|
|
|
* @return Resolutions |
54
|
|
|
*/ |
55
|
|
|
public function sortByPriority() |
56
|
|
|
{ |
57
|
|
|
usort($this->resolutions, function (Resolution $left, Resolution $right) { |
58
|
|
|
return $left->priority() < $right->priority() ? 1 : -1; |
59
|
|
|
}); |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
|
|
public function toArgumentsArray() |
68
|
|
|
{ |
69
|
|
|
$arguments = []; |
70
|
|
|
foreach ($this->resolutions as $resolution) { |
71
|
|
|
if (array_key_exists($resolution->position(), $arguments)) { |
72
|
|
|
continue; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$arguments[$resolution->position()] = $resolution->value(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
ksort($arguments); |
79
|
|
|
|
80
|
|
|
return $arguments; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc). |
85
|
|
|
*/ |
86
|
|
|
public function getIterator() |
87
|
|
|
{ |
88
|
|
|
return new \ArrayIterator($this->toArray()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return Resolution[] |
93
|
|
|
*/ |
94
|
|
|
public function toArray() |
95
|
|
|
{ |
96
|
|
|
return $this->resolutions; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param int $expectedArguments The number of expected arguments |
101
|
|
|
* |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
public function getMissingResolutionPositions($expectedArguments = null) |
105
|
|
|
{ |
106
|
|
|
$positions = []; |
107
|
|
|
|
108
|
|
|
if (null !== ($highestPosition = $this->getHighestPosition())) { |
109
|
|
|
foreach (range(0, $highestPosition) as $position) { |
110
|
|
|
if (null === $this->getByPosition($position)) { |
111
|
|
|
$positions[] = $position; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if (is_int($expectedArguments) && $expectedArguments > 0) { |
117
|
|
|
for ($i = $highestPosition === null ? -1 : $highestPosition; $i < ($expectedArguments - 1); $i++) { |
118
|
|
|
$positions[] = $i + 1; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $positions; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return int|null |
127
|
|
|
*/ |
128
|
|
|
private function getHighestPosition() |
129
|
|
|
{ |
130
|
|
|
if (count($this->resolutions) == 0) { |
131
|
|
|
return null; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$position = 0; |
135
|
|
|
foreach ($this->resolutions as $resolution) { |
136
|
|
|
$position = max($position, $resolution->position()); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $position; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param $position |
144
|
|
|
* |
145
|
|
|
* @return Resolution|null |
146
|
|
|
*/ |
147
|
|
|
private function getByPosition($position) |
148
|
|
|
{ |
149
|
|
|
foreach ($this->resolutions as $resolution) { |
150
|
|
|
if ($resolution->position() === $position) { |
151
|
|
|
return $resolution; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|