Completed
Push — master ( 3dab4c...0f5459 )
by Max
01:20
created

CheckTrait::check()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 8.8333
c 0
b 0
f 0
nc 7
cc 7
nop 1
1
<?php
2
3
/*
4
 *  @copyright (c) 2019 Mendel <[email protected]>
5
 *  @license see license.txt
6
 */
7
8
namespace drycart\data;
9
10
/**
11
 *
12
 * @author mendel
13
 */
14
trait CheckTrait
15
{
16
    /**
17
     * Get field data by field name
18
     * 
19
     * @param string $name name for access
20
     * @return mixed
21
     */
22
    abstract public function get(string $name);
23
    
24
    /**
25
     * Check if data satisfies the condition
26
     * @param array $conditions
27
     * @return bool
28
     */
29
    public function check(array $conditions) : bool
30
    {
31
        $args = CompareHelper::tryPrepareSimpleRules($conditions);
32
        $type = array_shift($args);
33
        switch ($type) {
34
            case 'AND':
35
            case 'and':
36
                return $this->checkAnd($args);
37
            case 'OR':
38
            case 'or':
39
                return $this->checkOr($args);
40
            case 'NOT':
41
            case 'not':
42
                return !$this->check($args[0]);
43
            default:
44
                return $this->checkField($type, $args[0], $args[1]);
45
        }
46
    }
47
48
    /**
49
     * Check AND condition
50
     * @param array $conditions
51
     * @return bool
52
     */
53
    private function checkAnd(array $conditions) : bool
54
    {
55
        foreach($conditions as $line) {
56
            if(!$this->check($line)) {
57
                return false;
58
            }
59
        }
60
        return true;
61
    }
62
63
    /**
64
     * Check OR condition
65
     * @param array $conditions
66
     * @return bool
67
     */
68
    private function checkOr(array $conditions) : bool
69
    {
70
        foreach($conditions as $line) {
71
            if($this->check($line)) {
72
                return true;
73
            }
74
        }
75
        return false;
76
    }
77
78
    /**
79
     * Check/compare some field by rule and some value
80
     * @param string $staredRule
81
     * @param mixed $arg1
82
     * @param mixed $arg2
83
     * @return bool
84
     */
85
    private function checkField(string $staredRule, $arg1, $arg2) : bool
86
    {
87
        [$rulePrefix, $rule] = StrHelper::findPrefix($staredRule, ['*']);
0 ignored issues
show
Bug introduced by
The variable $rulePrefix does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $rule does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
88
        $value1 = $this->get($arg1);
89
        if($rulePrefix == '*') {
90
            $value2 = $this->get($arg2);
91
        } else {
92
            $value2 = $arg2;
93
        }
94
        return CompareHelper::checkRule($rule, $value1, $value2);
95
    }
96
}
97