1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Widget; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
9
|
|
|
use Yiisoft\Form\FormModelInterface; |
10
|
|
|
use Yiisoft\Form\Helper\HtmlForm; |
11
|
|
|
use Yiisoft\Widget\Widget; |
12
|
|
|
|
13
|
|
|
abstract class AbstractWidget extends Widget |
14
|
|
|
{ |
15
|
|
|
protected array $attributes = []; |
16
|
|
|
private string $attribute = ''; |
17
|
|
|
private bool $encode = true; |
18
|
|
|
private ?FormModelInterface $formModel = null; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Add Html attribute. |
22
|
|
|
* |
23
|
|
|
* @param string $key attribute name. |
24
|
|
|
* @param mixed $value attribute value. |
25
|
|
|
* |
26
|
|
|
* @return static |
27
|
|
|
*/ |
28
|
1 |
|
public function addAttribute(string $key, $value): self |
29
|
|
|
{ |
30
|
1 |
|
$new = clone $this; |
31
|
1 |
|
$new->attributes[$key] = $value; |
32
|
1 |
|
return $new; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The HTML attributes. The following special options are recognized. |
37
|
|
|
* |
38
|
|
|
* @param array $value |
39
|
|
|
* |
40
|
|
|
* @return static |
41
|
|
|
* |
42
|
|
|
* See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
43
|
|
|
*/ |
44
|
192 |
|
public function attributes(array $value): self |
45
|
|
|
{ |
46
|
192 |
|
$new = clone $this; |
47
|
|
|
|
48
|
192 |
|
if ($value !== []) { |
49
|
129 |
|
$new->attributes = $value; |
50
|
|
|
} |
51
|
|
|
|
52
|
192 |
|
return $new; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Whether content should be HTML-encoded. |
57
|
|
|
* |
58
|
|
|
* @param bool $value |
59
|
|
|
* |
60
|
|
|
* @return static |
61
|
|
|
*/ |
62
|
14 |
|
public function encode(bool $value): self |
63
|
|
|
{ |
64
|
14 |
|
$new = clone $this; |
65
|
14 |
|
$new->encode = $value; |
66
|
14 |
|
return $new; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return static |
71
|
|
|
*/ |
72
|
511 |
|
public function for(FormModelInterface $formModel, string $attribute): self |
73
|
|
|
{ |
74
|
511 |
|
$new = clone $this; |
75
|
511 |
|
$new->formModel = $formModel; |
76
|
511 |
|
$new->attribute = $attribute; |
77
|
511 |
|
return $new; |
78
|
|
|
} |
79
|
|
|
|
80
|
486 |
|
protected function getAttribute(): string |
81
|
|
|
{ |
82
|
486 |
|
if ($this->attribute === '') { |
83
|
1 |
|
throw new InvalidArgumentException('Attribute is not set.'); |
84
|
|
|
} |
85
|
|
|
|
86
|
485 |
|
return $this->attribute; |
87
|
|
|
} |
88
|
|
|
|
89
|
198 |
|
protected function getEncode(): bool |
90
|
|
|
{ |
91
|
198 |
|
return $this->encode; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Return FormModelInterface object. |
96
|
|
|
* |
97
|
|
|
* @return FormModelInterface |
98
|
|
|
*/ |
99
|
498 |
|
protected function getFormModel(): FormModelInterface |
100
|
|
|
{ |
101
|
498 |
|
if ($this->formModel === null) { |
102
|
1 |
|
throw new InvalidArgumentException('Form model is not set.'); |
103
|
|
|
} |
104
|
|
|
|
105
|
497 |
|
return $this->formModel; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Generates a unique ID for the attribute, if it does not have one yet. |
110
|
|
|
* |
111
|
|
|
* @return string|null |
112
|
|
|
*/ |
113
|
422 |
|
protected function getId(): ?string |
114
|
|
|
{ |
115
|
422 |
|
$id = ArrayHelper::remove($this->attributes, 'id', ''); |
116
|
|
|
|
117
|
422 |
|
if (!is_string($id) && null !== $id) { |
118
|
1 |
|
throw new InvalidArgumentException('Attribute "id" must be a string or null.'); |
119
|
|
|
} |
120
|
|
|
|
121
|
421 |
|
if ($id === '') { |
122
|
421 |
|
$id = HtmlForm::getInputId($this->getFormModel(), $this->attribute); |
123
|
|
|
} |
124
|
|
|
|
125
|
421 |
|
return $id ?? null; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Generate name for the widget. |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
424 |
|
protected function getName(): string |
134
|
|
|
{ |
135
|
424 |
|
$name = $this->attributes['name'] ?? HtmlForm::getInputName($this->getFormModel(), $this->attribute); |
136
|
|
|
|
137
|
424 |
|
if (!is_string($name)) { |
138
|
1 |
|
throw new InvalidArgumentException('Attribute "name" must be a string.'); |
139
|
|
|
} |
140
|
|
|
|
141
|
423 |
|
return $name; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|