Debian Codegraph @

vendor/ipl/html/src/FormElement/CheckboxElement.php

raw
1 <?php
2
3 namespace ipl\Html\FormElement;
4
5 use ipl\Html\Attributes;
6
7 class CheckboxElement extends InputElement
8 {
9 /** @var bool Whether the checkbox is checked */
10 protected $checked = false;
11
12 /** @var string Value of the checkbox when it is checked */
13 protected $checkedValue = 'y';
14
15 /** @var string Value of the checkbox when it is not checked */
16 protected $uncheckedValue = 'n';
17
18 protected $type = 'checkbox';
19
20 /**
21 * Get whether the checkbox is checked
22 *
23 * @return bool
24 */
25 public function isChecked()
26 {
27 return $this->checked;
28 }
29
30 /**
31 * Set whether the checkbox is checked
32 *
33 * @param bool $checked
34 *
35 * @return $this
36 */
37 public function setChecked($checked)
38 {
39 $this->checked = (bool) $checked;
40
41 return $this;
42 }
43
44 /**
45 * Get the value of the checkbox when it is checked
46 *
47 * @return string
48 */
49 public function getCheckedValue()
50 {
51 return $this->checkedValue;
52 }
53
54 /**
55 * Set the value of the checkbox when it is checked
56 *
57 * @param string $checkedValue
58 *
59 * @return $this
60 */
61 public function setCheckedValue($checkedValue)
62 {
63 $this->checkedValue = $checkedValue;
64
65 return $this;
66 }
67
68 /**
69 * Get the value of the checkbox when it is not checked
70 *
71 * @return string
72 */
73 public function getUncheckedValue()
74 {
75 return $this->uncheckedValue;
76 }
77
78 /**
79 * Set the value of the checkbox when it is not checked
80 *
81 * @param string $uncheckedValue
82 *
83 * @return $this
84 */
85 public function setUncheckedValue($uncheckedValue)
86 {
87 $this->uncheckedValue = $uncheckedValue;
88
89 return $this;
90 }
91
92 public function setValue($value)
93 {
94 if (is_bool($value)) {
95 $value = $value ? $this->getCheckedValue() : $this->getUncheckedValue();
96 }
97
98 $this->setChecked($value === $this->getCheckedValue());
99
100 return parent::setValue($value);
101 }
102
103 public function getValueAttribute()
104 {
105 return $this->getCheckedValue();
106 }
107
108 protected function registerAttributeCallbacks(Attributes $attributes)
109 {
110 parent::registerAttributeCallbacks($attributes);
111
112 $attributes
113 ->registerAttributeCallback('checked', [$this, 'isChecked'], [$this, 'setChecked'])
114 ->registerAttributeCallback('checkedValue', null, [$this, 'setCheckedValue'])
115 ->registerAttributeCallback('uncheckedValue', null, [$this, 'setUncheckedValue']);
116 }
117
118 public function renderUnwrapped()
119 {
120 $html = parent::renderUnwrapped();
121
122 $value = $this->getAttribute('disabled')->getValue() && $this->isChecked()
123 ? $this->getCheckedValue()
124 : $this->getUncheckedValue();
125
126 return (new HiddenElement($this->getValueOfNameAttribute(), ['value' => $value])) . $html;
127 }
128
129 /**
130 * Determine if the checkbox is considered "checked".
131 * Returns true if the current value matches the checked value, otherwise false.
132 */
133 public function hasValue(): bool
134 {
135 return $this->getValue() === $this->getCheckedValue();
136 }
137 }