REval::validate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 10
cc 3
nc 3
nop 1
crap 3
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