1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Version\Constraint; |
6
|
|
|
|
7
|
|
|
use Version\Version; |
8
|
|
|
use Version\Exception\InvalidConstraintException; |
9
|
|
|
|
10
|
|
|
/** @noinspection PhpInconsistentReturnPointsInspection */ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author Nikola Posa <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class ComparisonConstraint implements ConstraintInterface |
16
|
|
|
{ |
17
|
|
|
public const OPERATOR_EQ = '='; |
18
|
|
|
public const OPERATOR_NEQ = '!='; |
19
|
|
|
public const OPERATOR_GT = '>'; |
20
|
|
|
public const OPERATOR_GTE = '>='; |
21
|
|
|
public const OPERATOR_LT = '<'; |
22
|
|
|
public const OPERATOR_LTE = '<='; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $operator; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Version |
31
|
|
|
*/ |
32
|
|
|
protected $operand; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private static $validOperators = [ |
38
|
|
|
self::OPERATOR_EQ, |
39
|
|
|
self::OPERATOR_NEQ, |
40
|
|
|
self::OPERATOR_GT, |
41
|
|
|
self::OPERATOR_GTE, |
42
|
|
|
self::OPERATOR_LT, |
43
|
|
|
self::OPERATOR_LTE, |
44
|
|
|
]; |
45
|
|
|
|
46
|
15 |
|
public function __construct(string $operator, Version $operand) |
47
|
|
|
{ |
48
|
15 |
|
if (! in_array($operator, static::$validOperators, true)) { |
|
|
|
|
49
|
1 |
|
throw InvalidConstraintException::forOperator($operator); |
50
|
|
|
} |
51
|
|
|
|
52
|
14 |
|
$this->operator = $operator; |
53
|
14 |
|
$this->operand = $operand; |
54
|
14 |
|
} |
55
|
|
|
|
56
|
11 |
|
public static function fromString(string $constraintString) : ConstraintInterface |
57
|
|
|
{ |
58
|
11 |
|
static $parser = null; |
59
|
|
|
|
60
|
11 |
|
if (null === $parser) { |
61
|
1 |
|
$parser = new ComparisonConstraintParser(); |
62
|
|
|
} |
63
|
|
|
|
64
|
11 |
|
return $parser->parse($constraintString); |
65
|
|
|
} |
66
|
|
|
|
67
|
5 |
|
public function getOperator() : string |
68
|
|
|
{ |
69
|
5 |
|
return $this->operator; |
70
|
|
|
} |
71
|
|
|
|
72
|
5 |
|
public function getOperand() : Version |
73
|
|
|
{ |
74
|
5 |
|
return $this->operand; |
75
|
|
|
} |
76
|
|
|
|
77
|
10 |
|
public function assert(Version $version) : bool |
78
|
|
|
{ |
79
|
10 |
|
switch ($this->operator) { |
80
|
10 |
|
case self::OPERATOR_EQ: |
81
|
2 |
|
return $version->isEqualTo($this->operand); |
82
|
8 |
|
case self::OPERATOR_NEQ: |
83
|
1 |
|
return !$version->isEqualTo($this->operand); |
84
|
7 |
|
case self::OPERATOR_GT: |
85
|
2 |
|
return $version->isGreaterThan($this->operand); |
86
|
5 |
|
case self::OPERATOR_GTE: |
87
|
3 |
|
return $version->isGreaterOrEqualTo($this->operand); |
88
|
3 |
|
case self::OPERATOR_LT: |
89
|
2 |
|
return $version->isLessThan($this->operand); |
90
|
1 |
|
case self::OPERATOR_LTE: |
91
|
1 |
|
return $version->isLessOrEqualTo($this->operand); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: