|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace hemio\form\Abstract_; |
|
4
|
|
|
|
|
5
|
|
|
use hemio\form as form_; |
|
6
|
|
|
use hemio\form\exception; |
|
7
|
|
|
use hemio\html; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* |
|
11
|
|
|
* |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class FormFieldDefault extends FormField implements form_\Focusable { |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
public $title = ''; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* |
|
24
|
|
|
* @var boolean |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $filled = false; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* |
|
30
|
|
|
* @var html\Interface_\Submittable |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $control; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $defaultValue = ''; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* |
|
42
|
|
|
* @param mixed $value |
|
43
|
|
|
*/ |
|
44
|
|
|
public function setDefaultValue($value) { |
|
45
|
|
|
$this->defaultValue = $value; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return mixed Default value |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getValueDefault() { |
|
52
|
|
|
return $this->withTransformations($this->defaultValue); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function getValueStored() { |
|
56
|
|
|
return $this->withTransformations( |
|
57
|
|
|
$this->getForm()->getValueStored($this->getName())); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function getValueToUse() { |
|
61
|
|
|
if ($this->getValueUser() !== null) |
|
62
|
|
|
return $this->getValueUser(); |
|
63
|
|
|
else if ($this->getValueStored() !== null) |
|
64
|
|
|
return $this->getValueStored(); |
|
65
|
|
|
else |
|
66
|
|
|
return $this->getValueDefault(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* |
|
71
|
|
|
* @return html\Input |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getControlElement() { |
|
74
|
|
|
return $this->control; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $name |
|
80
|
|
|
* @param string $title |
|
81
|
|
|
* @param html\Interface_\Submittable $control |
|
82
|
|
|
*/ |
|
83
|
|
|
public function init($name, $title, $control, $idSuffix = null) { |
|
84
|
|
|
$this->name = $name; |
|
85
|
|
|
$this->title = $title; |
|
86
|
|
|
$this->control = $control; |
|
87
|
|
|
$this->idSuffix = $idSuffix; |
|
88
|
|
|
if (strlen($title) > 0) |
|
89
|
|
|
$this->setAccessKey(mb_substr($title, 0, 1)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* |
|
94
|
|
|
* @return TemplateFormField |
|
95
|
|
|
* @throws exception\NotLazyEnough |
|
96
|
|
|
* @throws exception\AppendageTypeError |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getFieldTemplateClone($special = null) { |
|
99
|
|
|
|
|
100
|
|
|
$appendageName = form_\FormPost::FORM_FIELD_TEMPLATE . '_' . $special; |
|
101
|
|
|
|
|
102
|
|
|
if (!$this->existsInheritableAppendage($appendageName)) { |
|
103
|
|
|
$appendageName = form_\FormPost::FORM_FIELD_TEMPLATE; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$template = $this->getInheritableAppendage($appendageName); |
|
107
|
|
|
|
|
108
|
|
|
if ($template instanceof TemplateFormField) { |
|
109
|
|
|
return clone $template; |
|
110
|
|
|
} elseif ($template === null) { |
|
111
|
|
|
throw new exception\NotLazyEnough( |
|
112
|
|
|
sprintf( |
|
113
|
|
|
'There is no "%s" available for this Input', $appendageName |
|
114
|
|
|
) |
|
115
|
|
|
); |
|
116
|
|
|
} else { |
|
117
|
|
|
throw new exception\AppendageTypeError( |
|
118
|
|
|
sprintf( |
|
119
|
|
|
'Not an istance of TemplateFormFieldSingle "%s"', $appendageName |
|
120
|
|
|
) |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function describe() { |
|
126
|
|
|
return 'INPUT'; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
|
|
public function __toString() { |
|
134
|
|
|
if (!$this->filled) |
|
135
|
|
|
$this->fill(); |
|
136
|
|
|
|
|
137
|
|
|
return parent::__toString(); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
abstract public function fill(); |
|
141
|
|
|
|
|
142
|
|
|
public function setForm(Form $form) { |
|
143
|
|
|
$this->control->setAttribute('form', $form->getHtmlName()); |
|
144
|
|
|
$this->addInheritableAppendage('_FORM', $form); |
|
145
|
|
|
$form->addLogicalChild($this); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function setAccessKey($key) { |
|
149
|
|
|
$this->getControlElement()->setAttribute('accesskey', $key); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public function getHtmlTitle() { |
|
153
|
|
|
$accessKey = $this->getControlElement()->getAttribute('accesskey'); |
|
154
|
|
|
if (strlen($accessKey) !== 1) { |
|
155
|
|
|
return new html\Str($this->title); |
|
156
|
|
|
} else { |
|
157
|
|
|
$matches = []; |
|
158
|
|
|
if (preg_match('/^(.*?)(' . $accessKey . ')(.*)$/iu', $this->title, $matches)) { |
|
159
|
|
|
$container = new form_\Container; |
|
160
|
|
|
|
|
161
|
|
|
$u = new html\U(); |
|
162
|
|
|
$u->addChild(new html\Str($matches[2])); |
|
163
|
|
|
|
|
164
|
|
|
$container->addChild(new html\Str($matches[1])); |
|
|
|
|
|
|
165
|
|
|
$container->addChild($u); |
|
|
|
|
|
|
166
|
|
|
$container->addChild(new html\Str($matches[3])); |
|
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
return $container; |
|
169
|
|
|
} else { |
|
170
|
|
|
$container = new form_\Container; |
|
171
|
|
|
$container->addChild(new html\Str($this->title . ' (')); |
|
|
|
|
|
|
172
|
|
|
|
|
173
|
|
|
$u = new html\U(); |
|
174
|
|
|
$u->addChild(new html\Str($accessKey)); |
|
175
|
|
|
|
|
176
|
|
|
$container->addChild($u); |
|
|
|
|
|
|
177
|
|
|
|
|
178
|
|
|
$container->addChild(new html\Str(')')); |
|
|
|
|
|
|
179
|
|
|
|
|
180
|
|
|
return $container; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
protected $autofocusLevel = 5; |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* |
|
189
|
|
|
* @param bool $focus |
|
190
|
|
|
*/ |
|
191
|
|
|
public function setAutofocus($focus = true, $level = 10) { |
|
192
|
|
|
if ($focus) |
|
193
|
|
|
$this->autofocusLevel = $level; |
|
194
|
|
|
else |
|
195
|
|
|
$this->autofocusLevel = 0; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
public function getAutofocusLevel() { |
|
199
|
|
|
return $this->autofocusLevel; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
public function engageAutofocus() { |
|
203
|
|
|
$this->getControlElement()->setAttribute('autofocus', true); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* |
|
208
|
|
|
* @param boolean $required |
|
209
|
|
|
*/ |
|
210
|
|
|
public function setRequired($required = true) { |
|
211
|
|
|
$this->addValidityCheck(new form_\CheckMinLength(1)); |
|
212
|
|
|
$this->required = $required; |
|
213
|
|
|
$this->getControlElement()->setAttribute('required', (boolean) $required); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
} |
|
217
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: