Completed
Push — master ( 594724...afa8c7 )
by Derek Stephen
02:00
created

MultiCheckboxRender::renderBlock()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 26

Duplication

Lines 26
Ratio 100 %

Importance

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