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

CheckboxRender::renderBlock()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 13

Duplication

Lines 27
Ratio 100 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 0
Metric Value
dl 27
loc 27
c 0
b 0
f 0
ccs 14
cts 14
cp 1
rs 8.5806
cc 4
eloc 13
nc 4
nop 2
crap 4
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 View Code Duplication
class CheckboxRender 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 7
    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 7
        unset($element);
34 7
        $this->fragment = $this->dom->createDocumentFragment();
35
36
        // Make sure the FieldInterface is actually a Radio
37 7
        if (!$field instanceof CheckBox) {
38 1
            throw new InvalidArgumentException('Must be a Del\Form\Field\Checkbox');
39
        }
40
41 6
        $inline = $field->isRenderInline();
42
43 6
        $options = $field->getOptions();
44 6
        if (empty($options)) {
45 1
            throw new LogicException('You must set at least one option.');
46
        }
47
48
        // Loop through each checkbox element (the options)
49 5
        foreach ($options as $value => $label) {
50 5
            $radio = $this->processOption($field, $value, $label, $inline);
51 5
            $this->fragment->appendChild($radio);
52 5
        }
53
54 5
        return $this->fragment;
55
    }
56
57
58
    /**
59
     * @param FieldInterface $field
60
     * @param $value
61
     * @param $labelText
62
     * @return DOMElement
63
     */
64 5
    private function processOption(FieldInterface $field, $value, $labelText, $inline)
65
    {
66 5
        if ($inline === true) {
67 1
            return $this->renderCheckboxInline($field, $value, $labelText);
68
        }
69 4
        return $this->renderCheckbox($field, $value, $labelText);
70
    }
71
72
    /**
73
     * @param FieldInterface $field
74
     * @param $value
75
     * @param $labelText
76
     * @return DOMElement
77
     */
78 4
    private function renderCheckbox(FieldInterface $field, $value, $labelText)
79
    {
80 4
        $div = $this->dom->createElement('div');
81 4
        $div->setAttribute('class', 'checkbox');
82 4
        $radio = $this->renderCheckboxInline($field, $value, $labelText);
83 4
        $radio->removeAttribute('class');
84 4
        $div->appendChild($radio);
85 4
        return $div;
86
    }
87
88
    /**
89
     * @param FieldInterface $field
90
     * @param $value
91
     * @param $labelText
92
     * @return DOMElement
93
     */
94 5
    private function renderCheckboxInline(FieldInterface $field, $value, $labelText)
95
    {
96 5
        $label = $this->dom->createElement('label');
97 5
        $label->setAttribute('for', $field->getId());
98 5
        $label->setAttribute('class', 'checkbox-inline');
99
100 5
        $radio = $this->dom->createElement('input');
101 5
        $radio->setAttribute('type', 'checkbox');
102 5
        $radio->setAttribute('name', $field->getName().'[]');
103 5
        $radio->setAttribute('value', $value);
104 5
        $text = new DOMText($labelText);
105
106 5
        if(in_array($value, $field->getValue())) {
107 2
            $radio->setAttribute('checked', 'checked');
108 2
        }
109
110 5
        $label->appendChild($radio);
111 5
        $label->appendChild($text);
112
113 5
        return $label;
114
    }
115
116
117
}