RenderableBulmaTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 46
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A editable() 0 34 3
1
<?php declare(strict_types=1);
2
3
namespace Formularium\Frontend\Bulma;
4
5
use Formularium\Field;
6
use Formularium\HTMLNode;
7
8
trait RenderableBulmaTrait
9
{
10
    /**
11
     * Subcall of wrapper editable() from RenderableMaterializeTrait
12
     *
13
     * @param mixed $value
14
     * @param Field $field
15
     * @param HTMLNode $previous
16
     * @return HTMLNode
17
     */
18
    abstract public function _editable($value, Field $field, HTMLNode $previous): HTMLNode;
19
20
    public function editable($value, Field $field, HTMLNode $previous): HTMLNode
21
    {
22
        /** @var HTMLNode $base */
23
        $base = $this->_editable($value, $field, $previous);
24
        $base->addAttribute('class', "control");
25
26
        // add a div.field container
27
        $container = HTMLNode::factory(
28
            'div',
29
            [
30
                'class' => "field",
31
                'data-attribute' => $field->getName(),
32
                'data-datatype' => $field->getDatatype()->getName(),
33
                'data-basetype' => $field->getDatatype()->getBasetype()
34
            ],
35
            $base
36
        );
37
38
        // move the main label to the div.field container
39
        $label = $base->get('label.formularium-label');
40
        if (!empty($label)) {
41
            // delete
42
            $base->filter(function ($e) {
43
                return !($e->getTag() === 'label' && $e->getAttribute('class') === ['formularium-label']);
44
            });
45
            // fix class
46
            $label[0]->addAttributes([
47
                'class' => 'label',
48
            ]);
49
            // prepend
50
            $container->prependContent($label[0]);
51
        }
52
53
        return $container;
54
    }
55
}
56