Completed
Push — master ( f06ad0...9cb85d )
by Nico
06:26
created

Rule::isFalse()   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
/**
6
 * @license     http://opensource.org/licenses/mit-license.php MIT
7
 * @link        https://github.com/nicoSWD
8
 * @author      Nicolas Oelgart <[email protected]>
9
 */
10
namespace nicoSWD\Rules;
11
12
use Exception;
13
14
class Rule
15
{
16
    /** @var string */
17
    private $rule;
18
19
    /** @var Parser */
20
    private $parser;
21
22
    /** @var string */
23
    private $parsedRule = '';
24
25
    /** @var string */
26
    private $error = '';
27
28
    private $container;
29
30 60
    public function __construct(string $rule, array $variables = [])
31
    {
32 60
        $this->container = require __DIR__ . '/container.php';
33 60
        $this->parser = $this->container->parser($variables);
34 60
        $this->rule = $rule;
35 60
    }
36
37 4
    public function isTrue(): bool
38
    {
39 4
        return $this->container->evaluator()->evaluate(
40 4
            $this->parsedRule ?:
41 4
            $this->parser->parse($this->rule)
42
        );
43
    }
44
45 2
    public function isFalse(): bool
46
    {
47 2
        return !$this->isTrue();
48
    }
49
50
    /**
51
     * Tells whether a rule is valid (as in "can be parsed without error") or not.
52
     */
53 58
    public function isValid(): bool
54
    {
55
        try {
56 58
            $this->parsedRule = $this->parser->parse($this->rule);
57 56
        } catch (Exception $e) {
58 56
            $this->error = $e->getMessage();
59 56
            return false;
60
        }
61
62 2
        return true;
63
    }
64
65 58
    public function getError(): string
66
    {
67 58
        return $this->error;
68
    }
69
}
70