Issues (35)

src/Traits/Rules.php (2 issues)

1
<?php
2
3
namespace Enjoys\Forms\Traits;
4
5
use Enjoys\Forms\Exception\ExceptionRule;
6
use Enjoys\Forms\Rule\RuleInterface;
7
8
trait Rules
9
{
10
    /**
11
     * Когда $rule_error === true выводится это сообщение
12
     */
13
    private ?string $rule_error_message = null;
14
15
    /**
16
     * Если true - проверка validate не пройдена
17
     */
18
    private bool $rule_error = false;
19
20
21
    /**
22
     * Список правил для валидации
23
     * @var RuleInterface[]
24
     */
25
    protected array $rules = [];
26
27
    /**
28
     * Проверяет обязателен ли элемент для заполнения/выбора или нет
29
     * true - обязателен
30
     */
31 14
    public function isRequired(): bool
32
    {
33 14
        return $this->required;
34
    }
35
36
37
    /**
38
     * @throws ExceptionRule
39
     */
40 158
    public function addRule(string $ruleClass, mixed ...$params): static
41
    {
42
43 158
        if (!class_exists($ruleClass)) {
44 1
            throw new ExceptionRule(
45 1
                sprintf('Rule [%s] not found', $ruleClass)
46 1
            );
47
        }
48
        /** @var class-string<RuleInterface> $ruleClass */
49 157
        $rule = new $ruleClass(...$params);
50 153
        $rule->setRequest($this->getRequest());
0 ignored issues
show
It seems like getRequest() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

50
        $rule->setRequest($this->/** @scrutinizer ignore-call */ getRequest());
Loading history...
51
52
        //установка обязательности элемента
53 153
        if ($ruleClass === \Enjoys\Forms\Rules::REQUIRED) {
54 13
            $this->required = true;
0 ignored issues
show
Bug Best Practice introduced by
The property required does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
55
56
            /**
57
             * @todo Если обязателен элемент добавить required аттрибут для проверки на стороне браузера
58
             * Пока отключено
59
             */
60
            //$this->setAttribute('required');
61
        }
62
63 153
        $this->rules[] = $rule;
64 153
        return $this;
65
    }
66
67
    /**
68
     * Если валидация не пройдена, вызывается этот метод, устанавливает сообщение
69
     * ошибки и устанавливает флаг того что проверка не пройдена
70
     */
71 36
    public function setRuleError(?string $message): void
72
    {
73 36
        $this->rule_error = true;
74 36
        $this->rule_error_message = $message;
75
    }
76
77 26
    public function getRuleErrorMessage(): ?string
78
    {
79 26
        return $this->rule_error_message;
80
    }
81
82
    /**
83
     * Проверка валидации элемента, если true валидация не пройдена
84
     */
85 15
    public function isRuleError(): bool
86
    {
87 15
        return $this->rule_error;
88
    }
89
90
91
    /**
92
     * Возвращает список всех правил валидации, установленных для элемента
93
     * @return RuleInterface[]
94
     */
95 50
    public function getRules(): array
96
    {
97 50
        return $this->rules;
98
    }
99
100 1
    public function disableRules(): static
101
    {
102 1
        $this->rules = [];
103 1
        return $this;
104
    }
105
}
106