HorizontalFormRenderer::renderFieldBlock()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 9
c 2
b 0
f 0
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 9.9666
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Del\Form\Renderer;
4
5
use Del\Form\Renderer\Error\HorizontalFormErrorRender;
6
use DOMElement;
7
use DOMNode;
8
use DOMText;
9
10
class HorizontalFormRenderer extends AbstractFormRenderer implements FormRendererInterface
11
{
12 5
    public function __construct()
13
    {
14 5
        parent::__construct();
15
16
        // Add horizontal form class
17 5
        $this->form->setAttribute('class', 'form-horizontal');
18 5
        $this->errorRenderer = new HorizontalFormErrorRender($this->getDom());
19
    }
20
21
    /**
22
     * @return DOMElement
23
     */
24 3
    public function renderFieldLabel()
25
    {
26 3
        $label = $this->createLabelElement();
27 3
        $label->setAttribute('class', 'col-sm-2 col-md-3 control-label');
28 3
        $text = $this->createText($this->field->getLabel());
29 3
        $label->appendChild($text);
30 3
        return $label;
31
    }
32
33
    /**
34
     * @return DOMElement
35
     */
36 3
    public function renderFieldBlock()
37
    {
38 3
        $class = $this->block->getAttribute('class').' form-group row';
39 3
        $this->block->setAttribute('class', $class);
40
41 3
        $div = $this->createElement('div');
42 3
        $div->setAttribute('class', 'col');
43
44 3
        $this->processField($div);
45
46 3
        $this->block->appendChild($div);
47
48 3
        if (!is_null($this->errors)) {
49 1
            $this->block->appendChild($this->errors);
50
        }
51
52 3
        return $this->block;
53
    }
54
55
    /**
56
     * @param DOMElement $div
57
     */
58 3
    private function processField(DOMElement $div)
59
    {
60 3
        switch (get_class($this->field)) {
61 3
            case 'Del\Form\Field\Submit':
62 3
                $div->appendChild($this->element);
63 3
                $div->setAttribute('class', 'col');
64 3
                break;
65 3
            case 'Del\Form\Field\Radio':
66 2
                $radioDiv = $this->surroundInDiv($this->element, 'radio');
67 2
                $this->block->appendChild($this->label);
68 2
                $div->appendChild($radioDiv);
69 2
                break;
70 3
            case 'Del\Form\Field\CheckBox':
71 1
                $checkboxDiv = $this->surroundInDiv($this->element, 'checkbox');
72 1
                $this->block->appendChild($this->label);
73 1
                $div->appendChild($checkboxDiv);
74 1
                break;
75
            default:
76 3
                $this->block->appendChild($this->label);
77 3
                $div->appendChild($this->element);
78
        }
79
    }
80
81
    /**
82
     * Surround an element in a div with a given class
83
     *
84
     * @param DOMNode $element
85
     * @param $class
86
     * @return DOMElement
87
     */
88 3
    private function surroundInDiv(DOMNode $element, $class)
89
    {
90 3
        $div = $this->createElement('div');
91 3
        $div->setAttribute('class', $class);
92 3
        $div->appendChild($element);
93 3
        return $div;
94
    }
95
96
}