Completed
Push — master ( d76cf7...55ae92 )
by Nikola
02:19
created

VersionsCollection::matching()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Version;
6
7
use Countable;
8
use IteratorAggregate;
9
use ArrayIterator;
10
use Traversable;
11
use Version\Constraint\ConstraintInterface;
12
13
/**
14
 * @author Nikola Posa <[email protected]>
15
 */
16
class VersionsCollection implements Countable, IteratorAggregate
17
{
18
    public const SORT_ASC = 'ASC';
19
    public const SORT_DESC = 'DESC';
20
21
    /**
22
     * @var Version[]
23
     */
24
    protected $versions;
25
26 7
    public function __construct(Version $version, Version ...$versions)
27
    {
28 7
        $this->versions = array_merge([$version], $versions);
29 7
    }
30
31 2
    public function count() : int
32
    {
33 2
        return count($this->versions);
34
    }
35
36 3
    public function getIterator() : Traversable
37
    {
38 3
        return new ArrayIterator($this->versions);
39
    }
40
41 2
    public function sort(string $direction = self::SORT_ASC) : void
42
    {
43
        usort($this->versions, function (Version $a, Version $b) use ($direction) {
44 2
            $result = $a->compareTo($b);
45
46 2
            if ($direction === self::SORT_DESC) {
47 1
                $result *= -1;
48
            }
49
50 2
            return $result;
51 2
        });
52 2
    }
53
54 1
    public function matching(ConstraintInterface $constraint) : VersionsCollection
55
    {
56 1
        return new static(...array_filter(
57 1
            $this->versions,
58
            function (Version $version) use ($constraint) {
59 1
                return $version->matches($constraint);
60 1
            }
61
        ));
62
    }
63
}
64