1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* User: delboy1978uk |
4
|
|
|
* Date: 04/12/2016 |
5
|
|
|
* Time: 22:33 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Del\Form\Renderer\Field; |
9
|
|
|
|
10
|
|
|
use Del\Form\Field\FieldInterface; |
11
|
|
|
use Del\Form\Field\CheckBox; |
12
|
|
|
use DOMDocumentFragment; |
13
|
|
|
use DOMElement; |
14
|
|
|
use DOMNode; |
15
|
|
|
use DOMText; |
16
|
|
|
use InvalidArgumentException; |
17
|
|
|
use LogicException; |
18
|
|
|
|
19
|
|
|
class CheckboxRender extends AbstractFieldRender implements FieldRendererInterface |
20
|
|
|
{ |
21
|
|
|
/** @var DOMDocumentFragment $div */ |
22
|
|
|
private $fragment; |
23
|
|
|
|
24
|
|
|
/** @var bool $isMultiCheckbox */ |
25
|
|
|
private $isMultiCheckbox = false; |
26
|
|
|
|
27
|
|
|
private $counter = 0; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param FieldInterface $field |
31
|
|
|
* @param DOMElement $element |
32
|
9 |
|
* @return DOMNode |
33
|
|
|
*/ |
34
|
|
View Code Duplication |
public function renderBlock(FieldInterface $field, DOMElement $element) |
|
|
|
|
35
|
|
|
{ |
36
|
9 |
|
// We don't really want a containing div, so we'll ignore $element |
37
|
9 |
|
// and instead create a DOMDocumentFragment |
38
|
|
|
unset($element); |
39
|
|
|
$this->fragment = $this->getDom()->createDocumentFragment(); |
40
|
9 |
|
|
41
|
1 |
|
// Make sure the FieldInterface is actually a Radio |
42
|
|
|
if (!$field instanceof CheckBox) { |
43
|
|
|
throw new InvalidArgumentException('Must be a Del\Form\Field\Checkbox'); |
44
|
8 |
|
} |
45
|
|
|
|
46
|
8 |
|
$inline = $field->isRenderInline(); |
47
|
8 |
|
|
48
|
1 |
|
$options = $field->getOptions(); |
49
|
|
|
if (empty($options)) { |
50
|
|
|
throw new LogicException('You must set at least one option.'); |
51
|
|
|
} |
52
|
7 |
|
|
53
|
7 |
|
// Loop through each checkbox element (the options) |
54
|
7 |
|
$this->isMultiCheckbox = count($options) > 1; |
55
|
7 |
|
foreach ($options as $value => $label) { |
56
|
|
|
$radio = $this->processOption($field, $value, $label, $inline); |
57
|
|
|
$this->fragment->appendChild($radio); |
58
|
7 |
|
} |
59
|
|
|
|
60
|
|
|
return $this->fragment; |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param FieldInterface $field |
66
|
|
|
* @param $value |
67
|
|
|
* @param $labelText |
68
|
7 |
|
* @return DOMElement |
69
|
|
|
*/ |
70
|
7 |
|
private function processOption(FieldInterface $field, $value, $labelText, $inline) |
71
|
1 |
|
{ |
72
|
|
|
return $this->renderCheckbox($field, $value, $labelText, $inline); |
|
|
|
|
73
|
6 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param CheckBox $field |
77
|
|
|
* @param $value |
78
|
|
|
* @param $labelText |
79
|
|
|
* @return DOMElement |
80
|
|
|
*/ |
81
|
|
|
private function renderCheckbox(CheckBox $field, $value, $labelText, $inline) |
82
|
6 |
|
{ |
83
|
|
|
$div = $this->getDom()->createElement('div'); |
84
|
6 |
|
$class = $inline ? 'form-check-inline' : 'form-check'; |
85
|
6 |
|
$div->setAttribute('class', $class); |
86
|
6 |
|
$checkbox = $this->renderCheckboxInline($field, $value); |
87
|
6 |
|
$div->appendChild($checkbox); |
88
|
6 |
|
$div->appendChild($this->getLabel($field, $labelText)); |
89
|
6 |
|
|
90
|
|
|
return $div; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param FieldInterface $field |
95
|
|
|
* @param string $labelText |
96
|
|
|
* @return DOMElement |
97
|
|
|
*/ |
98
|
7 |
View Code Duplication |
private function getLabel(FieldInterface $field, string $labelText): DOMElement |
|
|
|
|
99
|
|
|
{ |
100
|
7 |
|
$this->counter ++; |
101
|
7 |
|
$label = $this->getDom()->createElement('label'); |
102
|
7 |
|
$label->setAttribute('for', $field->getId() . $this->counter); |
103
|
|
|
$label->setAttribute('class', 'form-check-label'); |
104
|
7 |
|
$text = $this->createText($labelText); |
105
|
7 |
|
$label->appendChild($text); |
106
|
7 |
|
|
107
|
7 |
|
return $label; |
108
|
7 |
|
} |
109
|
7 |
|
|
110
|
7 |
|
/** |
111
|
|
|
* @param FieldInterface $field |
112
|
7 |
|
* @param $value |
113
|
3 |
|
* @param $labelText |
114
|
|
|
* @return DOMElement |
115
|
|
|
*/ |
116
|
7 |
|
private function renderCheckboxInline(FieldInterface $field, $value) |
117
|
7 |
|
{ |
118
|
|
|
$checkbox = $this->getDom()->createElement('input'); |
119
|
7 |
|
$checkbox->setAttribute('class', 'form-check-input'); |
120
|
|
|
$checkbox->setAttribute('type', 'checkbox'); |
121
|
|
|
$fieldName = $this->isMultiCheckbox ? $field->getName() . '[]' : $field->getName(); |
122
|
|
|
$checkbox->setAttribute('name', $fieldName); |
123
|
|
|
$checkbox->setAttribute('value', $value); |
124
|
|
|
$fieldValue = $field->getValue(); |
125
|
|
|
|
126
|
|
|
if ($fieldValue === true || $fieldValue == $value || (is_array($fieldValue) && in_array($value, $fieldValue, true))) { |
127
|
|
|
$checkbox->setAttribute('checked', 'checked'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $checkbox; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.