1 | <?php |
||
23 | class Constraint |
||
24 | { |
||
25 | static private $parser; |
||
26 | |||
27 | 1 | static public function getParser() |
|
|
|||
28 | { |
||
29 | 1 | if (static::$parser === null) { |
|
30 | 1 | static::$parser = new VersionParser(); |
|
31 | } |
||
32 | |||
33 | 1 | return static::$parser; |
|
34 | } |
||
35 | |||
36 | 1 | static public function parse($constraint) |
|
40 | |||
41 | /** |
||
42 | * Merges two constraints. |
||
43 | * Doesn't resolve version conflicts. |
||
44 | * @param string $a |
||
45 | * @param string $b |
||
46 | * @return string |
||
47 | */ |
||
48 | 1 | static public function merge($a, $b) |
|
49 | { |
||
50 | 1 | $acon = static::parse($a); |
|
51 | 1 | $bcon = static::parse($b); |
|
52 | |||
53 | 1 | if ($acon instanceof EmptyConstraint) { |
|
54 | 1 | return $b; |
|
55 | } elseif ($bcon instanceof EmptyConstraint) { |
||
56 | 1 | return $a; |
|
57 | 1 | } elseif ($acon->matches($bcon) || $bcon->matches($acon)) { |
|
58 | 1 | return strlen($a)>strlen($b) ? $b : $a; |
|
59 | } else { |
||
60 | 1 | return $a . ' ' . $b; |
|
61 | } |
||
62 | } |
||
63 | |||
64 | 1 | static public function findMax(array $versions) |
|
65 | { |
||
66 | 1 | $versions = array_unique(array_values($versions)); |
|
67 | 1 | if (count($versions)<2) { |
|
68 | return reset($versions); |
||
69 | } |
||
70 | 1 | $max = $versions[0]; |
|
71 | 1 | for ($i=1; $i<= count($versions); $i++) { |
|
72 | 1 | $cur = $versions[$i]; |
|
73 | 1 | if (Comparator::compare($cur, '>', $max)) { |
|
74 | 1 | $max = $cur; |
|
75 | } |
||
76 | } |
||
77 | |||
78 | 1 | return trim($max); |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * Is constraint disjunctive. |
||
83 | * TODO redo after Semver will have such function. |
||
84 | * @param string $constraint |
||
85 | * @return bool |
||
86 | */ |
||
87 | static public function isDisjunctive($constraint) |
||
91 | |||
92 | } |
||
93 |