REval::compare()   B
last analyzed

Complexity

Conditions 8
Paths 8

Size

Total Lines 19
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 8

Importance

Changes 0
Metric Value
eloc 17
c 0
b 0
f 0
dl 0
loc 19
ccs 17
cts 17
cp 1
rs 8.4444
cc 8
nc 8
nop 3
crap 8
1
<?php
2
3
namespace kalanis\kw_table\core\Table\Rules;
4
5
6
use kalanis\kw_table\core\Interfaces\Table\IRule;
7
use kalanis\kw_table\core\TableException;
8
9
10
/**
11
 * Class REval
12
 * @package kalanis\kw_table\core\Table\Rules
13
 * Check content with validation against predefined value
14
 */
15
class REval extends ARule implements IRule
16
{
17 3
    public function validate($value): bool
18
    {
19 3
        if (preg_match('/([^\s]+)\s(.*)/i', strval($this->base), $matches)) {
20 2
            return $this->compare($value, $matches[1], $matches[2]);
21 2
        } elseif(preg_match('/(<|>|<=|>=|=|==)(.*)/i', strval($this->base), $matches)) {
22 1
            return $this->compare($value, $matches[1], $matches[2]);
23
        } else {
24 1
            throw new TableException('Unrecognized expression pattern');
25
        }
26
    }
27
28
    /**
29
     * @param mixed $value
30
     * @param string $expression
31
     * @param string $against
32
     * @throws TableException
33
     * @return bool
34
     */
35 2
    protected function compare($value, $expression, $against): bool
36
    {
37 2
        switch ($expression) {
38 2
            case '<':
39 1
                return $value < $against;
40 2
            case '>':
41 1
                return $value > $against;
42 2
            case '<=':
43 1
                return $value <= $against;
44 2
            case '>=':
45 1
                return $value >= $against;
46 2
            case '=':
47 1
                return $value == $against;
48 2
            case '!=':
49 1
                return $value != $against;
50 2
            case '==':
51 1
                return $value === $against;
52
            default:
53 1
                throw new TableException('Unrecognized expression sign');
54
        }
55
    }
56
}
57