Passed
Push — 5.x ( 65a76f...3a1e20 )
by Enjoys
59s queued 13s
created

Length::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.024

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 10
c 1
b 0
f 1
dl 0
loc 18
ccs 9
cts 11
cp 0.8182
rs 9.9332
cc 2
nc 2
nop 1
crap 2.024
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\Rules;
11
12
class Length extends Rules implements RuleInterface
13
{
14
    private array $operatorToMethodTranslation = [
15
        '==' => 'equal',
16
        '!=' => 'notEqual',
17
        '>' => 'greaterThan',
18
        '<' => 'lessThan',
19
        '>=' => 'greaterThanOrEqual',
20
        '<=' => 'lessThanOrEqual',
21
    ];
22
23 57
    public function setMessage(?string $message = null): ?string
24
    {
25 57
        if (is_null($message)) {
26 57
            $message = 'Ошибка ввода';
27
        }
28 57
        return parent::setMessage($message);
29
    }
30
31
    /**
32
     * @psalm-suppress PossiblyNullReference
33
     * @param Ruleable&Element $element
34
     * @return bool
35
     * @throws ExceptionRule
36
     */
37 8
    public function validate(Ruleable $element): bool
38
    {
39
40 8
        $method = $this->getRequest()->getRequest()->getMethod();
41 8
        $requestData = match (strtolower($method)) {
42 8
            'get' => $this->getRequest()->getQueryData()->toArray(),
43
            'post' => $this->getRequest()->getPostData()->toArray(),
44
            default => []
45
        };
46 8
        $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

46
        $value = \getValueByIndexPath($element->/** @scrutinizer ignore-call */ getName(), $requestData);
Loading history...
47
48
        // $input_value = $request->post($element->getName(), $request->get($element->getName(), ''));
49 8
        if (!$this->check($value)) {
50 2
            $element->setRuleError($this->getMessage());
51 2
            return false;
52
        }
53
54 6
        return true;
55
    }
56
57
    /**
58
     *
59
     * @param mixed $value
60
     * @return bool
61
     * @throws ExceptionRule
62
     */
63 57
    private function check($value): bool
64
    {
65 57
        if (is_array($value)) {
66 7
            return true;
67
        }
68
69 50
        $length = \mb_strlen(\trim((string) $value), 'UTF-8');
70 50
        if (empty($value)) {
71 7
            return true;
72
        }
73
74 43
        foreach ($this->getParams() as $operator => $threshold) {
75 43
            $method = 'unknown';
76
77 43
            if (isset($this->operatorToMethodTranslation[$operator])) {
78 42
                $method = $this->operatorToMethodTranslation[$operator];
79
            }
80
81 43
            if (!method_exists(Length::class, $method)) {
82 1
                throw new ExceptionRule('Unknown Compare Operator.');
83
            }
84
85 42
            if (!$this->$method($length, $threshold)) {
86 18
                return false;
87
            }
88
        }
89
90 24
        return true;
91
    }
92
93
    /**
94
     *
95
     * @param mixed $value
96
     * @param mixed $threshold
97
     * @return bool
98
     */
99 6
    private function equal($value, $threshold): bool
100
    {
101 6
        return $value == $threshold;
102
    }
103
104
    /**
105
     *
106
     * @param mixed $value
107
     * @param mixed $threshold
108
     * @return bool
109
     */
110 12
    private function notEqual($value, $threshold): bool
111
    {
112 12
        return $value != $threshold;
113
    }
114
115
    /**
116
     *
117
     * @param mixed $value
118
     * @param mixed $threshold
119
     * @return bool
120
     */
121 6
    private function greaterThan($value, $threshold): bool
122
    {
123 6
        return $value > $threshold;
124
    }
125
126
    /**
127
     *
128
     * @param mixed $value
129
     * @param mixed $threshold
130
     * @return bool
131
     */
132 6
    private function lessThan($value, $threshold): bool
133
    {
134 6
        return $value < $threshold;
135
    }
136
137
    /**
138
     *
139
     * @param mixed $value
140
     * @param mixed $threshold
141
     * @return bool
142
     */
143 6
    private function greaterThanOrEqual($value, $threshold): bool
144
    {
145 6
        return $value >= $threshold;
146
    }
147
148
    /**
149
     *
150
     * @param mixed $value
151
     * @param mixed $threshold
152
     * @return bool
153
     */
154 6
    private function lessThanOrEqual($value, $threshold): bool
155
    {
156 6
        return $value <= $threshold;
157
    }
158
}
159