DefaultErrorRender::addErrorMessages()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Del\Form\Renderer\Error;
6
7
8
use Del\Form\Field\FieldInterface;
9
use DOMElement;
10
11
class DefaultErrorRender extends AbstractErrorRender
12
{
13 3
    public function render(FieldInterface $field): DOMElement
14
    {
15 3
        $helpBlock = $this->getDom()->createElement('span');
16 3
        $helpBlock->setAttribute('class', 'text-danger');
17
18 3
        if ($this->shouldRender($field)) {
19 3
            $helpBlock = $field->hasCustomErrorMessage()
20 1
                ? $this->addCustomErrorMessage($helpBlock, $field)
21 2
                : $this->addErrorMessages($helpBlock, $field);
22
        }
23 3
        return $helpBlock;
24
    }
25
26 1
    private function addCustomErrorMessage(DOMElement $helpBlock, FieldInterface $field): DOMElement
27
    {
28 1
        $message = $field->getCustomErrorMessage();
29 1
        $text = $this->createText($message);
30 1
        $helpBlock->appendChild($text);
31 1
        return $helpBlock;
32
    }
33
34 2
    private function addErrorMessages(DOMElement $helpBlock, FieldInterface $field): DOMElement
35
    {
36 2
        $messages = $field->getMessages();
37
38 2
        foreach ($messages as $message) {
39 2
            $helpBlock = $this->appendMessage($helpBlock, $message);
40
        }
41 2
        return $helpBlock;
42
    }
43
44 2
    private function appendMessage(DOMElement $helpBlock, $message): DOMElement
45
    {
46 2
        $text = $this->createText($message);
47 2
        $br = $this->createLineBreak();
48 2
        $helpBlock->appendChild($text);
49 2
        $helpBlock->appendChild($br);
50 2
        return $helpBlock;
51
    }
52
}
53