Passed
Push — master ( 31045a...52c533 )
by Bruno
06:54
created

Renderable_bool::editable()   A

Complexity

Conditions 6
Paths 12

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 16
c 1
b 0
f 0
nc 12
nop 3
dl 0
loc 25
ccs 0
cts 22
cp 0
crap 42
rs 9.1111
1
<?php
2
3
namespace Formularium\Frontend\HTML\Renderable;
4
5
use Formularium\Datatype;
6
use Formularium\Field;
7
use Formularium\Frontend\HTML\Framework;
8
use Formularium\Frontend\HTML\Renderable;
9
use Formularium\HTMLElement;
10
11
class Renderable_bool extends Renderable
12
{
13
    public const FORMAT_CHOOSER = 'format_chooser';
14
    public const FORMAT_CHOOSER_SELECT = 'format_chooser_select';
15
    public const FORMAT_CHOOSER_RADIO = 'format_chooser_radio';
16
17
    use \Formularium\Frontend\HTML\RenderableViewableTrait {
18
        viewable as _viewable;
19
    }
20
    
21
    public function viewable($value, Field $field, HTMLElement $previous): HTMLElement
22
    {
23
        $formatted = $field->getDatatype()->format($value, $field);
24
25
        return $this->_viewable($formatted, $field, $previous);
26
    }
27
28
    public function editable($value, Field $field, HTMLElement $previous): HTMLElement
29
    {
30
        if (is_string($value)) {
31
            if ($value === 'true') {
32
                $value = true;
33
            } elseif ($value === 'false') {
34
                $value = false;
35
            } else {
36
                $value = null;
37
            }
38
        }
39
40
        $format = $field->getExtension(static::FORMAT_CHOOSER, static::FORMAT_CHOOSER_SELECT);
41
        
42
        if ($field->getValidators()[Datatype::REQUIRED] ?? false) {
43
            if ($format == static::FORMAT_CHOOSER_SELECT) {
44
                $element = $this->editableSelect($value, $field, $previous);
45
            } else {
46
                $element = $this->editableRadio($value, $field, $previous);
47
            }
48
        } else {
49
            $element = $this->editableSelect($value, $field, $previous);
50
        }
51
52
        return $this->container($element, $field);
53
    }
54
55
    /**
56
     * @param mixed $value
57
     * @param Field $field
58
     * @param HTMLElement $previous
59
     * @return HTMLElement
60
     */
61
    protected function editableRadio($value, Field $field, HTMLElement $previous): HTMLElement
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

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

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...
62
    {
63
        if (empty($value) && array_key_exists(static::DEFAULTVALUE, $field->getExtensions())) {
64
            $value = $field->getExtensions()[static::DEFAULTVALUE];
65
        }
66
67
        $element = new HTMLElement('ul');
68
69
        $idcounter = 1;
70
        foreach ([true => 'True', false => 'False'] as $v => $label) {
71
            $input = new HTMLElement('input');
72
73
            // send ids to delete/edit data later correctly.
74
            if ($value !== null && $v == $value) {
75
                $input->addAttribute('checked', 'checked');
76
            }
77
            $elementname = $field->getName(); // 'loh:' . $this->name . '[' . $attrid . '][value]' . '[' . $attrid . ']';
78
            $id = $elementname . $idcounter++;
79
            $input->addAttributes([
80
                'id' => $field->getName() . Framework::counter(),
81
                'name' => $elementname,
82
                'data-attribute' => $field->getName(),
83
                'data-datatype' => $field->getDatatype()->getName(),
84
                'data-basetype' => $field->getDatatype()->getBasetype(),
85
                'value' => $value ? 'true' : 'false',
86
                'type' => 'radio',
87
                'title' => $field->getExtension(static::LABEL, '')
88
            ]);
89
90
            if ($field->getValidators()[Datatype::REQUIRED] ?? false) {
91
                $element->setAttribute('required', 'required');
92
            }
93
            foreach ([static::DISABLED, static::READONLY] as $p) {
94
                if ($field->getExtension($p, false)) {
95
                    $input->setAttribute($p, $p);
96
                }
97
            }
98
    
99
            $li = new HTMLElement(
100
                'li',
101
                [],
102
                [$input, new HTMLElement('label', ['for' => $id], [$label])]
103
            );
104
            $element->addContent($li);
105
        }
106
        return $element;
107
    }
108
109
    /**
110
     * @param mixed $value
111
     * @param Field $field
112
     * @param HTMLElement $previous
113
     * @return HTMLElement
114
     */
115
    protected function editableSelect($value, Field $field, HTMLElement $previous): HTMLElement
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

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

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...
116
    {
117
        $element = new HTMLElement('select');
118
        $element->setAttributes([
119
            'id' => $field->getName() . Framework::counter(),
120
            'name' => $field->getName(),
121
            'class' => '',
122
            'data-attribute' => $field->getName(),
123
            'data-datatype' => $field->getDatatype()->getName(),
124
            'data-basetype' => $field->getDatatype()->getBasetype(),
125
            'title' => $field->getExtension(static::LABEL, '')
126
        ]);
127
128
        $optionEmpty = new HTMLElement('option', ['value' => ''], '', true);
129
        $optionTrue = new HTMLElement('option', ['value' => 'true'], 'True', true);
130
        $optionFalse = new HTMLElement('option', ['value' => 'false'], 'False', true);
131
132
        if ($value) {
133
            $optionTrue->setAttribute('selected', 'selected');
134
        } elseif (!($value === null)) {
135
            $optionFalse->setAttribute('selected', 'selected');
136
        }
137
138
        if ($field->getValidators()[Datatype::REQUIRED] ?? false) {
139
            $element->addContent($optionEmpty);
140
        }
141
        $element->addContent($optionFalse);
142
        $element->addContent($optionTrue);
143
144
        foreach ([static::DISABLED, static::READONLY] as $v) {
145
            if ($field->getExtension($v, false)) {
146
                $element->setAttribute($v, $v);
147
            }
148
        }
149
150
        return $element;
151
    }
152
}
153