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

ArgumentDescriptions::count()   A

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 0
1
<?php
2
3
namespace ArgumentResolver\Argument;
4
5
class ArgumentDescriptions implements \IteratorAggregate
6
{
7
    /**
8
     * @var ArgumentDescription[]
9
     */
10
    private $descriptions;
11
12
    /**
13
     * @param ArgumentDescription[] $descriptions
14
     */
15
    public function __construct(array $descriptions = [])
16
    {
17
        $this->descriptions = $descriptions;
18
    }
19
20
    /**
21
     * @param ArgumentDescription $description
22
     */
23
    public function add(ArgumentDescription $description)
24
    {
25
        $this->descriptions[] = $description;
26
    }
27
28
    /**
29
     * Sort `ArgumentDescription`s by position.
30
     *
31
     * @return ArgumentDescriptions
32
     */
33
    public function sortByPosition()
34
    {
35
        usort($this->descriptions, function (ArgumentDescription $left, ArgumentDescription $right) {
36
            return $left->getPosition() > $right->getPosition() ? 1 : -1;
37
        });
38
39
        return $this;
40
    }
41
42
    /**
43
     * {@inheritdoc).
44
     */
45
    public function getIterator()
46
    {
47
        return new \ArrayIterator($this->toArray());
48
    }
49
50
    /**
51
     * @return ArgumentDescription[]
52
     */
53
    public function toArray()
54
    {
55
        return $this->descriptions;
56
    }
57
58
    /**
59
     * @return int
60
     */
61
    public function count()
62
    {
63
        return count($this->descriptions);
64
    }
65
66
    /**
67
     * @param int $position
68
     *
69
     * @return ArgumentDescription|null
70
     */
71
    public function getByPosition($position)
72
    {
73
        foreach ($this->descriptions as $description) {
74
            if ($description->getPosition() === $position) {
75
                return $description;
76
            }
77
        }
78
79
        return;
80
    }
81
}
82