1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Version package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Nikola Posa <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For full copyright and license information, please refer to the LICENSE file, |
9
|
|
|
* located at the package root folder. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Version; |
13
|
|
|
|
14
|
|
|
use Countable; |
15
|
|
|
use IteratorAggregate; |
16
|
|
|
use ArrayIterator; |
17
|
|
|
use Version\Exception\InvalidArgumentException; |
18
|
|
|
use Version\Constraint\ConstraintInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Nikola Posa <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
final class VersionsCollection implements Countable, IteratorAggregate |
24
|
|
|
{ |
25
|
|
|
const SORT_ASC = 'ASC'; |
26
|
|
|
const SORT_DESC = 'DESC'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Version[] |
30
|
|
|
*/ |
31
|
|
|
private $versions = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param array $versions |
35
|
|
|
*/ |
36
|
10 |
|
public function __construct(array $versions) |
37
|
|
|
{ |
38
|
10 |
|
foreach ($versions as $version) { |
39
|
10 |
|
if (is_string($version)) { |
40
|
10 |
|
$version = Version::fromString($version); |
41
|
10 |
|
} elseif (!$version instanceof Version) { |
42
|
1 |
|
throw new InvalidArgumentException(sprintf( |
43
|
1 |
|
'Item in the versions array should be either string or Version instance, %s given', |
44
|
1 |
|
gettype($version) |
45
|
1 |
|
)); |
46
|
|
|
} |
47
|
|
|
|
48
|
10 |
|
$this->versions[] = $version; |
49
|
10 |
|
} |
50
|
9 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param array $versions |
54
|
|
|
* @return self |
55
|
|
|
*/ |
56
|
9 |
|
public static function fromArray(array $versions) |
57
|
|
|
{ |
58
|
9 |
|
return new self($versions); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return int |
63
|
|
|
*/ |
64
|
2 |
|
public function count() |
65
|
|
|
{ |
66
|
2 |
|
return count($this->versions); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return \Iterator |
71
|
|
|
*/ |
72
|
5 |
|
public function getIterator() |
73
|
|
|
{ |
74
|
5 |
|
return new ArrayIterator($this->versions); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string|bool $direction OPTIONAL |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
4 |
|
public function sort($direction = self::SORT_ASC) |
82
|
|
|
{ |
83
|
4 |
|
if (is_bool($direction)) { |
84
|
|
|
//backwards-compatibility |
85
|
2 |
|
$direction = (true === $direction) ? self::SORT_DESC : self::SORT_ASC; |
86
|
2 |
|
} |
87
|
|
|
|
88
|
|
|
usort($this->versions, function (Version $a, Version $b) use ($direction) { |
89
|
4 |
|
$result = $a->compareTo($b); |
90
|
|
|
|
91
|
4 |
|
if ($direction == self::SORT_DESC) { |
92
|
2 |
|
$result *= -1; |
93
|
2 |
|
} |
94
|
|
|
|
95
|
4 |
|
return $result; |
96
|
4 |
|
}); |
97
|
4 |
|
} |
98
|
|
|
|
99
|
1 |
|
public function matching(ConstraintInterface $constraint) |
100
|
|
|
{ |
101
|
1 |
|
return new self(array_filter( |
102
|
1 |
|
$this->versions, |
103
|
1 |
|
function (Version $version) use ($constraint) { |
104
|
1 |
|
return $version->matches($constraint); |
105
|
|
|
} |
106
|
1 |
|
)); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|