|
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
|
|
|
use Version\Exception\CollectionIsEmptyException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @author Nikola Posa <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class VersionsCollection implements Countable, IteratorAggregate |
|
18
|
|
|
{ |
|
19
|
|
|
public const SORT_ASC = 'ASC'; |
|
20
|
|
|
public const SORT_DESC = 'DESC'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var Version[] |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $versions; |
|
26
|
|
|
|
|
27
|
20 |
|
public function __construct(Version ...$versions) |
|
28
|
|
|
{ |
|
29
|
20 |
|
$this->versions = $versions; |
|
30
|
20 |
|
} |
|
31
|
|
|
|
|
32
|
8 |
|
public function count() : int |
|
33
|
|
|
{ |
|
34
|
8 |
|
return count($this->versions); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
public function isEmpty() : bool |
|
38
|
|
|
{ |
|
39
|
1 |
|
return empty($this->versions); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
4 |
|
public function first() : Version |
|
43
|
|
|
{ |
|
44
|
4 |
|
if (empty($this->versions)) { |
|
45
|
1 |
|
throw new CollectionIsEmptyException('Invoking first() on an empty collection'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
3 |
|
return $this->versions[0]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
3 |
|
public function last() : Version |
|
52
|
|
|
{ |
|
53
|
3 |
|
if (empty($this->versions)) { |
|
54
|
1 |
|
throw new CollectionIsEmptyException('Invoking last() on an empty collection'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
2 |
|
return $this->versions[count($this->versions) - 1]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
4 |
|
public function getIterator() : Traversable |
|
61
|
|
|
{ |
|
62
|
4 |
|
return new ArrayIterator($this->versions); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @deprecated This method will be removed in 4.0.0. Use sorted() instead. |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function sort(string $direction = self::SORT_ASC) : void |
|
69
|
|
|
{ |
|
70
|
|
|
usort($this->versions, function (Version $a, Version $b) use ($direction) { |
|
71
|
1 |
|
$result = $a->compareTo($b); |
|
72
|
|
|
|
|
73
|
1 |
|
if ($direction === self::SORT_DESC) { |
|
74
|
|
|
$result *= -1; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
return $result; |
|
78
|
1 |
|
}); |
|
79
|
1 |
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
public function sortedAscending() : VersionsCollection |
|
82
|
|
|
{ |
|
83
|
1 |
|
$versions = $this->versions; |
|
84
|
|
|
|
|
85
|
|
|
usort($versions, function (Version $a, Version $b) { |
|
86
|
1 |
|
return $a->compareTo($b); |
|
87
|
1 |
|
}); |
|
88
|
|
|
|
|
89
|
1 |
|
return new static(...$versions); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
2 |
|
public function sortedDescending() : VersionsCollection |
|
93
|
|
|
{ |
|
94
|
2 |
|
$versions = $this->versions; |
|
95
|
|
|
|
|
96
|
|
|
usort($versions, function (Version $a, Version $b) { |
|
97
|
2 |
|
return $a->compareTo($b) * -1; |
|
98
|
2 |
|
}); |
|
99
|
|
|
|
|
100
|
2 |
|
return new static(...$versions); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
3 |
|
public function matching(ConstraintInterface $constraint) : VersionsCollection |
|
104
|
|
|
{ |
|
105
|
3 |
|
return new static(...array_filter( |
|
106
|
3 |
|
$this->versions, |
|
107
|
|
|
function (Version $version) use ($constraint) { |
|
108
|
3 |
|
return $version->matches($constraint); |
|
109
|
3 |
|
} |
|
110
|
|
|
)); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
2 |
|
public function majorReleases() : VersionsCollection |
|
114
|
|
|
{ |
|
115
|
2 |
|
return new static(...array_filter( |
|
116
|
2 |
|
$this->versions, |
|
117
|
|
|
function (Version $version) { |
|
118
|
2 |
|
return $version->isMajorRelease(); |
|
119
|
2 |
|
} |
|
120
|
|
|
)); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
1 |
|
public function minorReleases() : VersionsCollection |
|
124
|
|
|
{ |
|
125
|
1 |
|
return new static(...array_filter( |
|
126
|
1 |
|
$this->versions, |
|
127
|
|
|
function (Version $version) { |
|
128
|
1 |
|
return $version->isMinorRelease(); |
|
129
|
1 |
|
} |
|
130
|
|
|
)); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
1 |
|
public function patchReleases() : VersionsCollection |
|
134
|
|
|
{ |
|
135
|
1 |
|
return new static(...array_filter( |
|
136
|
1 |
|
$this->versions, |
|
137
|
|
|
function (Version $version) { |
|
138
|
1 |
|
return $version->isPatchRelease(); |
|
139
|
1 |
|
} |
|
140
|
|
|
)); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @return Version[] |
|
145
|
|
|
*/ |
|
146
|
7 |
|
public function toArray() : array |
|
147
|
|
|
{ |
|
148
|
7 |
|
return $this->versions; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|