Passed
Push — adopt-factory ( 9be618 )
by Alexander
07:14
created

Error::run()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 13
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 22
ccs 13
cts 13
cp 1
crap 3
rs 9.8333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Widget;
6
7
use Yiisoft\Arrays\ArrayHelper;
8
use Yiisoft\Form\FormModelInterface;
9
use Yiisoft\Form\Helper\HtmlForm;
10
use Yiisoft\Html\Html;
11
use Yiisoft\Widget\Widget;
12
13
final class Error extends Widget
14
{
15
    private FormModelInterface $data;
16
    private string $attribute;
17
    private array $options = [];
18
19
    /**
20
     * Generates a tag that contains the first validation error of the specified form attribute.
21
     *
22
     * @return string the generated label tag
23
     */
24 69
    public function run(): string
25
    {
26 69
        $new = clone $this;
27
28 69
        $errorSource = ArrayHelper::remove($new->options, 'errorSource');
29
30 69
        if ($errorSource !== null) {
31 2
            $error = $errorSource($new->data, $new->attribute);
32
        } else {
33 67
            $error = $new->data->getFirstError(HtmlForm::getAttributeName($new->attribute));
34
        }
35
36 69
        $tag = ArrayHelper::remove($new->options, 'tag', 'div');
37 69
        if (empty($tag)) {
38 1
            return $error;
39
        }
40
41 69
        $encode = ArrayHelper::remove($new->options, 'encode', true);
42
43 69
        return Html::tag($tag, $error, $new->options)
44 69
            ->encode($encode)
45 69
            ->render();
46
    }
47
48
    /**
49
     * Set form model, name and options for the widget.
50
     *
51
     * @param FormModelInterface $data Form model.
52
     * @param string $attribute Form model property this widget is rendered for.
53
     * @param array $options The HTML attributes for the widget container tag.
54
     * See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
55
     *
56
     * @return self
57
     */
58 69
    public function config(FormModelInterface $data, string $attribute, array $options = []): self
59
    {
60 69
        $new = clone $this;
61 69
        $new->data = $data;
62 69
        $new->attribute = $attribute;
63 69
        $new->options = $options;
64 69
        return $new;
65
    }
66
67
    /**
68
     * Callback that will be called to obtain an error message.
69
     *
70
     * The signature of the callback must be:
71
     *
72
     * ```php
73
     * [$FormModel, function()]
74
     * ```
75
     *
76
     * @param array $value
77
     *
78
     * @return self
79
     */
80 2
    public function errorSource(array $value = []): self
81
    {
82 2
        $new = clone $this;
83 2
        $new->options['errorSource'] = $value;
84 2
        return $new;
85
    }
86
87
    /**
88
     * Whether to HTML-encode the error messages.
89
     *
90
     * Defaults to true. This option is ignored if item option is set.
91
     *
92
     * @param bool $value
93
     *
94
     * @return self
95
     */
96 1
    public function noEncode(bool $value = false): self
97
    {
98 1
        $new = clone $this;
99 1
        $new->options['encode'] = $value;
100 1
        return $new;
101
    }
102
103
    /**
104
     * The tag name of the container element.
105
     *
106
     * Null to render error messages without container {@see Html::tag()}.
107
     *
108
     * @param string|null $value
109
     *
110
     * @return self
111
     */
112 1
    public function tag(?string $value = null): self
113
    {
114 1
        $new = clone $this;
115 1
        $new->options['tag'] = $value;
116 1
        return $new;
117
    }
118
}
119