Completed
Push — master ( b2db3a...f1ef23 )
by Samuel
12s
created

Resolutions::getMissingResolutionPositions()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 6
nop 1
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
     * @param array $resolutions
30
     */
31
    public function addCollection(array $resolutions)
32
    {
33
        foreach ($resolutions as $resolution) {
34
            $this->add($resolution);
35
        }
36
    }
37
38
    /**
39
     * Sort `Resolution`s by priority.
40
     *
41
     * The higher the priority is, the first the `Resolution` will be.
42
     *
43
     * @return Resolutions
44
     */
45
    public function sortByPriority()
46
    {
47
        usort($this->resolutions, function (Resolution $left, Resolution $right) {
48
            return $left->priority() < $right->priority() ? 1 : -1;
49
        });
50
51
        return $this;
52
    }
53
54
    /**
55
     * @return array
56
     */
57
    public function toArgumentsArray()
58
    {
59
        $arguments = [];
60
        foreach ($this->resolutions as $resolution) {
61
            if (array_key_exists($resolution->position(), $arguments)) {
62
                continue;
63
            }
64
65
            $arguments[$resolution->position()] = $resolution->value();
66
        }
67
68
        ksort($arguments);
69
70
        return $arguments;
71
    }
72
73
    /**
74
     * {@inheritdoc).
75
     */
76
    public function getIterator()
77
    {
78
        return new \ArrayIterator($this->toArray());
79
    }
80
81
    /**
82
     * @return Resolution[]
83
     */
84
    public function toArray()
85
    {
86
        return $this->resolutions;
87
    }
88
89
    /**
90
     * @param int $expectedArguments The number of expected arguments
91
     *
92
     * @return array
93
     */
94
    public function getMissingResolutionPositions($expectedArguments = null)
95
    {
96
        $positions = [];
97
98
        foreach (range(0, $highestPosition = $this->getHighestPosition()) as $position) {
99
            if (null === $this->getByPosition($position)) {
100
                $positions[] = $position;
101
            }
102
        }
103
104
        if (is_int($expectedArguments)) {
105
            for ($i = $highestPosition + 1; $i < $expectedArguments; $i++) {
106
                $positions[] = $i;
107
            }
108
        }
109
110
        return $positions;
111
    }
112
113
    /**
114
     * @return int
115
     */
116
    private function getHighestPosition()
117
    {
118
        $position = 0;
119
120
        foreach ($this->resolutions as $resolution) {
121
            $position = max($position, $resolution->position());
122
        }
123
124
        return $position;
125
    }
126
127
    /**
128
     * @param $position
129
     *
130
     * @return Resolution|null
131
     */
132
    private function getByPosition($position)
133
    {
134
        foreach ($this->resolutions as $resolution) {
135
            if ($resolution->position() === $position) {
136
                return $resolution;
137
            }
138
        }
139
140
        return;
141
    }
142
}
143