Test Failed
Push — master ( e3b206...99d1d4 )
by Enjoys
02:23 queued 23s
created

Length::greaterThan()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\Forms\Rule;
6
7
use Enjoys\Forms\Element;
8
use Enjoys\Forms\Exception\ExceptionRule;
9
use Enjoys\Forms\Interfaces\Ruleable;
10
use Enjoys\Forms\Traits\Request;
11
12
class Length implements RuleInterface
13
{
14
    use Request;
15
16
    /**
17
     * @var string[]
18
     */
19
    private array $operatorToMethodTranslation = [
20
        '==' => 'equal',
21
        '!=' => 'notEqual',
22
        '>' => 'greaterThan',
23
        '<' => 'lessThan',
24
        '>=' => 'greaterThanOrEqual',
25
        '<=' => 'lessThanOrEqual',
26
    ];
27
    private string $message;
28
    private array $params;
29
30
    public function __construct(array $params, ?string $message = null)
31
    {
32
        $this->message = $message ?? 'Ошибка ввода';
33
        $this->params = $params;
34
    }
35
36
37
38
    /**
39
     * @psalm-suppress PossiblyNullReference
40
     * @param Ruleable&Element $element
41
     * @return bool
42
     * @throws ExceptionRule
43
     */
44
    public function validate(Ruleable $element): bool
45
    {
46
        $method = $this->getRequest()->getMethod();
47
48
        /** @var array $requestData */
49
        $requestData = match (strtolower($method)) {
50
            'get' => $this->getRequest()->getQueryParams(),
51
            'post' => $this->getRequest()->getParsedBody(),
52
            default => []
53
        };
54
55
        /** @var string|int|array|false $value */
56
        $value = \getValueByIndexPath($element->getName(), $requestData);
0 ignored issues
show
Bug introduced by
The method getName() does not exist on Enjoys\Forms\Interfaces\Ruleable. Since it exists in all sub-types, consider adding an abstract or default implementation to Enjoys\Forms\Interfaces\Ruleable. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
        $value = \getValueByIndexPath($element->/** @scrutinizer ignore-call */ getName(), $requestData);
Loading history...
57
58
        if (!$this->check($value)) {
59
            $element->setRuleError($this->message);
60
            return false;
61
        }
62
63
        return true;
64
    }
65
66
67
    /**
68
     * @param string|int|array|false $value
69
     * @return bool
70
     * @throws ExceptionRule
71
     */
72
    private function check(string|int|array|false $value): bool
73
    {
74
        if (is_array($value) || $value === false) {
0 ignored issues
show
introduced by
The condition is_array($value) is always true.
Loading history...
75
            return true;
76
        }
77
78
        $length = \mb_strlen(\trim((string)$value), 'UTF-8');
79
        if (empty($value)) {
80
            return true;
81
        }
82
83
        /** @var string $operator */
84
        /** @var int|string $threshold */
85
        foreach ($this->params as $operator => $threshold) {
86
            $method = $this->operatorToMethodTranslation[$operator] ?? 'unknown';
87
88
            if (!method_exists(Length::class, $method)) {
89
                throw new ExceptionRule('Unknown Compare Operator.');
90
            }
91
92
            if (!$this->$method($length, $threshold)) {
93
                return false;
94
            }
95
        }
96
97
        return true;
98
    }
99
100
    /**
101
     *
102
     * @param int $value
103
     * @param int|string $threshold
104
     * @return bool
105
     */
106
    private function equal(int $value, int|string $threshold): bool
107
    {
108
        return $value == $threshold;
109
    }
110
111
    /**
112
     *
113
     * @param int $value
114
     * @param int|string $threshold
115
     * @return bool
116
     */
117
    private function notEqual(int $value, int|string $threshold): bool
118
    {
119
        return $value != $threshold;
120
    }
121
122
    /**
123
     *
124
     * @param int $value
125
     * @param int|string $threshold
126
     * @return bool
127
     */
128
    private function greaterThan(int $value, int|string $threshold): bool
129
    {
130
        return $value > $threshold;
131
    }
132
133
    /**
134
     *
135
     * @param int $value
136
     * @param int|string $threshold
137
     * @return bool
138
     */
139
    private function lessThan(int $value, int|string $threshold): bool
140
    {
141
        return $value < $threshold;
142
    }
143
144
    /**
145
     *
146
     * @param int $value
147
     * @param int|string $threshold
148
     * @return bool
149
     */
150
    private function greaterThanOrEqual(int $value, int|string $threshold): bool
151
    {
152
        return $value >= $threshold;
153
    }
154
155
    /**
156
     *
157
     * @param int $value
158
     * @param int|string $threshold
159
     * @return bool
160
     */
161
    private function lessThanOrEqual(int $value, int|string $threshold): bool
162
    {
163
        return $value <= $threshold;
164
    }
165
}
166