Completed
Push — master ( 30f23d...c3cbe9 )
by Derek Stephen
05:45
created

SelectRender::renderBlock()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

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