-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
BasicWidget.php
209 lines (187 loc) · 6.24 KB
/
BasicWidget.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);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\View\Widget;
use Cake\View\Form\ContextInterface;
use Cake\View\StringTemplate;
/**
* Basic input class.
*
* This input class can be used to render basic simple
* input elements like hidden, text, email, tel and other
* types.
*/
class BasicWidget implements WidgetInterface
{
/**
* StringTemplate instance.
*
* @var \Cake\View\StringTemplate
*/
protected StringTemplate $_templates;
/**
* Data defaults.
*
* @var array<string, mixed>
*/
protected array $defaults = [
'name' => '',
'val' => null,
'type' => 'text',
'escape' => true,
'templateVars' => [],
];
/**
* Constructor.
*
* @param \Cake\View\StringTemplate $templates Templates list.
*/
public function __construct(StringTemplate $templates)
{
$this->_templates = $templates;
}
/**
* Render a text widget or other simple widget like email/tel/number.
*
* This method accepts a number of keys:
*
* - `name` The name attribute.
* - `val` The value attribute.
* - `escape` Set to false to disable escaping on all attributes.
*
* Any other keys provided in $data will be converted into HTML attributes.
*
* @param array<string, mixed> $data The data to build an input with.
* @param \Cake\View\Form\ContextInterface $context The current form context.
* @return string
*/
public function render(array $data, ContextInterface $context): string
{
$data = $this->mergeDefaults($data, $context);
$data['value'] = $data['val'];
unset($data['val']);
if ($data['value'] === false) {
// explicitly convert to 0 to avoid empty string which is marshaled as null
$data['value'] = '0';
}
$fieldName = $data['fieldName'] ?? null;
if ($fieldName) {
if ($data['type'] === 'number' && !isset($data['step'])) {
$data = $this->setStep($data, $context, $fieldName);
}
$typesWithMaxLength = ['text', 'email', 'tel', 'url', 'search'];
if (
!array_key_exists('maxlength', $data)
&& in_array($data['type'], $typesWithMaxLength, true)
) {
$data = $this->setMaxLength($data, $context, $fieldName);
}
}
return $this->_templates->format('input', [
'name' => $data['name'],
'type' => $data['type'],
'templateVars' => $data['templateVars'],
'attrs' => $this->_templates->formatAttributes(
$data,
['name', 'type']
),
]);
}
/**
* Merge default values with supplied data.
*
* @param array<string, mixed> $data Data array
* @param \Cake\View\Form\ContextInterface $context Context instance.
* @return array<string, mixed> Updated data array.
*/
protected function mergeDefaults(array $data, ContextInterface $context): array
{
$data += $this->defaults;
if (isset($data['fieldName']) && !array_key_exists('required', $data)) {
$data = $this->setRequired($data, $context, $data['fieldName']);
}
return $data;
}
/**
* Set value for "required" attribute if applicable.
*
* @param array<string, mixed> $data Data array
* @param \Cake\View\Form\ContextInterface $context Context instance.
* @param string $fieldName Field name.
* @return array<string, mixed> Updated data array.
*/
protected function setRequired(array $data, ContextInterface $context, string $fieldName): array
{
if (
empty($data['disabled'])
&& (
(isset($data['type'])
&& $data['type'] !== 'hidden'
)
|| !isset($data['type'])
)
&& $context->isRequired($fieldName)
) {
$data['required'] = true;
}
return $data;
}
/**
* Set value for "maxlength" attribute if applicable.
*
* @param array<string, mixed> $data Data array
* @param \Cake\View\Form\ContextInterface $context Context instance.
* @param string $fieldName Field name.
* @return array<string, mixed> Updated data array.
*/
protected function setMaxLength(array $data, ContextInterface $context, string $fieldName): array
{
$maxLength = $context->getMaxLength($fieldName);
if ($maxLength !== null) {
$data['maxlength'] = min($maxLength, 100000);
}
return $data;
}
/**
* Set value for "step" attribute if applicable.
*
* @param array<string, mixed> $data Data array
* @param \Cake\View\Form\ContextInterface $context Context instance.
* @param string $fieldName Field name.
* @return array<string, mixed> Updated data array.
*/
protected function setStep(array $data, ContextInterface $context, string $fieldName): array
{
$dbType = $context->type($fieldName);
$fieldDef = $context->attributes($fieldName);
if ($dbType === 'decimal' && isset($fieldDef['precision'])) {
$decimalPlaces = $fieldDef['precision'];
$data['step'] = sprintf('%.' . $decimalPlaces . 'F', pow(10, -1 * $decimalPlaces));
} elseif ($dbType === 'float') {
$data['step'] = 'any';
}
return $data;
}
/**
* @inheritDoc
*/
public function secureFields(array $data): array
{
if (!isset($data['name']) || $data['name'] === '') {
return [];
}
return [$data['name']];
}
}