HorizontalFormRenderer::processField()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 4

Importance

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