Test Failed
Push — master ( e97986...2933e0 )
by Bruno
19:41 queued 09:43
created

RenderableVuetifyInputTrait::_editable()   B

Complexity

Conditions 8
Paths 1

Size

Total Lines 43
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 43
rs 8.4444
cc 8
nc 1
nop 3
1
<?php declare(strict_types=1);
2
3
namespace Formularium\Frontend\Vuetify;
4
5
use Formularium\Field;
6
use Formularium\Frontend\HTML\Renderable;
7
use Formularium\HTMLNode;
8
9
trait RenderableVuetifyInputTrait
10
{
11
    use RenderableVuetifyTrait;
12
13
    public function viewable($value, Field $field, HTMLNode $previous): HTMLNode
0 ignored issues
show
Unused Code introduced by
The parameter $value 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

13
    public function viewable(/** @scrutinizer ignore-unused */ $value, Field $field, 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...
Unused Code introduced by
The parameter $field 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

13
    public function viewable($value, /** @scrutinizer ignore-unused */ Field $field, 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...
14
    {
15
        return $previous;
16
    }
17
18
    /**
19
     * Subcall of wrapper editable() from RenderableMaterializeTrait
20
     *
21
     * @param mixed $value
22
     * @param Field $field
23
     * @param HTMLNode $previous
24
     * @return HTMLNode
25
     */
26
    public function _editable($value, Field $field, HTMLNode $previous): HTMLNode
0 ignored issues
show
Unused Code introduced by
The parameter $value 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

26
    public function _editable(/** @scrutinizer ignore-unused */ $value, Field $field, 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...
27
    {
28
        // add extra classes
29
        $previous->walk(
30
            function ($e) use ($field) {
31
                if ($e instanceof HTMLNode) {
32
                    if ($e->getTag() === 'input') {
33
                        if (($e->getAttribute('type')[0] ?? '') === 'radio') {
34
                            $e->setTag('v-radio')
35
                                ->setAttribute('label', $e->getAttribute('title'));
36
                        } else {
37
                            $e->setTag('v-text-field');
38
                            $this->setBaseAttributes($e, $field);
39
                        }
40
                    } elseif ($e->getTag() === 'select') {
41
                        $this->processSelect($e, $field);
42
                    } elseif ($e->getTag() === 'textarea') {
43
                        $e->setTag('v-textarea');
44
                        $this->setBaseAttributes($e, $field);
45
                    } else {
46
                        return;
47
                    }
48
49
                    $size = $field->getRenderable(Renderable::SIZE, '');
50
                    switch ($size) {
51
                        case Renderable::SIZE_LARGE:
52
                            $e->addAttribute('size', 'large');
53
                            break;
54
                        case Renderable::SIZE_SMALL:
55
                            $e->addAttribute('size', 'small');
56
                            break;
57
                    }
58
59
                    /* TODO
60
                    $icon = $field->getRenderable(Renderable::ICON, '');
61
                    if ($icon) {
62
                        $e->append(new HTMLNode('v-icon', str_replace('v-', '', $icon)));
63
                    }
64
                    */
65
                }
66
            }
67
        );
68
        return $previous;
69
    }
70
71
    protected function setBaseAttributes(HTMLNode $e, Field $field)
72
    {
73
        $renderable = $field->getRenderables();
74
75
        if (array_key_exists(Renderable::LABEL, $renderable)) {
76
            $e->setAttribute('label', $renderable[Renderable::LABEL]);
77
        }
78
        if (array_key_exists(Renderable::COMMENT, $renderable)) {
79
            $e->setAttribute(
80
                'messages',
81
                $renderable[Renderable::COMMENT]
82
            );
83
        }
84
    }
85
86
    protected function processSelect(HTMLNode $select, Field $field)
87
    {
88
        $select->setTag('v-select');
89
90
        $options = [];
91
        $select->walk(
92
            function (HTMLNode $e) use (&$options) {
93
                if ($e->getTag() == 'option') {
94
                    $idx = $e->getAttribute('value')[0] ?? '';
95
                    $text = $e->getContent()[0] ?? '';
96
                    $options[] = ['value' => $idx, 'text' => $text];
97
                }
98
            }
99
        );
100
        $select->clearContent();
101
        $select->addAttribute(':items', json_encode($options));
102
        $select->addAttribute('item-value', "value");
103
        $select->addAttribute('item-text', "text");
104
        $this->setBaseAttributes($select, $field);
105
    }
106
}
107