-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathField.php
More file actions
360 lines (306 loc) · 7.67 KB
/
Field.php
File metadata and controls
360 lines (306 loc) · 7.67 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php
namespace Gregwar\Formidable\Fields;
use Gregwar\Formidable\Language\LanguageAware;
/**
* Parent class for fields
*
* @author Grégoire Passault <g.passault@gmail.com>
*/
abstract class Field extends LanguageAware
{
/**
* Field type for the HTML code
*/
protected $type = 'text';
/**
* Field name
*/
protected $name;
protected $index = null;
protected $hook = null;
/**
* HTML attributes
*/
protected $attributes = array();
/**
* Field value
*/
protected $value = null;
/**
* Is this field required ?
*/
protected $required = false;
/**
* Regular expression
*/
protected $regex;
/**
* Text size
*/
protected $minlength;
protected $maxlength;
/**
* Pretty name, for error messages
*/
protected $prettyname;
/**
* Is this field read only?
*/
protected $readonly = false;
/**
* Is the value changed?
*/
protected $valueChanged = false;
/**
* Constraints on the field
*/
protected $constraints = array();
/**
* Data for the mapping
*/
protected $mapping;
public function __sleep()
{
return array(
'constraints', 'mapping', 'valueChanged',
'readonly', 'prettyname', 'minlength', 'maxlength', 'regex', 'required',
'type', 'name', 'attributes', 'value', 'index'
);
}
/**
* Define an attribute
*/
public function setAttribute($name, $value)
{
$this->attributes[$name] = $value;
}
/**
* Get an attribute
*/
public function getAttribute($name)
{
if ($this->hasAttribute($name)) {
return $this->attributes[$name];
} else {
return null;
}
}
/**
* Does this has the attribute $name ?
*/
public function hasAttribute($name)
{
return isset($this->attributes[$name]);
}
/**
* Remove the attribute $name
*/
public function unsetAttribute($name)
{
unset($this->attributes[$name]);
}
/**
* Function called by the dispatcher
*/
public function push($name, $value = null)
{
switch ($name) {
case 'name':
$this->setName($value);
break;
case 'type':
break;
case 'value':
$this->setValue($value, true);
break;
case 'required':
$this->required = true;
break;
case 'regex':
$this->regex = $value;
break;
case 'minlength':
$this->minlength = $value;
break;
case 'maxlength':
$this->maxlength = $value;
$this->attributes['maxlength'] = $value;
break;
case 'mapping':
$this->mapping = $value;
break;
case 'prettyname':
$this->prettyname = $value;
break;
case 'readonly':
$this->readonly = true;
$this->attributes['readonly'] = 'readonly';
break;
default:
if (preg_match('#^([a-z0-9_-]+)$#mUsi', $name)) {
if (null !== $value) {
$this->setAttribute($name, $value);
} else {
$this->setAttribute($name, $name);
}
}
}
}
public function printName()
{
if ($this->prettyname) {
return $this->prettyname;
}
return $this->name;
}
/**
* Constraints check
*/
public function check()
{
if ($this->valueChanged && $this->readonly) {
return array('read_only', $this->printName());
}
if (null === $this->value || '' === $this->value) {
if ($this->required) {
return array('value_required', $this->printName());
}
} else {
// Expressions régulière
if ($this->regex) {
if (!preg_match('/'.$this->regex.'/mUsi', $this->value)) {
return array('bad_format', $this->printName());
}
}
// Longueur minimum et maximum
if ($this->minlength && strlen($this->value) < $this->minlength) {
return array('at_least', $this->printName(), $this->minlength);
}
if ($this->maxlength && strlen($this->value) > $this->maxlength) {
return array('not_more', $this->printName(), $this->maxlength);
}
}
// Custom constraints
foreach ($this->constraints as $constraint) {
$error = $constraint($this->value);
if ($error) {
return $error;
}
}
}
public function getName()
{
$name = $this->getBaseName();
if ($this->index !== null) {
if ($this->index) {
foreach ($this->index as $part) {
$name .= '['.$part.']';
}
} else {
$name .= '[]';
}
}
if ($this->hook !== null) {
$hook = $this->hook;
return $hook($name);
} else {
return $name;
}
}
public function getBaseName()
{
return $this->name;
}
public function getIndex()
{
return $this->index;
}
public function hookName(\Closure $hook)
{
$this->hook = $hook;
}
public function setName($name)
{
if (preg_match('#^(.+)\[(.*)\]$#mUsi', $name, $match) == 1) {
$this->name = $match[1];
$this->index = array();
if ($match[2]) {
$parts = explode('][', $match[2]);
foreach ($parts as $part) {
$this->index[] = $part;
}
}
} else {
$this->name = $name;
}
}
public function getMappingName()
{
return $this->mapping;
}
public function getValue()
{
return $this->value;
}
public function getMappingValue()
{
return $this->getValue();
}
/**
* Définition de la valeur
*/
public function setValue($value, $default = false)
{
if ($value != $this->value && !$default) {
$this->valueChanged = true;
}
if (is_string($value) || is_int($value) || is_float($value)) {
$this->value = (string)$value;
} else {
$this->value = null;
}
}
public function __toString()
{
return $this->getHtml();
}
public function getHtml()
{
$html = '<input ';
foreach ($this->attributes as $name => $value) {
$html.= $name.'="'.$value.'" ';
}
if ($this->required) {
$html.= 'required="required" ';
}
$html.= 'type="'.$this->type.'" ';
$html.= 'name="'.$this->getName().'" ';
if (($value = $this->getValue()) !== null && gettype($value) != 'object') {
$html.= 'value="'.htmlspecialchars($value).'" ';
}
$html.= '/>';
return $html;
}
public function getSource()
{
return '';
}
public function source($values)
{
}
public function needJs()
{
return false;
}
public function addConstraint($closure)
{
if (!$closure instanceof \Closure) {
throw new \InvalidArgumentException('addConstraint() argument should be a \Closure');
}
$this->constraints[] = $closure;
}
public function readOnly()
{
return $this->readonly;
}
}