Completed
Push — master ( b236f6...1806cf )
by Derek Stephen
01:32
created

CheckboxRender::renderCheckboxInline()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 11
cts 11
cp 1
rs 8.9297
c 0
b 0
f 0
cc 6
nc 4
nop 3
crap 6
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
    /**
28
     * @param FieldInterface $field
29 7
     * @param DOMElement $element
30
     * @return DOMNode
31
     */
32 View Code Duplication
    public function renderBlock(FieldInterface $field, DOMElement $element)
0 ignored issues
show
Duplication introduced by
This method 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...
33 7
    {
34 7
        // We don't really want a containing div, so we'll ignore $element
35
        // and instead create a DOMDocumentFragment
36
        unset($element);
37 7
        $this->fragment = $this->getDom()->createDocumentFragment();
38 1
39
        // Make sure the FieldInterface is actually a Radio
40
        if (!$field instanceof CheckBox) {
41 6
            throw new InvalidArgumentException('Must be a Del\Form\Field\Checkbox');
42
        }
43 6
44 6
        $inline = $field->isRenderInline();
45 1
46
        $options = $field->getOptions();
47
        if (empty($options)) {
48
            throw new LogicException('You must set at least one option.');
49 5
        }
50 5
51 5
        // Loop through each checkbox element (the options)
52
        $this->isMultiCheckbox = count($options) > 1;
53
        foreach ($options as $value => $label) {
54 5
            $radio = $this->processOption($field, $value, $label, $inline);
55
            $this->fragment->appendChild($radio);
56
        }
57
58
        return $this->fragment;
59
    }
60
61
62
    /**
63
     * @param FieldInterface $field
64 5
     * @param $value
65
     * @param $labelText
66 5
     * @return DOMElement
67 1
     */
68
    private function processOption(FieldInterface $field, $value, $labelText, $inline)
69 4
    {
70
        if ($inline === true) {
71
            return $this->renderCheckboxInline($field, $value, $labelText);
72
        }
73
        return $this->renderCheckbox($field, $value, $labelText);
74
    }
75
76
    /**
77
     * @param FieldInterface $field
78 4
     * @param $value
79
     * @param $labelText
80 4
     * @return DOMElement
81 4
     */
82 4
    private function renderCheckbox(FieldInterface $field, $value, $labelText)
83 4
    {
84 4
        $div = $this->getDom()->createElement('div');
85 4
        $div->setAttribute('class', 'checkbox');
86
        $radio = $this->renderCheckboxInline($field, $value, $labelText);
87
        $radio->removeAttribute('class');
88
        $div->appendChild($radio);
89
        return $div;
90
    }
91
92
    /**
93
     * @param FieldInterface $field
94 5
     * @param $value
95
     * @param $labelText
96 5
     * @return DOMElement
97 5
     */
98 5
    private function renderCheckboxInline(FieldInterface $field, $value, $labelText)
99
    {
100 5
        $label = $this->getDom()->createElement('label');
101 5
        $label->setAttribute('for', $field->getId());
102 5
        $label->setAttribute('class', 'checkbox-inline');
103 5
104 5
        $radio = $this->getDom()->createElement('input');
105
        $radio->setAttribute('type', 'checkbox');
106 5
        $fieldName = $this->isMultiCheckbox ? $field->getName() . '[]' : $field->getName();
107 2
        $radio->setAttribute('name', $fieldName);
108
        $radio->setAttribute('value', $value);
109
        $text = $this->createText($labelText);
110 5
        $fieldValue = $field->getValue();
111 5
112
        if ($fieldValue === true || $fieldValue === $value || (is_array($fieldValue) && in_array($value, $fieldValue, true))) {
113 5
            $radio->setAttribute('checked', 'checked');
114
        }
115
116
        $label->appendChild($radio);
117
        $label->appendChild($text);
118
119
        return $label;
120
    }
121
}
122