Completed
Push — master ( a47a63...7aba9f )
by Nikola
9s
created

Constraint::fromString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
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\Constraint;
13
14
use Version\Version;
15
use Version\Exception\InvalidConstraintException;
16
use Version\Constraint\Parser\StandardParser;
17
18
/**
19
 * @author Nikola Posa <[email protected]>
20
 */
21
class Constraint implements ConstraintInterface
22
{
23
    const OPERATOR_EQ = '=';
24
    const OPERATOR_NEQ = '!=';
25
    const OPERATOR_GT = '>';
26
    const OPERATOR_GTE = '>=';
27
    const OPERATOR_LT = '<';
28
    const OPERATOR_LTE = '<=';
29
30
    /**
31
     * @var string
32
     */
33
    protected $operator;
34
35
    /**
36
     * @var Version
37
     */
38
    protected $operand;
39
40
    /**
41
     * @var array
42
     */
43
    private static $validOperators = [
44
        self::OPERATOR_EQ,
45
        self::OPERATOR_NEQ,
46
        self::OPERATOR_GT,
47
        self::OPERATOR_GTE,
48
        self::OPERATOR_LT,
49
        self::OPERATOR_LTE,
50
    ];
51
52 15
    private function __construct($operator, Version $operand)
53
    {
54 15
        $this->operator = $operator;
55 15
        $this->operand = $operand;
56 15
    }
57
58
    /**
59
     * @param string $operator
60
     * @param Version $operand
61
     * @return self
62
     */
63 16
    public static function fromProperties($operator, Version $operand)
64
    {
65 16
        if (!self::isOperatorValid($operator)) {
66 1
            throw InvalidConstraintException::forOperator($operator);
67
        }
68
69 15
        return new self($operator, $operand);
70
    }
71
72 16
    protected static function isOperatorValid($operator)
73
    {
74 16
        return in_array($operator, self::$validOperators);
75
    }
76
77
    /**
78
     * @param string $constraintString
79
     * @return self
80
     */
81 2
    public static function fromString($constraintString)
82
    {
83 2
        $parser = new StandardParser();
84
85 2
        return $parser->parse($constraintString);
86
    }
87
88
    /**
89
     * @return string
90
     */
91 5
    public function getOperator()
92
    {
93 5
        return $this->operator;
94
    }
95
96
    /**
97
     * @return Version
98
     */
99 5
    public function getOperand()
100
    {
101 5
        return $this->operand;
102
    }
103
104
    /**
105
     * {@inheritDoc}
106
     */
107 10
    public function assert(Version $version)
108
    {
109 10
        switch ($this->operator) {
110 10
            case self::OPERATOR_EQ :
111 2
                return $version->isEqualTo($this->operand);
112 8
            case self::OPERATOR_NEQ :
113 1
                return !$version->isEqualTo($this->operand);
114 7
            case self::OPERATOR_GT :
115 2
                return $version->isGreaterThan($this->operand);
116 5
            case self::OPERATOR_GTE :
117 2
                return $version->isGreaterOrEqualTo($this->operand);
118 4
            case self::OPERATOR_LT :
119 2
                return $version->isLessThan($this->operand);
120 2
            case self::OPERATOR_LTE :
121 2
                return $version->isLessOrEqualTo($this->operand);
122
        }
123
    }
124
}
125