1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Del\Form\Renderer\Field;
|
4
|
|
|
|
5
|
|
|
use Del\Form\Field\FieldInterface;
|
6
|
|
|
use Del\Form\Field\Radio;
|
7
|
|
|
use DOMDocumentFragment;
|
8
|
|
|
use DOMElement;
|
9
|
|
|
use DOMNode;
|
10
|
|
|
use DOMText;
|
11
|
|
|
use InvalidArgumentException;
|
12
|
|
|
use LogicException;
|
13
|
|
|
|
14
|
|
|
class RadioRender extends AbstractFieldRender implements FieldRendererInterface
|
15
|
|
|
{
|
16
|
|
|
/** @var DOMDocumentFragment $div */
|
17
|
|
|
private $fragment;
|
18
|
|
|
|
19
|
|
|
private $counter = 0;
|
20
|
|
|
|
21
|
|
|
/**
|
22
|
|
|
* @param FieldInterface $field
|
23
|
|
|
* @param DOMElement $element
|
24
|
10 |
|
* @return DOMNode
|
25
|
|
|
*/
|
26
|
|
View Code Duplication |
public function renderBlock(FieldInterface $field, DOMElement $element)
|
|
|
|
|
27
|
|
|
{
|
28
|
10 |
|
// We don't really want a containing div, so we'll ignore $element
|
29
|
10 |
|
// and instead create a DOMDocumentFragment
|
30
|
|
|
unset($element);
|
31
|
|
|
$this->fragment = $this->getDom()->createDocumentFragment();
|
32
|
10 |
|
|
33
|
1 |
|
// Make sure the FieldInterface is actually a Radio
|
34
|
|
|
if (!$field instanceof Radio) {
|
35
|
|
|
throw new InvalidArgumentException('Must be a Del\Form\Field\Radio');
|
36
|
9 |
|
}
|
37
|
|
|
|
38
|
9 |
|
$inline = $field->isRenderInline();
|
39
|
9 |
|
|
40
|
1 |
|
$options = $field->getOptions();
|
41
|
|
|
if (empty($options)) {
|
42
|
|
|
throw new LogicException('You must set at least one option.');
|
43
|
|
|
}
|
44
|
8 |
|
|
45
|
8 |
|
// Loop through each radio element (the options)
|
46
|
8 |
|
foreach ($options as $value => $label) {
|
47
|
|
|
$radio = $this->renderRadio($field, $value, $label, $inline);
|
48
|
|
|
$this->fragment->appendChild($radio);
|
49
|
8 |
|
}
|
50
|
|
|
|
51
|
|
|
return $this->fragment;
|
|
|
|
|
52
|
|
|
}
|
53
|
|
|
|
54
|
|
|
/**
|
55
|
|
|
* @param FieldInterface $field
|
56
|
|
|
* @param $value
|
57
|
|
|
* @param $labelText
|
58
|
|
|
* @param $inline
|
59
|
8 |
|
* @return DOMElement
|
60
|
|
|
*/
|
61
|
8 |
|
private function renderRadio(FieldInterface $field, $value, $labelText, $inline)
|
62
|
2 |
|
{
|
63
|
|
|
$div = $this->createElement('div');
|
64
|
7 |
|
$class = $inline ? 'form-check-inline' : 'form-check';
|
65
|
|
|
$div->setAttribute('class', $class);
|
66
|
|
|
$radio = $this->renderRadioInline($field, $value, $labelText);
|
67
|
|
|
$label = $this->getLabel($field, $labelText);
|
68
|
|
|
$div->appendChild($radio);
|
69
|
|
|
$div->appendChild($label);
|
70
|
|
|
|
71
|
|
|
return $div;
|
72
|
|
|
}
|
73
|
7 |
|
|
74
|
|
|
/**
|
75
|
7 |
|
* @param FieldInterface $field
|
76
|
7 |
|
* @param string $labelText
|
77
|
7 |
|
* @return DOMElement
|
78
|
7 |
|
*/
|
79
|
7 |
View Code Duplication |
private function getLabel(FieldInterface $field, string $labelText): DOMElement
|
|
|
|
|
80
|
7 |
|
{
|
81
|
|
|
$this->counter ++;
|
82
|
|
|
$label = $this->getDom()->createElement('label');
|
83
|
|
|
$label->setAttribute('for', $field->getId() . $this->counter);
|
84
|
|
|
$label->setAttribute('class', 'form-check-label');
|
85
|
|
|
$text = $this->createText($labelText);
|
86
|
|
|
$label->appendChild($text);
|
87
|
|
|
|
88
|
|
|
return $label;
|
89
|
8 |
|
}
|
90
|
|
|
|
91
|
8 |
|
/**
|
92
|
8 |
|
* @param FieldInterface $field
|
93
|
8 |
|
* @param $value
|
94
|
|
|
* @param $labelText
|
95
|
8 |
|
* @return DOMElement
|
96
|
8 |
|
*/
|
97
|
8 |
|
private function renderRadioInline(FieldInterface $field, $value, $labelText)
|
98
|
8 |
|
{
|
99
|
8 |
|
$radio = $this->createElement('input');
|
100
|
|
|
$radio->setAttribute('class', 'form-check-input');
|
101
|
8 |
|
$radio->setAttribute('type', 'radio');
|
102
|
2 |
|
$radio->setAttribute('name', $field->getName());
|
103
|
|
|
$radio->setAttribute('value', $value);
|
104
|
|
|
$text = $this->createText($labelText);
|
|
|
|
|
105
|
8 |
|
|
106
|
8 |
|
if ($field->getValue() == $radio->getAttribute('value')) {
|
107
|
|
|
$radio->setAttribute('checked', 'checked');
|
108
|
8 |
|
}
|
109
|
|
|
|
110
|
|
|
return $radio;
|
111
|
|
|
}
|
112
|
|
|
} |
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.