Input.php
1.2 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
<?php
namespace AC\Form\Element;
use AC\Form\Element;
class Input extends Element
{
protected function is_valid_type($type)
{
$valid_types = [
'hidden',
'text',
'number',
'email',
'radio',
'checkbox',
];
return in_array($type, $valid_types);
}
public function render(): string
{
$template = '<input %s>%s';
$attributes = $this->get_attributes();
$attributes['name'] = $this->get_name();
$attributes['id'] = $this->get_id();
$attributes['value'] = $this->get_value();
$attributes['type'] = $this->get_type();
return sprintf($template, $this->get_attributes_as_string($attributes), $this->render_description());
}
public function get_type()
{
$type = $this->get_attribute('type');
if ( ! $type) {
return 'text';
}
return strtolower($type);
}
/**
* @param string $type
*
* @return $this
*/
public function set_type($type)
{
if ($this->is_valid_type($type)) {
$this->set_attribute('type', $type);
}
return $this;
}
}