/
Widget.php
209 lines (187 loc) · 6.92 KB
/
Widget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
declare(strict_types=1);
namespace Yii\Extension\Simple\Forms;
use InvalidArgumentException;
use UnexpectedValueException;
use Yii\Extension\Simple\Model\ModelInterface;
use Yii\Extension\Simple\Widget\AbstractWidget;
use Yiisoft\Html\NoEncodeStringableInterface;
abstract class Widget extends AbstractWidget implements NoEncodeStringableInterface
{
protected string $attribute = '';
protected array $attributes = [];
protected ModelInterface $modelInterface;
private bool $autoGenerate = true;
private string $charset = 'UTF-8';
private string $id = '';
/**
* The HTML attributes for the navbar. The following special options are recognized.
*
* @param array $value
*
* @return static
*/
public function attributes(array $value): self
{
$new = clone $this;
$new->attributes = $value;
return $new;
}
/**
* Set the character set used to generate the widget id. See {@see HtmlForm::getInputId()}.
*
* @param string $value
*
* @return static
*/
public function charset(string $value): self
{
$new = clone $this;
$new->charset = $value;
return $new;
}
/**
* Set form interface, attribute name and attributes for the widget.
*
* @param ModelInterface $modelInterface Form.
* @param string $attribute Form model property this widget is rendered for.
* @param array $attributes The HTML attributes for the widget container tag.
*
* @return static
*
* See {@see Html::renderTagAttributes()} for details on how attributes are being rendered.
*/
public function config(ModelInterface $modelInterface, string $attribute, array $attributes = []): self
{
$new = clone $this;
$new->modelInterface = $modelInterface;
$new->attribute = $attribute;
$new->attributes = $attributes;
return $new;
}
/**
* Set the Id of the widget.
*
* @param string $value
*
* @return static
*/
public function id(string $value): self
{
$new = clone $this;
$new->id = $value;
return $new;
}
/**
* Returns the real attribute name from the given attribute expression.
* If `$attribute` has neither prefix nor suffix, it will be returned back without change.
*
* @param string $attribute the attribute name or expression
*
* @throws InvalidArgumentException if the attribute name contains non-word characters.
*
* @return string the attribute name without prefix and suffix.
*
* {@see parseAttribute()}
*/
protected function getAttributeName(string $attribute): string
{
return (string) $this->parseAttribute($attribute)['name'];
}
protected function getCharset(): string
{
return $this->charset;
}
protected function getId(string $formName, string $attribute): string
{
$new = clone $this;
/** @var string */
$id = $new->attributes['id'] ?? $new->id;
if ($id === '') {
$id = $new->getInputId($formName, $attribute, $new->charset);
}
return $id;
}
/**
* Generates an appropriate input name for the specified attribute name or expression.
*
* This method generates a name that can be used as the input name to collect user input for the specified
* attribute. The name is generated according to the of the form and the given attribute name. For example, if the
* form name of the `Post` form is `Post`, then the input name generated for the `content` attribute would be
* `Post[content]`.
*
* See {@see getAttributeName()} for explanation of attribute expression.
*
* @param string $formName the formname.
* @param string $attribute the attribute name or expression.
*
* @throws InvalidArgumentException if the attribute name contains non-word characters or empty form name for
* tabular inputs.
*
* @return string the generated input name.
*/
protected function getInputName(string $formName, string $attribute): string
{
$data = $this->parseAttribute($attribute);
if ($formName === '' && $data['prefix'] === '') {
return $attribute;
}
if ($formName !== '') {
return $formName . (string) $data['prefix'] . '[' . (string) $data['name'] . ']' . (string) $data['suffix'];
}
throw new InvalidArgumentException('The formName cannot be empty.');
}
/**
* Generates an appropriate input ID for the specified attribute name or expression.
*
* This method converts the result {@see getInputName()} into a valid input ID.
*
* For example, if {@see getInputName()} returns `Post[content]`, this method will return `post-content`.
*
* @param string $form the formname
* @param string $attribute the attribute name or expression. See {@see getAttributeName()} for explanation of
* attribute expression.
* @param string $charset default `UTF-8`.
*
* @throws InvalidArgumentException if the attribute name contains non-word characters.
* @throws UnexpectedValueException if charset is unknown
*
* @return string the generated input ID.
*/
private function getInputId(string $formName, string $attribute, string $charset = 'UTF-8'): string
{
$name = mb_strtolower($this->getInputName($formName, $attribute), $charset);
return str_replace(['[]', '][', '[', ']', ' ', '.'], ['', '-', '-', '', '-', '-'], $name);
}
/**
* This method parses an attribute expression and returns an associative array containing
* real attribute name, prefix and suffix.
* For example: `['name' => 'content', 'prefix' => '', 'suffix' => '[0]']`
*
* An attribute expression is an attribute name prefixed and/or suffixed with array indexes. It is mainly used in
* tabular data input and/or input of array type. Below are some examples:
*
* - `[0]content` is used in tabular data input to represent the "content" attribute for the first model in tabular
* input;
* - `dates[0]` represents the first array element of the "dates" attribute;
* - `[0]dates[0]` represents the first array element of the "dates" attribute for the first model in tabular
* input.
*
* @param string $attribute the attribute name or expression.
*
* @return array
*
* @throws InvalidArgumentException if the attribute name contains non-word characters.
*/
private function parseAttribute(string $attribute): array
{
if (!preg_match('/(^|.*\])([\w\.\+]+)(\[.*|$)/u', $attribute, $matches)) {
throw new InvalidArgumentException('Attribute name must contain word characters only.');
}
return [
'name' => $matches[2],
'prefix' => $matches[1],
'suffix' => $matches[3],
];
}
}