-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathForm.php
More file actions
156 lines (118 loc) · 3.47 KB
/
Form.php
File metadata and controls
156 lines (118 loc) · 3.47 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
<?php
namespace Foolz\FoolFrame\Model;
use Symfony\Component\HttpFoundation\Request;
class Form
{
/**
* @var Request
*/
protected $request;
public function __construct(Request $request)
{
$this->request = $request;
}
public function __call($name, $args)
{
return call_user_func_array([$this, 'generic'], ['type' => $name] + $args);
}
public function open($args = false, $hidden = [])
{
if ($args === false) {
$args = ['action' => $this->request->getUri()];
} else if (is_string($args)) {
$args = '/' . ltrim($args, '/');
$args = ['action' => $this->request->getUriForPath($args)];
}
if (!isset($args['method'])) {
$args['method'] = 'POST';
}
$s = '<form'.$this->expandArgs($args).'>';
foreach($hidden as $name => $value) {
$s .= '<input type="hidden" name="'.htmlentities($name).'" value="'.htmlentities($value).'">';
}
return $s;
}
public function close()
{
return '</form>';
}
public function generic($type, $args)
{
if ($type) {
$args['type'] = $type;
}
return '<input'.$this->expandArgs($args).'>';
}
public function input($args)
{
if (!is_array($args)) {
$args = ['name' => $args];
}
return $this->generic('text', $args);
}
public function hidden($field, $value = null, $args = [])
{
$s = '<input type="hidden"';
$args['name'] = $field;
if ($value !== null) {
$args['value'] = $value;
}
$s .= $this->expandArgs($args).'>';
return $s;
}
public function textarea($args)
{
$value = '';
if (isset($args['value'])) {
$value = $args['value'];
unset($args['value']);
}
return '<textarea'.$this->expandArgs($args).'>'.htmlspecialchars($value).'</textarea>';
}
public function select($field, $default, $values, $args = [])
{
$s = '<select';
$args['name'] = $field;
$s .= $this->expandArgs($args).'>';
foreach ($values as $key => $value) {
$s .= '<option value="'.htmlentities($key).'"'.($default === $key ? ' selected' : '').'>'.htmlspecialchars($value).'</option>';
}
$s .= '</select>';
return $s;
}
public function radio($field, $value = null, $checked = false, $args = [], $type = 'radio')
{
$s = '<input type="'.$type.'"';
$args['name'] = $field;
if (is_array($field)) {
$args = $field;
} else {
if ($value) {
$args['value'] = $value;
} else {
$args['value'] = '';
}
if ($checked) {
$args['checked'] = 'checked';
}
}
$s .= $this->expandArgs($args).'>';
return $s;
}
public function checkbox($field, $value = null, $checked = false, $args = [])
{
return $this->radio($field, $value, $checked, $args, 'checkbox');
}
public function label($text, $for)
{
return '<label for="'.htmlentities($for).'">'.htmlspecialchars($text).'</label>';
}
protected function expandArgs($args)
{
$s = '';
foreach($args as $key => $value) {
$s .= ' '.htmlentities($key). '="'.htmlentities($value).'"';
}
return $s;
}
}