Completed
Push — master ( 17bdf6...eaddb5 )
by Nikola
02:22
created

ComparisonConstraint::getOperand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Version\Constraint;
6
7
use ReflectionClass;
8
use Version\Version;
9
use Version\Exception\InvalidComparisonConstraintException;
10
11
/** @noinspection PhpInconsistentReturnPointsInspection */
12
13
/**
14
 * @author Nikola Posa <[email protected]>
15
 */
16
class ComparisonConstraint implements ConstraintInterface
17
{
18
    public const OPERATOR_EQ = '=';
19
    public const OPERATOR_NEQ = '!=';
20
    public const OPERATOR_GT = '>';
21
    public const OPERATOR_GTE = '>=';
22
    public const OPERATOR_LT = '<';
23
    public const OPERATOR_LTE = '<=';
24
25
    /**
26
     * @var string
27
     */
28
    protected $operator;
29
30
    /**
31
     * @var Version
32
     */
33
    protected $operand;
34
35 15
    public function __construct(string $operator, Version $operand)
36
    {
37 15
        $this->validateOperator($operator);
38
39 14
        $this->operator = $operator;
40 14
        $this->operand = $operand;
41 14
    }
42
43
    /**
44
     * @param string $constraintString
45
     * @return ComparisonConstraint|CompositeConstraint
46
     */
47 11
    public static function fromString(string $constraintString)
48
    {
49 11
        static $parser = null;
50
51 11
        if (null === $parser) {
52 1
            $parser = new ComparisonConstraintParser();
53
        }
54
55 11
        return $parser->parse($constraintString);
56
    }
57
58 5
    public function getOperator() : string
59
    {
60 5
        return $this->operator;
61
    }
62
63 5
    public function getOperand() : Version
64
    {
65 5
        return $this->operand;
66
    }
67
68 10
    public function assert(Version $version) : bool
69
    {
70 10
        switch ($this->operator) {
71 10
            case self::OPERATOR_EQ:
72 2
                return $version->isEqualTo($this->operand);
73 8
            case self::OPERATOR_NEQ:
74 1
                return !$version->isEqualTo($this->operand);
75 7
            case self::OPERATOR_GT:
76 2
                return $version->isGreaterThan($this->operand);
77 5
            case self::OPERATOR_GTE:
78 3
                return $version->isGreaterOrEqualTo($this->operand);
79 3
            case self::OPERATOR_LT:
80 2
                return $version->isLessThan($this->operand);
81 1
            case self::OPERATOR_LTE:
82 1
                return $version->isLessOrEqualTo($this->operand);
83
        }
84
    }
85
86 15
    protected function validateOperator($operator) : void
87
    {
88 15
        static $validOperators = null;
89
90 15
        if (null === $validOperators) {
91
            $validOperators = (new ReflectionClass($this))->getConstants();
92
        }
93
94 15
        if (! in_array($operator, $validOperators, true)) {
95 1
            throw InvalidComparisonConstraintException::forUnsupportedOperator($operator);
96
        }
97 14
    }
98
}
99