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

MultiSelectRender::renderBlock()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 10
cc 3
nc 3
nop 2
crap 3
1
<?php declare(strict_types=1);
2
3
namespace Del\Form\Renderer\Field;
4
5
use Del\Form\Field\FieldInterface;
6
use Del\Form\Field\MultiSelect;
7
use DOMElement;
8
use InvalidArgumentException;
9
10
class MultiSelectRender extends AbstractFieldRender
11
{
12 5
    public function renderBlock(FieldInterface $field, DOMElement $element): DOMElement
13
    {
14 5
        if (!$field instanceof MultiSelect) {
15 1
            throw new InvalidArgumentException('Must be a Del\Form\Field\MultiSelect');
16
        }
17
18 4
        $element->setAttribute('name', $field->getName() . '[]');
19
20 4
        foreach ($field->getOptions() as $value => $label) {
21 3
            $option = $this->processOption($field, $value, $label);
22 3
            $element->appendChild($option);
23
        }
24
25 4
        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() !== null && in_array($option->getAttribute('value'), $field->getValue())) {
36 1
            $option->setAttribute('selected', 'selected');
37
        }
38
39 3
        return $option;
40
    }
41
}
42