Passed
Push — master ( 94344c...86ce1f )
by Alexander
07:20 queued 04:31
created

ValidationClassTrait::inputValidClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Field\Base\ValidationClass;
6
7
use Yiisoft\Form\FormModelInterface;
8
use Yiisoft\Html\Html;
9
10
/**
11
 * @psalm-require-extends \Yiisoft\Form\Field\Base\PartsField
12
 */
13
trait ValidationClassTrait
14
{
15
    private ?string $invalidClass = null;
16
    private ?string $validClass = null;
17
    private ?string $inputInvalidClass = null;
18
    private ?string $inputValidClass = null;
19
20
    /**
21
     * Set invalid CSS class.
22
     */
23 4
    public function invalidClass(?string $class): self
24
    {
25 4
        $new = clone $this;
26 4
        $new->invalidClass = $class;
27 4
        return $new;
28
    }
29
30
    /**
31
     * Set valid CSS class.
32
     */
33 4
    public function validClass(?string $class): self
34
    {
35 4
        $new = clone $this;
36 4
        $new->validClass = $class;
37 4
        return $new;
38
    }
39
40
    /**
41
     * Set invalid CSS class for input tag.
42
     */
43 3
    public function inputInvalidClass(?string $class): self
44
    {
45 3
        $new = clone $this;
46 3
        $new->inputInvalidClass = $class;
47 3
        return $new;
48
    }
49
50
    /**
51
     * Set valid CSS class for input tag.
52
     */
53 3
    public function inputValidClass(?string $class): self
54
    {
55 3
        $new = clone $this;
56 3
        $new->inputValidClass = $class;
57 3
        return $new;
58
    }
59
60 178
    protected function addValidationClassToAttributes(
61
        array &$attributes,
62
        FormModelInterface $formModel,
63
        string $attributeName,
64
    ): void {
65 178
        $this->addClassesToAttributes(
66
            $attributes,
67
            $formModel,
68
            $attributeName,
69 178
            $this->invalidClass,
70 178
            $this->validClass,
71
        );
72
    }
73
74 304
    protected function addInputValidationClassToAttributes(
75
        array &$attributes,
76
        FormModelInterface $formModel,
77
        string $attributeName,
78
    ): void {
79 304
        $this->addClassesToAttributes(
80
            $attributes,
81
            $formModel,
82
            $attributeName,
83 304
            $this->inputInvalidClass,
84 304
            $this->inputValidClass,
85
        );
86
    }
87
88 319
    private function addClassesToAttributes(
89
        array &$attributes,
90
        FormModelInterface $formModel,
91
        string $attributeName,
92
        ?string $invalidClass,
93
        ?string $validClass,
94
    ): void {
95 319
        if (!$formModel->isValidated()) {
96 280
            return;
97
        }
98
99 39
        $hasErrors = $formModel->getFormErrors()->hasErrors($attributeName);
100
101 39
        if ($hasErrors && $invalidClass !== null) {
102 4
            Html::addCssClass($attributes, $invalidClass);
103
        }
104
105 39
        if (!$hasErrors && $validClass !== null) {
106 4
            Html::addCssClass($attributes, $validClass);
107
        }
108
    }
109
}
110