Completed
Push — master ( a1a3b2...ba72b8 )
by Derek Stephen
07:25
created

FormRenderer::createChildElement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * User: delboy1978uk
4
 * Date: 29/11/2016
5
 * Time: 19:44
6
 */
7
8
namespace Del\Form;
9
10
use Del\Form\Field\FieldInterface;
11
use DOMDocument;
12
use DOMElement;
13
14
class FormRenderer
15
{
16
    /** @var DOMDocument $dom */
17
    private $dom;
18
19
    /** @var DomElement $form */
20
    private $form;
21
22
    /** @var bool $displayErrors */
23
    private $displayErrors;
24
25 16
    public function __construct($name)
26
    {
27 16
        $this->dom = new DOMDocument();
28 16
        $form = $this->dom->createElement('form');
29 16
        $form->setAttribute('name', $name);
30 16
        $this->form = $form;
0 ignored issues
show
Documentation Bug introduced by
It seems like $form of type object<DOMElement> is incompatible with the declared type object<Del\Form\DomElement> of property $form.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
31 16
    }
32
33 4
    public function render(AbstractForm $form, $displayErrors = true)
34
    {
35 4
        $this->displayErrors = $displayErrors;
36 4
        $method = $form->getMethod() ?: AbstractForm::METHOD_POST;
37 4
        $id = $form->getId() ?: $this->form->getAttribute('name');
38 4
        $action = $form->getAction() ?: $this->form->getAttribute('action');
39 4
        $encType = $form->getEncType() ?: $this->form->getAttribute('enc-type');
40 4
        $class = $form->getClass();
41
42 4
        $this->form->setAttribute('id', $id);
43 4
        $this->form->setAttribute('method', $method);
44 4
        $this->form->setAttribute('class', $class);
45 4
        $this->form->setAttribute('action', $action);
46 4
        $this->form->setAttribute('enctype', $encType);
47
48 4
        $fields = $form->getFields();
49
50 4
        $fields->rewind();
51 4
        while ($fields->valid()) {
52 3
            $current = $fields->current();
53 3
            $child = $this->createFieldDOM($current);
54 3
            $this->form->appendChild($child);
55 3
            $fields->next();
56 3
        }
57 4
        $fields->rewind();
58
59 4
        $this->dom->appendChild($this->form);
60
61 4
        return $this->dom->saveHTML();
62
    }
63
64
65
66
    /**
67
     * @param FieldInterface $field
68
     * @return DOMElement
69
     */
70 3
    private function createFieldDOM(FieldInterface $field)
71
    {
72 3
        $formGroup = $this->dom->createElement('div');
73 3
        $formGroup->setAttribute('class', 'form-group');
74
75 3
        $label = $this->dom->createElement('label');
76 3
        $label->setAttribute('for', $field->getId());
77 3
        $label->textContent = $field->getLabel();
78
79 3
        $formField = $this->createChildElement($field);
80
81 3
        $formGroup->appendChild($label);
82 3
        $formGroup->appendChild($formField);
83
84 3
        if (!$field->isValid() && $this->displayErrors == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
85 1
            $formGroup = $this->createHelpBlock($formGroup, $field->getMessages());
86 1
        }
87
88 3
        return $formGroup;
89
    }
90
91 1
    private function createHelpBlock(DOMElement $formGroup, array $messages)
92
    {
93 1
        $formGroup->setAttribute('class', 'form-group has-error');
94 1
        $helpBlock = $this->dom->createElement('span');
95 1
        $helpBlock->setAttribute('class', 'help-block');
96 1
        $errorMessages = '';
97 1
        foreach ($messages as $message) {
98 1
            if(is_array($message)) {
99 1
                foreach ($message as $m) {
100 1
                    $errorMessages .= $m."\n";
101 1
                }
102 1
            } else {
103
                $errorMessages .= $message."\n";
104
            }
105 1
        }
106 1
        $helpBlock->textContent = $errorMessages;
107 1
        $formGroup->appendChild($helpBlock);
108 1
        return $formGroup;
109
    }
110
111
112
113
    /**
114
     * @param FieldInterface $field
115
     * @return DOMElement
116
     */
117 3
    private function createChildElement(FieldInterface $field)
118
    {
119 3
        $child = $this->dom->createElement($field->getTag());
120
121 3
        $child->setAttribute('type', $field->getTagType());
122 3
        $child->setAttribute('name', $field->getName());
123 3
        $child->setAttribute('id', $field->getId());
124 3
        $child->setAttribute('value', $field->getValue());
125 3
        $child->setAttribute('class', $field->getClass());
126
127 3
        return $child;
128
    }
129
}