Toggle.php
1.6 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
<?php
namespace AC\Form\Element;
use AC\Form\Element;
use AC\View;
class Toggle extends Element {
/**
* @var boolean
*/
private $checked;
/**
* @var string
*/
private $unchecked_value;
/**
* @var array
*/
private $container_attributes;
/**
* @var string
*/
private $container_class;
public function __construct( $name, $label, $checked = false, $value = null, $unchecked_value = 'false' ) {
parent::__construct( $name, [] );
$this->set_label( $label );
$this->checked = (bool) $checked;
$this->unchecked_value = (string) $unchecked_value;
$this->container_attributes = [];
if ( $value ) {
$this->set_value( $value );
}
}
public function set_container_attributes( $attributes ) {
if ( isset( $attributes['class'] ) ) {
$this->container_class = $attributes['class'];
unset( $attributes['class'] );
}
$this->container_attributes = (array) $attributes;
}
protected function get_type() {
return 'checkbox';
}
public function render() {
$view = new View( [
'id' => $this->get_name(),
'name' => $this->get_name(),
'label' => $this->get_label(),
'checked' => $this->checked,
'value' => $this->get_value(),
'unchecked_value' => $this->unchecked_value,
'attributes' => $this->get_attributes_as_string( $this->get_attributes() ),
'container_attributes' => $this->get_attributes_as_string( $this->container_attributes ),
'container_class' => $this->container_class,
] );
return $view->set_template( 'component/toggle-v2' )->render();
}
}