CheckHelper::checkField()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 4
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
 * Helper for flexible conditions 
12
 *
13
 * @author mendel
14
 */
15
class CheckHelper
16
{
17
    protected static $allRules = [];
18
    
19
    /**
20
     * Check if data satisfies the condition
21
     * 
22
     * @param mixed $data
23
     * @param array $conditions
24
     * @return bool
25
     */
26
    public static function check($data, array $conditions) : bool
27
    {
28
        if(empty($conditions)) {
29
            return true;
30
        }
31
        $args = self::tryPrepareSimpleRules($conditions);
32
        $type = array_shift($args);
33
        switch (strtolower($type)) {
34
            case 'and':
35
                return self::checkAnd($data, $args);
36
            case 'or':
37
                return self::checkOr($data, $args);
38
            case 'not':
39
                return !self::check($data, $args[0]);
40
            default:
41
                return self::checkField($data, $type, $args[0], $args[1]);
42
        }
43
    }
44
    
45
    /**
46
     * If array of rules is in "simple format"
47
     * convert it to full format
48
     * 
49
     * @param array $rules
50
     * @return array
51
     */
52
    protected static function tryPrepareSimpleRules(array $rules) : array
53
    {
54
        self::initAllRules();
55
        if(empty($rules) or isset($rules[0])) {
56
            return $rules;
57
        }
58
        $result = ['and'];
59
        foreach($rules as $fieldRule=>$arg2) {
60
            [$rule, $arg1] = StrHelper::findPrefix($fieldRule, static::$allRules, '=');
0 ignored issues
show
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...
Bug introduced by
The variable $arg1 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...
61
            $result[] = [$rule, $arg1, $arg2];
62
        }
63
        return $result;
64
    }
65
66
    /**
67
     * Init list of rules if not initialized
68
     * 
69
     * @return void
70
     */
71
    public static function initAllRules() : void
72
    {
73
        if(empty(static::$allRules)) {
74
            foreach(CompareHelper::RULES as $rule) {
75
                static::$allRules[] = '*'.$rule;
76
                static::$allRules[] = $rule;
77
            }
78
            foreach(array_keys(CompareHelper::RULES_ALIASES) as $rule) {
79
                static::$allRules[] = '*'.$rule;
80
                static::$allRules[] = $rule;
81
            }
82
            // Sort by lenght
83
            usort(static::$allRules, function(string $a, string $b) : int {
84
                return strlen($b) <=> strlen($a); // for reversal result
85
            });
86
        }
87
    }
88
    
89
    /**
90
     * Check AND condition
91
     * 
92
     * @param mixed $data
93
     * @param array $conditions
94
     * @return bool
95
     */
96
    protected static function checkAnd($data, array $conditions) : bool
97
    {
98
        foreach($conditions as $line) {
99
            if(!self::check($data,$line)) {
100
                return false;
101
            }
102
        }
103
        return true;
104
    }
105
106
    /**
107
     * Check OR condition
108
     * 
109
     * @param mixed $data
110
     * @param array $conditions
111
     * @return bool
112
     */
113
    protected static function checkOr($data, array $conditions) : bool
114
    {
115
        foreach($conditions as $line) {
116
            if(self::check($data,$line)) {
117
                return true;
118
            }
119
        }
120
        return false;
121
    }
122
123
    /**
124
     * Check/compare some field by rule and some value
125
     * 
126
     * @param mixed $data
127
     * @param string $staredRule
128
     * @param mixed $arg1
129
     * @param mixed $arg2
130
     * @return bool
131
     */
132
    protected static function checkField($data, string $staredRule, $arg1, $arg2) : bool
133
    {
134
        [$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...
135
        $value1 = $data->$arg1;
136
        if($rulePrefix == '*') {
137
            $value2 = $data->$arg2;
138
        } else {
139
            $value2 = $arg2;
140
        }
141
        return CompareHelper::compareByRule($rule, $value1, $value2);
142
    }
143
}
144