Completed
Push — master ( 077dd8...ca59d5 )
by Derek Stephen
01:55
created

HorizontalFormRenderer::renderFieldBlock()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

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