Completed
Push — master ( 30f23d...c3cbe9 )
by Derek Stephen
05:45
created

RadioRender::renderRadioInline()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 21
Ratio 100 %

Code Coverage

Tests 15
CRAP Score 2

Importance

Changes 0
Metric Value
dl 21
loc 21
c 0
b 0
f 0
ccs 15
cts 15
cp 1
rs 9.3142
cc 2
eloc 14
nc 2
nop 3
crap 2
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\Radio;
12
use DOMDocumentFragment;
13
use DOMElement;
14
use DOMNode;
15
use DOMText;
16
use InvalidArgumentException;
17
use LogicException;
18
19 View Code Duplication
class RadioRender extends AbstractFieldRender implements FieldRendererInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

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.

Loading history...
20
{
21
    /** @var DOMDocumentFragment $div */
22
    private $fragment;
23
24
    /**
25
     * @param FieldInterface $field
26
     * @param DOMElement $element
27
     * @return DOMNode
28
     */
29 8
    public function renderBlock(FieldInterface $field, DOMElement $element)
30
    {
31
        // We don't really want a containing div, so we'll ignore $element
32
        // and instead create a DOMDocumentFragment
33 8
        unset($element);
34 8
        $this->fragment = $this->dom->createDocumentFragment();
35
36
        // Make sure the FieldInterface is actually a Radio
37 8
        if (!$field instanceof Radio) {
38 1
            throw new InvalidArgumentException('Must be a Del\Form\Field\Radio');
39
        }
40
41 7
        $inline = $field->isRenderInline();
42
43 7
        $options = $field->getOptions();
44 7
        if (empty($options)) {
45 1
            throw new LogicException('You must set at least one option.');
46
        }
47
48
        // Loop through each radio element (the options)
49 6
        foreach ($options as $value => $label) {
50 6
            $radio = $this->processOption($field, $value, $label, $inline);
51 6
            $this->fragment->appendChild($radio);
52 6
        }
53
54 6
        return $this->fragment;
55
    }
56
57
58
    /**
59
     * @param FieldInterface $field
60
     * @param $value
61
     * @param $labelText
62
     * @return DOMElement
63
     */
64 6
    private function processOption(FieldInterface $field, $value, $labelText, $inline)
65
    {
66 6
        if ($inline === true) {
67 1
            return $this->renderRadioInline($field, $value, $labelText);
68
        }
69 5
        return $this->renderRadio($field, $value, $labelText);
70
    }
71
72
    /**
73
     * @param FieldInterface $field
74
     * @param $value
75
     * @param $labelText
76
     * @return DOMElement
77
     */
78 5
    private function renderRadio(FieldInterface $field, $value, $labelText)
79
    {
80 5
        $div = $this->dom->createElement('div');
81 5
        $div->setAttribute('class', 'radio');
82 5
        $radio = $this->renderRadioInline($field, $value, $labelText);
83 5
        $radio->removeAttribute('class');
84 5
        $div->appendChild($radio);
85 5
        return $div;
86
    }
87
88
    /**
89
     * @param FieldInterface $field
90
     * @param $value
91
     * @param $labelText
92
     * @return DOMElement
93
     */
94 6
    private function renderRadioInline(FieldInterface $field, $value, $labelText)
95
    {
96 6
        $label = $this->dom->createElement('label');
97 6
        $label->setAttribute('for', $field->getId());
98 6
        $label->setAttribute('class', 'radio-inline');
99
100 6
        $radio = $this->dom->createElement('input');
101 6
        $radio->setAttribute('type', 'radio');
102 6
        $radio->setAttribute('name', $field->getName());
103 6
        $radio->setAttribute('value', $value);
104 6
        $text = new DOMText($labelText);
105
106 6
        if($field->getValue() == $radio->getAttribute('value')) {
107 1
            $radio->setAttribute('checked', 'checked');
108 1
        }
109
110 6
        $label->appendChild($radio);
111 6
        $label->appendChild($text);
112
113 6
        return $label;
114
    }
115
}