1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Version\Comparison\Constraint; |
6
|
|
|
|
7
|
|
|
use ReflectionClass; |
8
|
|
|
use Version\Version; |
9
|
|
|
use Version\Comparison\Exception\InvalidOperationConstraint; |
10
|
|
|
|
11
|
|
|
class OperationConstraint implements Constraint |
12
|
|
|
{ |
13
|
|
|
public const OPERATOR_EQ = '='; |
14
|
|
|
public const OPERATOR_NEQ = '!='; |
15
|
|
|
public const OPERATOR_GT = '>'; |
16
|
|
|
public const OPERATOR_GTE = '>='; |
17
|
|
|
public const OPERATOR_LT = '<'; |
18
|
|
|
public const OPERATOR_LTE = '<='; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
protected $operator; |
22
|
|
|
|
23
|
|
|
/** @var Version */ |
24
|
|
|
protected $operand; |
25
|
|
|
|
26
|
16 |
|
final public function __construct(string $operator, Version $operand) |
27
|
|
|
{ |
28
|
16 |
|
$this->validateOperator($operator); |
29
|
|
|
|
30
|
15 |
|
$this->operator = $operator; |
31
|
15 |
|
$this->operand = $operand; |
32
|
15 |
|
} |
33
|
|
|
|
34
|
1 |
|
public static function equalTo(Version $operand) |
35
|
|
|
{ |
36
|
1 |
|
return new static(self::OPERATOR_EQ, $operand); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public static function notEqualTo(Version $operand) |
40
|
|
|
{ |
41
|
|
|
return new static(self::OPERATOR_NEQ, $operand); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public static function greaterThan(Version $operand) |
45
|
|
|
{ |
46
|
|
|
return new static(self::OPERATOR_GT, $operand); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public static function greaterOrEqualTo(Version $operand) |
50
|
|
|
{ |
51
|
|
|
return new static(self::OPERATOR_GTE, $operand); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public static function lessThan(Version $operand) |
55
|
|
|
{ |
56
|
|
|
return new static(self::OPERATOR_LT, $operand); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public static function lessOrEqualTo(Version $operand) |
60
|
|
|
{ |
61
|
|
|
return new static(self::OPERATOR_LTE, $operand); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $constraintString |
66
|
|
|
* @return OperationConstraint|CompositeConstraint |
67
|
|
|
*/ |
68
|
10 |
|
public static function fromString(string $constraintString) |
69
|
|
|
{ |
70
|
10 |
|
static $parser = null; |
71
|
|
|
|
72
|
10 |
|
if (null === $parser) { |
73
|
1 |
|
$parser = new OperationConstraintParser(); |
74
|
|
|
} |
75
|
|
|
|
76
|
10 |
|
return $parser->parse($constraintString); |
77
|
|
|
} |
78
|
|
|
|
79
|
6 |
|
public function getOperator(): string |
80
|
|
|
{ |
81
|
6 |
|
return $this->operator; |
82
|
|
|
} |
83
|
|
|
|
84
|
6 |
|
public function getOperand(): Version |
85
|
|
|
{ |
86
|
6 |
|
return $this->operand; |
87
|
|
|
} |
88
|
|
|
|
89
|
10 |
|
public function assert(Version $version): bool |
90
|
|
|
{ |
91
|
10 |
|
switch ($this->operator) { |
92
|
10 |
|
case self::OPERATOR_EQ: |
93
|
2 |
|
return $version->isEqualTo($this->operand); |
94
|
8 |
|
case self::OPERATOR_NEQ: |
95
|
1 |
|
return !$version->isEqualTo($this->operand); |
96
|
7 |
|
case self::OPERATOR_GT: |
97
|
2 |
|
return $version->isGreaterThan($this->operand); |
98
|
5 |
|
case self::OPERATOR_GTE: |
99
|
3 |
|
return $version->isGreaterOrEqualTo($this->operand); |
100
|
3 |
|
case self::OPERATOR_LT: |
101
|
2 |
|
return $version->isLessThan($this->operand); |
102
|
1 |
|
case self::OPERATOR_LTE: |
103
|
1 |
|
return $version->isLessOrEqualTo($this->operand); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
16 |
|
protected function validateOperator(string $operator): void |
108
|
|
|
{ |
109
|
16 |
|
static $validOperators = null; |
110
|
|
|
|
111
|
16 |
|
if (null === $validOperators) { |
112
|
|
|
$validOperators = (new ReflectionClass($this))->getConstants(); |
113
|
|
|
} |
114
|
|
|
|
115
|
16 |
|
if (! in_array($operator, $validOperators, true)) { |
116
|
1 |
|
throw InvalidOperationConstraint::unsupportedOperator($operator); |
117
|
|
|
} |
118
|
15 |
|
} |
119
|
|
|
} |
120
|
|
|
|