Radio::create()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 8.3786
c 0
b 0
f 0
cc 7
nc 7
nop 0
1
<?php
2
3
namespace Faulancer\Form\Type\Base;
4
5
use Faulancer\Form\Type\AbstractType;
6
7
/**
8
 * Class Radio
9
 *
10
 * @package Faulancer\Form\Type\Base
11
 * @author Florian Knapp <[email protected]>
12
 */
13
class Radio extends AbstractType
14
{
15
16
    protected $element = [];
17
18
    protected $inputType = 'input';
19
20
    /**
21
     * @return self
22
     */
23
    public function create()
24
    {
25
        parent::create();
26
27
        $result = [];
28
29
        foreach ($this->definition['options'] as $optionName => $valueDef) {
30
31
            $output = '<' . $this->inputType;
32
33
            $output .= ' value="' . $valueDef['value'] . '"';
34
35
            foreach ($this->definition['attributes'] as $attr => $val) {
36
                $output .= ' ' . $attr . '="' . $val . '" ';
37
            }
38
39
            $output .= ' id="' . $this->definition['attributes']['name'] . '_' . $valueDef['value'] . '"';
40
41
            if (!empty($this->getValue()) && $valueDef['value'] === $this->getValue()) {
42
                $output .= ' checked="checked"';
43
            } elseif (empty($this->getValue()) && $this->definition['default'] === $optionName) {
44
                $output .= ' checked="checked"';
45
            }
46
47
            $output .= '/>';
48
49
            $result[$optionName] = [
50
                'label' => $valueDef['label'],
51
                'field' => $output
52
            ];
53
54
        }
55
56
        $this->element = $result;
57
58
        return $this;
59
60
    }
61
62
    /**
63
     * @param string $optionName
64
     * @return string
65
     */
66
    public function getOption(string $optionName)
67
    {
68
        return $this->element[$optionName]['field'];
69
    }
70
71
    /**
72
     * @param string $optionName
73
     * @return string
74
     * @codeCoverageIgnore
75
     */
76
    public function getOptionLabel(string $optionName)
77
    {
78
        return '<label for="' . $this->definition['attributes']['name'] . '_' . $this->definition['options'][$optionName]['value'] . '">' . $this->element[$optionName]['label'] . '</label>';
79
    }
80
81
}