Resolutions::add()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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
        if (null !== ($highestPosition = $this->getHighestPosition())) {
99
            foreach (range(0, $highestPosition) as $position) {
100
                if (null === $this->getByPosition($position)) {
101
                    $positions[] = $position;
102
                }
103
            }
104
        }
105
106
        if (is_int($expectedArguments) && $expectedArguments > 0) {
107
            for ($i = $highestPosition === null ? -1 : $highestPosition; $i < ($expectedArguments - 1); $i++) {
108
                $positions[] = $i + 1;
109
            }
110
        }
111
112
        return $positions;
113
    }
114
115
    /**
116
     * @return int|null
117
     */
118
    private function getHighestPosition()
119
    {
120
        if (count($this->resolutions) == 0) {
121
            return null;
122
        }
123
124
        $position = 0;
125
        foreach ($this->resolutions as $resolution) {
126
            $position = max($position, $resolution->position());
127
        }
128
129
        return $position;
130
    }
131
132
    /**
133
     * @param $position
134
     *
135
     * @return Resolution|null
136
     */
137
    private function getByPosition($position)
138
    {
139
        foreach ($this->resolutions as $resolution) {
140
            if ($resolution->position() === $position) {
141
                return $resolution;
142
            }
143
        }
144
145
        return;
146
    }
147
}
148