Passed
Push — master ( ff6e33...6f3a15 )
by Derek Stephen
02:10 queued 17s
created

SelectRender::processOption()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 7
c 3
b 0
f 0
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 10
cc 2
nc 2
nop 3
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Del\Form\Renderer\Field;
6
7
use Del\Form\Field\FieldInterface;
8
use Del\Form\Field\Select;
9
use DOMElement;
10
use InvalidArgumentException;
11
12
class SelectRender extends AbstractFieldRender
13
{
14 5
    public function renderBlock(FieldInterface $field, DOMElement $element): DOMElement
15
    {
16 5
        if (!$field instanceof Select) {
17 2
            throw new InvalidArgumentException('Must be a Del\Form\Field\Select');
18
        }
19
20 3
        foreach ($field->getOptions() as $value => $label) {
21 3
            $option = $this->processOption($field, $value, $label);
22 3
            $element->appendChild($option);
23
        }
24
25 3
        return $element;
26
    }
27
28 3
    private function processOption(FieldInterface $field, $value, $label): DOMElement
29
    {
30 3
        $option = $this->createElement('option');
31 3
        $option->setAttribute('value', (string) $value);
32 3
        $label = $this->createText($label);
33 3
        $option->appendChild($label);
34
35 3
        if ($field->getValue() == $option->getAttribute('value')) {
36 1
            $option->setAttribute('selected', 'selected');
37
        }
38
39 3
        return $option;
40
    }
41
}
42