|
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\Comparison\Constraint\Constraint; |
|
12
|
|
|
|
|
13
|
|
|
class VersionCollection implements Countable, IteratorAggregate |
|
14
|
|
|
{ |
|
15
|
|
|
public const SORT_ASC = 'ASC'; |
|
16
|
|
|
public const SORT_DESC = 'DESC'; |
|
17
|
|
|
|
|
18
|
|
|
/** @var Version[] */ |
|
19
|
|
|
protected $versions; |
|
20
|
|
|
|
|
21
|
18 |
|
public function __construct(Version ...$versions) |
|
22
|
|
|
{ |
|
23
|
18 |
|
$this->versions = $versions; |
|
24
|
18 |
|
} |
|
25
|
|
|
|
|
26
|
8 |
|
public function count(): int |
|
27
|
|
|
{ |
|
28
|
8 |
|
return count($this->versions); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
1 |
|
public function isEmpty(): bool |
|
32
|
|
|
{ |
|
33
|
1 |
|
return empty($this->versions); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
4 |
|
public function first(): ?Version |
|
37
|
|
|
{ |
|
38
|
4 |
|
return $this->versions[0] ?? null; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
3 |
|
public function last(): ?Version |
|
42
|
|
|
{ |
|
43
|
3 |
|
return $this->versions[count($this->versions) - 1] ?? null; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
3 |
|
public function getIterator(): Traversable |
|
47
|
|
|
{ |
|
48
|
3 |
|
return new ArrayIterator($this->versions); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
public function sortedAscending(): VersionCollection |
|
52
|
|
|
{ |
|
53
|
1 |
|
$versions = $this->versions; |
|
54
|
|
|
|
|
55
|
|
|
usort($versions, function (Version $a, Version $b) { |
|
56
|
1 |
|
return $a->compareTo($b); |
|
57
|
1 |
|
}); |
|
58
|
|
|
|
|
59
|
1 |
|
return new static(...$versions); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
2 |
|
public function sortedDescending(): VersionCollection |
|
63
|
|
|
{ |
|
64
|
2 |
|
$versions = $this->versions; |
|
65
|
|
|
|
|
66
|
|
|
usort($versions, function (Version $a, Version $b) { |
|
67
|
2 |
|
return $a->compareTo($b) * -1; |
|
68
|
2 |
|
}); |
|
69
|
|
|
|
|
70
|
2 |
|
return new static(...$versions); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
3 |
|
public function matching(Constraint $constraint): VersionCollection |
|
74
|
|
|
{ |
|
75
|
3 |
|
return new static(...array_filter( |
|
76
|
3 |
|
$this->versions, |
|
77
|
|
|
function (Version $version) use ($constraint) { |
|
78
|
3 |
|
return $version->matches($constraint); |
|
79
|
3 |
|
} |
|
80
|
|
|
)); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
2 |
|
public function majorReleases(): VersionCollection |
|
84
|
|
|
{ |
|
85
|
2 |
|
return new static(...array_filter( |
|
86
|
2 |
|
$this->versions, |
|
87
|
|
|
function (Version $version) { |
|
88
|
2 |
|
return $version->isMajorRelease(); |
|
89
|
2 |
|
} |
|
90
|
|
|
)); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
public function minorReleases(): VersionCollection |
|
94
|
|
|
{ |
|
95
|
1 |
|
return new static(...array_filter( |
|
96
|
1 |
|
$this->versions, |
|
97
|
|
|
function (Version $version) { |
|
98
|
1 |
|
return $version->isMinorRelease(); |
|
99
|
1 |
|
} |
|
100
|
|
|
)); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
1 |
|
public function patchReleases(): VersionCollection |
|
104
|
|
|
{ |
|
105
|
1 |
|
return new static(...array_filter( |
|
106
|
1 |
|
$this->versions, |
|
107
|
|
|
function (Version $version) { |
|
108
|
1 |
|
return $version->isPatchRelease(); |
|
109
|
1 |
|
} |
|
110
|
|
|
)); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return Version[] |
|
115
|
|
|
*/ |
|
116
|
7 |
|
public function toArray(): array |
|
117
|
|
|
{ |
|
118
|
7 |
|
return $this->versions; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|