Passed
Push — master ( bf4e5d...29b18f )
by Bruno
06:18
created

Renderable_enum::editableSelect()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 40
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 40
rs 8.9297
c 0
b 0
f 0
cc 6
nc 18
nop 3
1
<?php declare(strict_types=1);
2
3
namespace Formularium\Frontend\HTML\Renderable;
4
5
use Formularium\Datatype;
6
use Formularium\Datatype\Datatype_enum;
7
use Formularium\Field;
8
use Formularium\Frontend\HTML\Framework;
9
use Formularium\Frontend\HTML\Renderable;
10
use Formularium\HTMLNode;
11
12
class Renderable_enum extends Renderable
13
{
14
    public const FORMAT_CHOOSER = 'format_chooser';
15
    public const FORMAT_CHOOSER_SELECT = 'format_chooser_select';
16
    public const FORMAT_CHOOSER_RADIO = 'format_chooser_radio';
17
    public const LAYOUT_RADIO = 'inline';
18
    public const LAYOUT_RADIO_INLINE = 'inline';
19
20
    /**
21
     * Default value for format chooset
22
     *
23
     * @var string
24
     */
25
    protected $format_chooser_default = self::FORMAT_CHOOSER_SELECT;
26
27
    use \Formularium\Frontend\HTML\RenderableViewableTrait {
28
        viewable as _viewable;
29
    }
30
    
31
    public function viewable($value, Field $field, HTMLNode $previous): HTMLNode
32
    {
33
        $formatted = $field->getDatatype()->format($value);
34
35
        return $this->_viewable($formatted, $field, $previous);
36
    }
37
38
    public function editable($value, Field $field, HTMLNode $previous): HTMLNode
39
    {
40
        $format = $field->getRenderable(static::FORMAT_CHOOSER, $this->format_chooser_default);
41
        
42
        if ($format == static::FORMAT_CHOOSER_SELECT) {
43
            $element = $this->editableSelect($value, $field, $previous);
44
        } else {
45
            $element = $this->editableRadio($value, $field, $previous);
46
        }
47
48
        return $this->container($element, $field);
49
    }
50
51
    /**
52
     * @param mixed $value
53
     * @param Field $field
54
     * @param HTMLNode $previous
55
     * @return HTMLNode
56
     */
57
    protected function editableRadio($value, Field $field, HTMLNode $previous): HTMLNode
0 ignored issues
show
Unused Code introduced by
The parameter $previous is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

57
    protected function editableRadio($value, Field $field, /** @scrutinizer ignore-unused */ HTMLNode $previous): HTMLNode

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
58
    {
59
        if (empty($value) && array_key_exists(static::DEFAULTVALUE, $field->getRenderables())) {
60
            $value = $field->getRenderables()[static::DEFAULTVALUE];
61
        }
62
63
        $element = new HTMLNode($this->framework->getEditableContainerTag(), ['class' => 'formularium-radio-group']);
64
65
        /**
66
         * @var Datatype_enum $datatype
67
         */
68
        $datatype = $field->getDatatype();
69
        foreach ($datatype->getChoices() as $v => $label) {
70
            $input = new HTMLNode('input');
71
72
            // send ids to delete/edit data later correctly.
73
            if ($value !== null && $v == $value) {
74
                $input->addAttribute('checked', 'checked');
75
            }
76
            $elementname = $field->getName(); // 'loh:' . $this->name . '[' . $attrid . '][value]' . '[' . $attrid . ']';
77
            $id = $field->getName() . Framework::counter();
78
            $input->addAttributes([
79
                'id' => $id,
80
                'name' => $elementname,
81
                'data-attribute' => $field->getName(),
82
                'data-datatype' => $datatype->getName(),
83
                'data-basetype' => $datatype->getBasetype(),
84
                'value' => $value,
85
                'type' => 'radio',
86
                'title' => $label
87
            ]);
88
89
            if ($field->getValidators()[Datatype::REQUIRED] ?? false) {
90
                $element->setAttribute('required', 'required');
91
            }
92
            foreach ([static::DISABLED, static::READONLY] as $p) {
93
                if ($field->getRenderable($p, false)) {
94
                    $input->setAttribute($p, $p);
95
                }
96
            }
97
    
98
            $li = new HTMLNode(
99
                'div',
100
                [
101
                    'class' => 'formularium-radio-item'
102
                ],
103
                [
104
                    $input,
105
                    new HTMLNode('label', ['class' => 'formularium-radio-label', 'for' => $id], [HTMLNode::factory('span', [], $label)])
106
                ]
107
            );
108
            $element->addContent($li);
109
        }
110
111
        return $element;
112
    }
113
114
    /**
115
     * @param mixed $value
116
     * @param Field $field
117
     * @param HTMLNode $previous
118
     * @return HTMLNode
119
     */
120
    protected function editableSelect($value, Field $field, HTMLNode $previous): HTMLNode
0 ignored issues
show
Unused Code introduced by
The parameter $previous is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

120
    protected function editableSelect($value, Field $field, /** @scrutinizer ignore-unused */ HTMLNode $previous): HTMLNode

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
121
    {
122
        $element = new HTMLNode('select');
123
        $element->setAttributes([
124
            'id' => $field->getName() . Framework::counter(),
125
            'name' => $field->getName(),
126
            'class' => '',
127
            'data-attribute' => $field->getName(),
128
            'data-datatype' => $field->getDatatype()->getName(),
129
            'data-basetype' => $field->getDatatype()->getBasetype(),
130
            'title' => $field->getRenderable(static::LABEL, '')
131
        ]);
132
133
        $optionEmpty = new HTMLNode('option', ['value' => ''], '', true);
0 ignored issues
show
Unused Code introduced by
The assignment to $optionEmpty is dead and can be removed.
Loading history...
134
        /**
135
         * @var Datatype_enum $datatype
136
         */
137
        $datatype = $field->getDatatype();
138
139
        if ($field->getValidators()[Datatype::REQUIRED] ?? false) {
140
            $optionEmpty = new HTMLNode('option', ['value' => ''], '', true);
141
            $element->addContent($optionEmpty);
142
        }
143
144
        foreach ($datatype->getChoices() as $v => $label) {
145
            $option = new HTMLNode('option', ['value' => $v], $label, true);
146
147
            if ($value == $v) {
148
                $option->setAttribute('selected', 'selected');
149
            }
150
            $element->addContent($option);
151
        }
152
153
        foreach ([static::DISABLED, static::READONLY] as $v) {
154
            if ($field->getRenderable($v, false)) {
155
                $element->setAttribute($v, $v);
156
            }
157
        }
158
159
        return $element;
160
    }
161
}
162