Passed
Push — master ( 5a8560...7d651d )
by Bruno
05:41
created

Renderable_choice   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A viewable() 0 3 1
A _editable() 0 47 5
1
<?php
2
3
namespace Formularium\Frontend\Materialize\Renderable;
4
5
use Formularium\Field;
6
use Formularium\HTMLElement;
7
use Formularium\Frontend\HTML\Renderable\Renderable_choice as HTMLRenderable_choice;
8
use Formularium\Frontend\Materialize\RenderableMaterializeTrait;
9
10
class Renderable_choice extends \Formularium\Renderable
11
{
12
    use RenderableMaterializeTrait;
13
    
14
    public function viewable($value, Field $field, HTMLElement $previous): HTMLElement
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 HTMLElement $previous
25
     * @return HTMLElement
26
     */
27
    public function _editable($value, Field $field, HTMLElement $previous): HTMLElement
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, 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...
28
    {
29
        $newContent = [];
30
        $format = $field->getExtension(HTMLRenderable_choice::FORMAT_CHOOSER, HTMLRenderable_choice::FORMAT_CHOOSER_SELECT);
31
32
        if ($format == HTMLRenderable_choice::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[] = HTMLElement::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 = HTMLElement::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