Passed
Pull Request — master (#192)
by Sergei
03:23
created

Error::run()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 17
nc 6
nop 0
dl 0
loc 27
ccs 17
cts 17
cp 1
crap 6
rs 9.0777
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Field\Part;
6
7
use InvalidArgumentException;
8
use Yiisoft\Form\Field\Base\FormAttributeTrait;
9
use Yiisoft\Html\Tag\CustomTag;
10
use Yiisoft\Widget\Widget;
11
12
use function call_user_func;
13
14
/**
15
 * Represent a field validation error (if there are several errors, the first one is used). If field is no validation
16
 * error, field part will be hidden.
17
 */
18
final class Error extends Widget
19
{
20
    use FormAttributeTrait;
21
22
    /**
23
     * @psalm-var non-empty-string
24
     */
25
    private string $tag = 'div';
26
    private array $tagAttributes = [];
27
28
    private bool $encode = true;
29
30
    private ?string $message = null;
31
32
    /**
33
     * @var callable|null
34
     */
35
    private $messageCallback = null;
36
37
    /**
38
     * Set the container tag name for the error.
39
     *
40
     * @param string $tag Container tag name.
41
     *
42
     * @return static
43
     */
44 4
    public function tag(string $tag): self
45
    {
46 4
        if ($tag === '') {
47 1
            throw new InvalidArgumentException('Tag name cannot be empty.');
48
        }
49
50 3
        $new = clone $this;
51 3
        $new->tag = $tag;
52 3
        return $new;
53
    }
54
55 3
    public function tagAttributes(array $attributes): self
56
    {
57 3
        $new = clone $this;
58 3
        $new->tagAttributes = $attributes;
59 3
        return $new;
60
    }
61
62
    /**
63
     * Whether content should be HTML-encoded.
64
     *
65
     * @param bool $value
66
     *
67
     * @return static
68
     */
69 1
    public function encode(bool $value): self
70
    {
71 1
        $new = clone $this;
72 1
        $new->encode = $value;
73 1
        return $new;
74
    }
75
76
    /**
77
     * Error message to display.
78
     *
79
     * @return static
80
     */
81 7
    public function message(?string $value): self
82
    {
83 7
        $new = clone $this;
84 7
        $new->message = $value;
85 7
        return $new;
86
    }
87
88
    /**
89
     * Callback that will be called to obtain an error message.
90
     *
91
     * @param callable|null $value
92
     *
93
     * @return static
94
     */
95 3
    public function messageCallback(?callable $value): self
96
    {
97 3
        $new = clone $this;
98 3
        $new->messageCallback = $value;
99 3
        return $new;
100
    }
101
102
    /**
103
     * Generates a tag that contains the first validation error of the specified form attribute.
104
     *
105
     * @return string The generated error tag.
106
     */
107 392
    protected function run(): string
108
    {
109 392
        $useModel = $this->hasFormModelAndAttribute();
110
111 392
        $message = $useModel
112 331
            ? $this->message ?? $this->getFirstError()
113 63
            : $this->message;
114
115 392
        if ($message === null) {
116 362
            return '';
117
        }
118
119 30
        if ($this->messageCallback !== null) {
120
            /** @var string $message */
121 3
            $message = call_user_func(
122 3
                $this->messageCallback,
123
                $message,
124 3
                $useModel ? $this->getFormModel() : null,
125 3
                $useModel ? $this->attribute : null
126
            );
127
        }
128
129 30
        return CustomTag::name($this->tag)
130 30
            ->attributes($this->tagAttributes)
131 30
            ->content($message)
132 30
            ->encode($this->encode)
133 30
            ->render();
134
    }
135
}
136