RenderableBootstrapWrapperTrait   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 47
c 3
b 0
f 0
dl 0
loc 88
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A wrapper() 0 9 1
B bootstrapify() 0 59 7
1
<?php declare(strict_types=1);
2
3
namespace Formularium\Frontend\Bootstrap;
4
5
use Formularium\Field;
6
use Formularium\HTMLNode;
7
use Formularium\Frontend\HTML\Renderable;
8
9
trait RenderableBootstrapWrapperTrait
10
{
11
    /**
12
     *
13
     *
14
     * @param mixed $value
15
     * @param Field $field
16
     * @param HTMLNode $previous
17
     * @return HTMLNode
18
     */
19
    public function wrapper($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

19
    public function wrapper(/** @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...
20
    {
21
        $previous->addAttributes([
22
            'class' => "form-group",
23
            'data-attribute' => $field->getName(),
24
            'data-datatype' => $field->getDatatype()->getName(),
25
            'data-basetype' => $field->getDatatype()->getBasetype()
26
        ]);
27
        return $previous;
28
    }
29
30
    /**
31
     *
32
     *
33
     * @param mixed $value
34
     * @param Field $field
35
     * @param HTMLNode $previous
36
     * @return HTMLNode
37
     */
38
    public function bootstrapify($value, Field $field, HTMLNode $previous, string $tag = 'input'): 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

38
    public function bootstrapify(/** @scrutinizer ignore-unused */ $value, Field $field, HTMLNode $previous, string $tag = 'input'): 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...
39
    {
40
        // add extra classes
41
        $input = $previous->get($tag);
42
        if (empty($input)) {
43
            return $previous;
44
        }
45
        $input = $input[0];
46
        $input->addAttributes([
47
            'class' => 'form-control',
48
        ]);
49
        $comment = $previous->get('.formularium-comment');
50
        if (!empty($comment)) {
51
            $comment[0]->setTag('small')->addAttributes([
52
                'class' => 'form-text text-muted',
53
            ]);
54
        }
55
        $size = $field->getRenderable(Renderable::SIZE, '');
56
        switch ($size) {
57
            case Renderable::SIZE_LARGE:
58
                $input->addAttribute('class', 'form-control-lg');
59
                break;
60
            case Renderable::SIZE_SMALL:
61
                $input->addAttribute('class', 'form-control-sm');
62
                break;
63
        }
64
65
        $icon = $field->getRenderable(Renderable::ICON, '');
66
        if ($icon) {
67
            $iconData = [];
68
            $iconPack = $field->getRenderable(Renderable::ICON_PACK, '');
69
            if ($iconPack) {
70
                $iconData[] = $iconPack;
71
            }
72
            $iconData[] = $icon;
73
            $group = HTMLNode::factory(
74
                'div',
75
                [ 'class' => "input-group mb-3" ],
76
                [
77
                    HTMLNode::factory(
78
                        'div',
79
                        [ 'class' => "input-group-prepend" ],
80
                        HTMLNode::factory(
81
                            'span',
82
                            [ 'class' => "input-group-text" ],
83
                            HTMLNode::factory(
84
                                'i',
85
                                [ 'class' => $iconData ],
86
                                []
87
                            )
88
                        )
89
                    ),
90
                    clone $input
91
                ]
92
            );
93
            $input->replace($group);
94
        }
95
96
        return $previous;
97
    }
98
}
99