Passed
Push — master ( 34f20f...a12a6d )
by Enjoys
57s queued 12s
created

Equal   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 91.3%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 24
c 1
b 0
f 0
dl 0
loc 67
ccs 21
cts 23
cp 0.913
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 19 2
A __construct() 0 5 1
A check() 0 16 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\Forms\Rule;
6
7
use Enjoys\Forms\Element;
8
use Enjoys\Forms\Interfaces\Ruleable;
9
use Enjoys\Forms\Traits\Request;
10
11
/**
12
 * @example
13
 * new Equal($message, ['expect']); or
14
 * new Equal($message, (array) 'expect'); or
15
 * new Equal($message, ['expect', 1, '255']);
16
 */
17
class Equal implements RuleInterface
18
{
19
    use Request;
20
21
    private string $message;
22
    private array $params;
23
24
25
    /**
26
     * @param array<array-key, scalar>|scalar $params
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, scalar>|scalar at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, scalar>|scalar.
Loading history...
27
     * @param string|null $message
28
     */
29 13
    public function __construct(mixed $params, string $message = null)
30
    {
31
32 13
        $this->params = (array) $params;
33 13
        $this->message = $message ?? 'Допустимые значения (указаны через запятую): ' . \implode(', ', $this->params);
34
    }
35
36
37
    /**
38
     * @psalm-suppress PossiblyNullReference
39
     * @param Ruleable&Element $element
40
     * @return bool
41
     */
42 13
    public function validate(Ruleable $element): bool
43
    {
44
45 13
        $method = $this->getRequest()->getRequest()->getMethod();
46 13
        $requestData = match (strtolower($method)) {
47 13
            'get' => $this->getRequest()->getQueryData()->toArray(),
48
            'post' => $this->getRequest()->getPostData()->toArray(),
49
            default => []
50
        };
51
52
        /** @var mixed $value */
53 13
        $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

53
        $value = \getValueByIndexPath($element->/** @scrutinizer ignore-call */ getName(), $requestData);
Loading history...
54
55 13
        if (false === $this->check($value)) {
56 5
            $element->setRuleError($this->message);
57 5
            return false;
58
        }
59
60 8
        return true;
61
    }
62
63
    /**
64
     * @param mixed $value
65
     * @return array-key|bool
0 ignored issues
show
Documentation Bug introduced by
The doc comment array-key|bool at position 0 could not be parsed: Unknown type name 'array-key' at position 0 in array-key|bool.
Loading history...
66
     * @noinspection PhpMissingReturnTypeInspection
67
     */
68 13
    private function check(mixed $value)
69
    {
70
71 13
        if ($value === false) {
72 1
            return true;
73
        }
74 12
        if (is_array($value)) {
75 4
            foreach ($value as $_val) {
76 4
                if (false === $this->check($_val)) {
77 1
                    return false;
78
                }
79
            }
80 3
            return true;
81
        }
82
83 12
        return array_search($value, $this->params);
84
    }
85
}
86