Passed
Push — 5.x ( 2c5c24...6dc93b )
by Enjoys
11:29
created

Length::greaterThan()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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

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