RadioRender::renderRadioInline()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 9
c 3
b 0
f 0
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.9666
cc 2
nc 2
nop 3
crap 2
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 InvalidArgumentException;
11
use LogicException;
12
13
class RadioRender extends AbstractFieldRender
14
{
15
    private DOMDocumentFragment $fragment;
16
    private int $counter = 0;
17
18 10
    public function renderBlock(FieldInterface $field, DOMElement $element): DOMNode
19
    {
20
        // We don't really want a containing div, so we'll ignore $element
21
        // and instead create a DOMDocumentFragment
22 10
        $this->fragment = $this->getDom()->createDocumentFragment();
23
24
        // Make sure the FieldInterface is actually a Radio
25 10
        if (!$field instanceof Radio) {
26 1
            throw new InvalidArgumentException('Must be a Del\Form\Field\Radio');
27
        }
28
29 9
        $inline = $field->isRenderInline();
30 9
        $options = $field->getOptions();
31
32 9
        if (empty($options)) {
33 1
            throw new LogicException('You must set at least one option.');
34
        }
35
36
        // Loop through each radio element (the options)
37 8
        foreach ($options as $value => $label) {
38 8
            $radio = $this->renderRadio($field, $value, $label, $inline);
39 8
            $this->fragment->appendChild($radio);
40
        }
41
42 8
        return $this->fragment;
43
    }
44
45 8
    private function renderRadio(FieldInterface $field, $value, $labelText, $inline): DOMElement
46
    {
47 8
        $div = $this->createElement('div');
48 8
        $class = $inline ? 'form-check-inline' : 'form-check';
49 8
        $div->setAttribute('class', $class);
50 8
        $radio = $this->renderRadioInline($field, $value, $labelText);
51 8
        $label = $this->getLabel($field, $labelText);
52 8
        $div->appendChild($radio);
53 8
        $div->appendChild($label);
54
55 8
        return $div;
56
    }
57
58 8
    private function getLabel(FieldInterface $field, string $labelText): DOMElement
59
    {
60 8
        $this->counter ++;
61 8
        $label = $this->getDom()->createElement('label');
62 8
        $label->setAttribute('for', $field->getId() . $this->counter);
63 8
        $label->setAttribute('class', 'form-check-label');
64 8
        $text = $this->createText($labelText);
65 8
        $label->appendChild($text);
66
67 8
        return $label;
68
    }
69
70 8
    private function renderRadioInline(FieldInterface $field, $value, $labelText): DOMElement
71
    {
72 8
        $radio = $this->createElement('input');
73 8
        $radio->setAttribute('class', 'form-check-input');
74 8
        $radio->setAttribute('type', 'radio');
75 8
        $radio->setAttribute('name', $field->getName());
76 8
        $radio->setAttribute('value', $value);
77
        /** @todo label? do we eed this? */
78 8
        $text = $this->createText($labelText);
0 ignored issues
show
Unused Code introduced by
The assignment to $text is dead and can be removed.
Loading history...
79
80 8
        if ($field->getValue() == $radio->getAttribute('value')) {
81 2
            $radio->setAttribute('checked', 'checked');
82
        }
83
84 8
        return $radio;
85
    }
86
}
87