RuleHelper   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 79
rs 10
c 0
b 0
f 0
wmc 23

1 Method

Rating   Name   Duplication   Size   Complexity  
D evaluate() 0 68 23
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Smoren\Schemator\Helpers;
6
7
/**
8
 * @internal
9
 */
10
class RuleHelper
11
{
12
    /**
13
     * Checks rule for value.
14
     *
15
     * @param mixed $value value to check
16
     * @param string $rule rule for checking
17
     * @param array<mixed> $args arguments for rule
18
     *
19
     * @return bool
20
     */
21
    public static function evaluate($value, string $rule, array $args): bool
22
    {
23
        switch ($rule) {
24
            case '=':
25
                /**
26
                 * @var scalar $lhs
27
                 * @var scalar $rhs
28
                 */
29
                [$lhs, $rhs] = [$value, $args[0]];
30
                if (strval($lhs) === strval($rhs)) {
31
                    return true;
32
                }
33
                break;
34
            case '!=':
35
                /**
36
                 * @var scalar $lhs
37
                 * @var scalar $rhs
38
                 */
39
                [$lhs, $rhs] = [$value, $args[0]];
40
                if (strval($lhs) !== strval($rhs)) {
41
                    return true;
42
                }
43
                break;
44
            case '>':
45
                if ($value > $args[0]) {
46
                    return true;
47
                }
48
                break;
49
            case '>=':
50
                if ($value >= $args[0]) {
51
                    return true;
52
                }
53
                break;
54
            case '<':
55
                if ($value < $args[0]) {
56
                    return true;
57
                }
58
                break;
59
            case '<=':
60
                if ($value <= $args[0]) {
61
                    return true;
62
                }
63
                break;
64
            case 'between':
65
                if ($value >= $args[0] && $value <= $args[1]) {
66
                    return true;
67
                }
68
                break;
69
            case 'between strict':
70
                if ($value > $args[0] && $value < $args[1]) {
71
                    return true;
72
                }
73
                break;
74
            case 'in':
75
                /** @var array{array<mixed>} $args */
76
                if (in_array($value, $args[0])) {
77
                    return true;
78
                }
79
                break;
80
            case 'not in':
81
                /** @var array{array<mixed>} $args */
82
                if (!in_array($value, $args[0])) {
83
                    return true;
84
                }
85
                break;
86
        }
87
88
        return false;
89
    }
90
}
91