Passed
Pull Request — master (#192)
by Alexander
06:02 queued 02:54
created

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