CompareOperator   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 95
ccs 39
cts 39
cp 1
rs 10
c 0
b 0
f 0
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setOperator() 0 3 1
B evaluate() 0 23 8
A configure() 0 25 3
1
<?php
2
/**
3
 * @author Aleksandar Panic
4
 * @license http://www.apache.org/licenses/LICENSE-2.0
5
 * @since 1.0.0
6
 **/
7
8
namespace ArekX\ArrayExpression\Operators;
9
10
use ArekX\ArrayExpression\Interfaces\Operator;
11
use ArekX\ArrayExpression\Interfaces\ValueParser;
12
13
/**
14
 * Class CompareOperator
15
 * Operator for handling comparisons.
16
 *
17
 * @package ArekX\ArrayExpression\Operators
18
 */
19
class CompareOperator extends BaseOperator
20
{
21
    const EQ = '=';
22
    const LT = '<';
23
    const GT = '>';
24
    const LTE = '<=';
25
    const GTE = '>=';
26
    const NE = '<>';
27
    const IN = 'in';
28
29
    /**
30
     * Operator which will be used for comparison.
31
     *
32
     * @var string
33
     */
34
    public $operator = self::EQ;
35
36
    /**
37
     * Value which will be taken from value parser.
38
     *
39
     * @var Operator
40
     */
41
    public $operandA;
42
43
    /**
44
     * @var Operator
45
     */
46
    public $operandB;
47
48
    /**
49
     * @inheritDoc
50
     */
51 12
    public function configure(array $config)
52
    {
53 12
        $this->setName($config[0] ?? 'unknown');
54
55 12
        if (count($config) < 3) {
56 1
            throw new \InvalidArgumentException("Minimum format must be satisfied: ['{$this->getName()}', <expressionA>, <expressionB>]");
57
        }
58
59 11
        $lastParam = $config[3] ?? null;
60
61 11
        $this->assertIsExpression($config[1]);
62
63 9
        if ($lastParam !== null) {
64 3
            $this->assertIsExpression($lastParam);
65 2
            $this->operandA = $this->parser->parse($config[1]);
66 2
            $this->setOperator($config[2]);
67 2
            $this->operandB = $this->parser->parse($lastParam);
68 2
            return;
69
        }
70
71 6
        $this->assertIsExpression($config[2]);
72
73 5
        $this->operandA = $this->parser->parse($config[1]);
74 5
        $this->operandB = $this->parser->parse($config[2]);
75 5
        $this->setOperator(self::EQ);
76 5
    }
77
78
    /**
79
     * Sets operator for result checking.
80
     *
81
     * @param string $operator
82
     */
83 7
    public function setOperator($operator)
84
    {
85 7
        $this->operator = $operator;
86 7
    }
87
88
    /**
89
     * @inheritDoc
90
     */
91 6
    public function evaluate(ValueParser $value)
92
    {
93 6
        $resultA = $this->operandA->evaluate($value);
94 6
        $resultB = $this->operandB->evaluate($value);
95
96 6
        switch ($this->operator) {
97 6
            case self::NE:
98 1
                return $resultA !== $resultB;
99 6
            case self::EQ:
100 5
                return $resultA === $resultB;
101 2
            case self::LT:
102 1
                return $resultA < $resultB;
103 2
            case self::LTE:
104 1
                return $resultA <= $resultB;
105 2
            case self::GT:
106 1
                return $resultA > $resultB;
107 2
            case self::GTE:
108 1
                return $resultA >= $resultB;
109 2
            case self::IN:
110 1
                return in_array($resultA, $resultB, true);
111
        }
112
113 1
        return $resultA === $resultB;
114
    }
115
}