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

Renderable_enum   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 35
dl 0
loc 64
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A viewable() 0 3 1
A _editable() 0 47 5
1
<?php declare(strict_types=1); 
2
3
namespace Formularium\Frontend\Materialize\Renderable;
4
5
use Formularium\Field;
6
use Formularium\HTMLNode;
7
use Formularium\Frontend\HTML\Renderable\Renderable_enum as HTMLRenderable_enum;
8
use Formularium\Frontend\Materialize\RenderableMaterializeTrait;
9
10
class Renderable_enum extends \Formularium\Renderable
11
{
12
    use RenderableMaterializeTrait;
13
    
14
    public function viewable($value, Field $field, HTMLNode $previous): HTMLNode
15
    {
16
        return $previous;
17
    }
18
19
    /**
20
     * Subcall of wrapper editable() from RenderableMaterializeTrait
21
     *
22
     * @param mixed $value
23
     * @param Field $field
24
     * @param HTMLNode $previous
25
     * @return HTMLNode
26
     */
27
    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

27
    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...
28
    {
29
        $newContent = [];
30
        $format = $field->getRenderable(HTMLRenderable_enum::FORMAT_CHOOSER, HTMLRenderable_enum::FORMAT_CHOOSER_SELECT);
31
32
        if ($format == HTMLRenderable_enum::FORMAT_CHOOSER_RADIO) {
33
            $labels = [];
34
            // add extra classes
35
            foreach ($previous->get('.formularium-radio-item') as $container) {
36
                $label = $container->get('label')[0];
37
                $input = $container->get('input')[0];
38
                $labels[] = HTMLNode::factory('p', [], $label->prependContent($input));
39
            }
40
            $previous->setContent($labels);
41
        } else {
42
            // add extra classes
43
            $input = $previous->get('select');
44
            $input[0]->removeAttribute('class');
45
            $id = $input[0]->getAttribute('id')[0];
46
            $newContent[] = $input[0];
47
            $label = $previous->get('label');
48
            if (!empty($label)) {
49
                $newContent[] = $label[0];
50
            }
51
            $comment = $previous->get('.formularium-comment');
52
            if (!empty($comment)) {
53
                $comment[0]->setTag('span')->setAttributes([
54
                    'class' => 'helper-text',
55
                    'data-error' => "wrong",
56
                    'data-success' => "right"
57
                ]);
58
                $newContent[] = $comment[0];
59
            }
60
            $script = HTMLNode::factory(
61
                'script',
62
                [],
63
                "document.addEventListener('DOMContentLoaded', function() {
64
                    var elems = document.querySelectorAll('#${id}');
65
                    var options = {};
66
                    var instances = M.FormSelect.init(elems, options);
67
                });",
68
                true
69
            );
70
            $newContent[] = $script;
71
            $previous->setContent($newContent);
72
        }
73
        return $previous;
74
    }
75
}
76