1 | <?php |
||
2 | |||
3 | namespace Arbory\Base\Admin\Form\Controls; |
||
4 | |||
5 | use Arbory\Base\Html\Elements\Content; |
||
6 | use Arbory\Base\Html\Elements\Element; |
||
7 | use Arbory\Base\Html\Elements\Inputs\Input as InputElement; |
||
8 | |||
9 | class CheckboxControl extends InputControl |
||
10 | { |
||
11 | protected $type = 'checkbox'; |
||
12 | |||
13 | /** |
||
14 | * @var bool |
||
15 | */ |
||
16 | protected $checked = false; |
||
17 | |||
18 | /** |
||
19 | * @param Element $control |
||
20 | * @return Content |
||
21 | * |
||
22 | * @throws \Arbory\Base\Exceptions\BadMethodCallException |
||
23 | */ |
||
24 | public function render(Element $control) |
||
25 | { |
||
26 | $control->setType($this->type); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
27 | $input = parent::render($control); |
||
28 | |||
29 | $content = new Content(); |
||
30 | |||
31 | if ($this->isReadOnly()) { |
||
32 | $input->addAttributes(['disabled' => '']); |
||
33 | |||
34 | if ($this->isChecked()) { |
||
35 | $input->attributes()->forget( |
||
36 | ['id'] |
||
37 | ); |
||
38 | |||
39 | $hidden = (new InputElement())->setType('hidden'); |
||
40 | |||
41 | $hidden->setName($this->getName()); |
||
42 | $hidden->setValue($this->getValue()); |
||
43 | |||
44 | $content->push($hidden); |
||
45 | } |
||
46 | } |
||
47 | |||
48 | if ($this->isChecked()) { |
||
49 | $input->attributes()->put('checked', ''); |
||
50 | } |
||
51 | |||
52 | $content->push($input); |
||
53 | |||
54 | return $content; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @return bool |
||
59 | */ |
||
60 | public function isChecked(): bool |
||
61 | { |
||
62 | return $this->checked; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @param bool $checked |
||
67 | * @return CheckboxControl |
||
68 | */ |
||
69 | public function setChecked(bool $checked): self |
||
70 | { |
||
71 | $this->checked = $checked; |
||
72 | |||
73 | return $this; |
||
74 | } |
||
75 | } |
||
76 |