|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package Cadmium\Framework\Form |
|
5
|
|
|
* @author Anton Romanov |
|
6
|
|
|
* @copyright Copyright (c) 2015-2017, Anton Romanov |
|
7
|
|
|
* @link http://cadmium-cms.com |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Form\Field { |
|
11
|
|
|
|
|
12
|
|
|
use Form, Str, Tag, Template; |
|
13
|
|
|
|
|
14
|
|
|
class Text extends Form\Field { |
|
15
|
|
|
|
|
16
|
|
|
# Field default value |
|
17
|
|
|
|
|
18
|
|
|
protected $value = ''; |
|
19
|
|
|
|
|
20
|
|
|
# Field data |
|
21
|
|
|
|
|
22
|
|
|
private $type = FORM_FIELD_TEXT, $maxlength = 0; |
|
23
|
|
|
|
|
24
|
|
|
# Field configuration |
|
25
|
|
|
|
|
26
|
|
|
protected $config = [ |
|
27
|
|
|
|
|
28
|
|
|
'multiline' => false, |
|
29
|
|
|
'codestyle' => false, |
|
30
|
|
|
'spaces' => '', |
|
31
|
|
|
'convert' => '', |
|
32
|
|
|
'transform' => '', |
|
33
|
|
|
|
|
34
|
|
|
'placeholder' => '', |
|
35
|
|
|
'readonly' => false, |
|
36
|
|
|
'autofocus' => false, |
|
37
|
|
|
'rows' => 0, |
|
38
|
|
|
'cols' => 0 |
|
39
|
|
|
]; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get a hidden input tag |
|
43
|
|
|
*/ |
|
44
|
|
|
|
|
45
|
|
|
private function getHidden() : Tag { |
|
46
|
|
|
|
|
47
|
|
|
return $this->getTag('input', ['type' => 'hidden', 'value' => $this->value]); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get a password input tag |
|
52
|
|
|
*/ |
|
53
|
|
|
|
|
54
|
|
|
private function getPassword() : Tag { |
|
55
|
|
|
|
|
56
|
|
|
return $this->getTag('input', ['type' => 'password', 'value' => '']); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get a captcha input tag |
|
61
|
|
|
*/ |
|
62
|
|
|
|
|
63
|
|
|
private function getCaptcha() : Tag { |
|
64
|
|
|
|
|
65
|
|
|
return $this->getTag('input', ['type' => 'text', 'value' => '']); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Get a text input tag |
|
70
|
|
|
*/ |
|
71
|
|
|
|
|
72
|
|
|
private function getText() : Tag { |
|
73
|
|
|
|
|
74
|
|
|
return $this->getTag('input', ['type' => 'text', 'value' => $this->value]); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get a textarea input tag |
|
79
|
|
|
*/ |
|
80
|
|
|
|
|
81
|
|
|
private function getTextarea() : Tag { |
|
82
|
|
|
|
|
83
|
|
|
$tag = $this->getTag('textarea', [], $this->value); |
|
84
|
|
|
|
|
85
|
|
|
if ($this->rows > 0) $tag->setAttribute('rows', $this->rows); |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
if ($this->cols > 0) $tag->setAttribute('cols', $this->cols); |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
# ------------------------ |
|
90
|
|
|
|
|
91
|
|
|
return $tag; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Process spaces |
|
96
|
|
|
*/ |
|
97
|
|
|
|
|
98
|
|
|
private function processSpaces() { |
|
99
|
|
|
|
|
100
|
|
|
if ($this->spaces === 'strip') $this->value = Str::stripSpaces($this->value); |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
else if ($this->spaces === 'single') $this->value = Str::singleSpaces($this->value); |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Process convert |
|
107
|
|
|
*/ |
|
108
|
|
|
|
|
109
|
|
|
private function processConvert() { |
|
110
|
|
|
|
|
111
|
|
|
if ($this->convert === 'url') $this->value = Str::toUrl($this->value, $this->maxlength); |
|
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
else if ($this->convert === 'var') $this->value = Str::toVar($this->value, $this->maxlength); |
|
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Process case transform |
|
118
|
|
|
*/ |
|
119
|
|
|
|
|
120
|
|
|
private function processTransform() { |
|
121
|
|
|
|
|
122
|
|
|
if ($this->transform === 'lower') $this->value = Str::toLower($this->value); |
|
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
else if ($this->transform === 'upper') $this->value = Str::toUpper($this->value); |
|
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Constructor |
|
129
|
|
|
*/ |
|
130
|
|
|
|
|
131
|
|
|
public function __construct(Form $form, string $key, string $value = '', |
|
132
|
|
|
|
|
133
|
|
|
string $type = FORM_FIELD_TEXT, int $maxlength = 0, array $config = []) { |
|
134
|
|
|
|
|
135
|
|
|
# Init field |
|
136
|
|
|
|
|
137
|
|
|
self::init($form, $key, $config); |
|
138
|
|
|
|
|
139
|
|
|
# Set data |
|
140
|
|
|
|
|
141
|
|
|
$this->type = $type; $this->maxlength = $maxlength; |
|
142
|
|
|
|
|
143
|
|
|
# Set value |
|
144
|
|
|
|
|
145
|
|
|
$this->setValue($value); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Set a value |
|
150
|
|
|
* |
|
151
|
|
|
* @return bool : true if the result value is not empty, otherwise false |
|
152
|
|
|
*/ |
|
153
|
|
|
|
|
154
|
|
|
public function setValue(string $value) : bool { |
|
155
|
|
|
|
|
156
|
|
|
if (($this->type === FORM_FIELD_PASSWORD) || ($this->type === FORM_FIELD_CAPTCHA)) { |
|
157
|
|
|
|
|
158
|
|
|
$this->value = Str::substr($value, 0, $this->maxlength); |
|
159
|
|
|
|
|
160
|
|
|
} else { |
|
161
|
|
|
|
|
162
|
|
|
$multiline = (($this->type === FORM_FIELD_TEXTAREA) && $this->multiline); |
|
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
$codestyle = (($this->type === FORM_FIELD_TEXTAREA) && $this->codestyle); |
|
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
$this->value = Str::formatInput($value, $this->maxlength, $multiline, $codestyle); |
|
167
|
|
|
|
|
168
|
|
|
# Process operations |
|
169
|
|
|
|
|
170
|
|
|
$this->processSpaces(); $this->processConvert(); $this->processTransform(); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
# ------------------------ |
|
174
|
|
|
|
|
175
|
|
|
return ('' !== $this->value); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Get a block |
|
180
|
|
|
*/ |
|
181
|
|
|
|
|
182
|
|
|
public function getBlock() : Template\Block { |
|
183
|
|
|
|
|
184
|
|
|
# Process hidden field |
|
185
|
|
|
|
|
186
|
|
|
if ($this->type === FORM_FIELD_HIDDEN) return $this->getHidden()->getBlock(); |
|
187
|
|
|
|
|
188
|
|
|
# Process visible field |
|
189
|
|
|
|
|
190
|
|
|
else if ($this->type === FORM_FIELD_PASSWORD) $tag = $this->getPassword(); |
|
191
|
|
|
|
|
192
|
|
|
else if ($this->type === FORM_FIELD_CAPTCHA) $tag = $this->getCaptcha(); |
|
193
|
|
|
|
|
194
|
|
|
else if ($this->type === FORM_FIELD_TEXTAREA) $tag = $this->getTextarea(); |
|
195
|
|
|
|
|
196
|
|
|
else $tag = $this->getText(); |
|
197
|
|
|
|
|
198
|
|
|
# Set appearance |
|
199
|
|
|
|
|
200
|
|
|
if ($this->maxlength > 0) $tag->setAttribute('maxlength', $this->maxlength); |
|
201
|
|
|
|
|
202
|
|
|
if ('' !== $this->placeholder) $tag->setAttribute('placeholder', $this->placeholder); |
|
|
|
|
|
|
203
|
|
|
|
|
204
|
|
|
if ($this->readonly) $tag->setAttribute('readonly', 'readonly'); |
|
|
|
|
|
|
205
|
|
|
|
|
206
|
|
|
if ($this->autofocus) $tag->setAttribute('autofocus', 'autofocus'); |
|
|
|
|
|
|
207
|
|
|
|
|
208
|
|
|
# ------------------------ |
|
209
|
|
|
|
|
210
|
|
|
return $tag->getBlock(); |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.