Passed
Push — master ( b78bc9...3bbd58 )
by Bruno
15:37
created

Renderable_file::viewable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 3
dl 0
loc 24
rs 9.7
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Formularium\Frontend\Buefy\Renderable;
4
5
use Formularium\Datatype;
6
use Formularium\Field;
7
use Formularium\Frontend\HTML\Renderable\Renderable_file as HTMLRenderable_file;
8
use Formularium\HTMLElement;
9
use Formularium\Renderable;
10
use Formularium\Validator\File;
11
12
class Renderable_file extends \Formularium\Renderable
13
{
14
    public function editable($value, Field $field, HTMLElement $previous): HTMLElement
15
    {
16
        $renderable = $field->getRenderables();
17
        $validators = $field->getValidators();
18
19
        $inputAtts = [
20
            'name' => $field->getName(),
21
            'v-model' => $field->getName(),
22
            'drag-drop' => ''
23
        ];
24
        if ($renderable[File::ACCEPT] ?? false) {
25
            if (is_array($renderable[File::ACCEPT])) {
26
                $accept = join(',', $renderable[File::ACCEPT]);
27
            } else {
28
                $accept = $renderable[File::ACCEPT];
29
            }
30
            $inputAtts['accept'] = htmlspecialchars($accept);
31
        }
32
        if ($validators[Datatype::REQUIRED] ?? false) {
33
            $inputAtts['required'] = 'required';
34
        }
35
        if ($validators[File::MAX_SIZE] ?? false) {
36
            $inputAtts['data-max-size'] = $validators[File::MAX_SIZE];
37
        }
38
        foreach ([static::DISABLED, static::READONLY] as $v) {
39
            if ($field->getRenderable($v, false)) {
40
                $inputAtts[$v] = true;
41
            }
42
        }
43
44
        $container = HTMLElement::factory(
45
            'b-field',
46
            [],
47
            HTMLElement::factory(
48
                'b-upload',
49
                $inputAtts,
50
                HTMLElement::factory(
51
                    'section',
52
                    ['class' => 'section'],
53
                    HTMLElement::factory(
54
                        'div',
55
                        ['class' => "content has-text-centered"],
56
                        [
57
                            HTMLElement::factory(
58
                                'p',
59
                                [],
60
                                HTMLElement::factory(
61
                                    'b-icon',
62
                                    [ 'icon' => "upload", 'size' => "is-large" ]
63
                                )
64
                            ),
65
                            HTMLElement::factory(
66
                                'p',
67
                                [ 'class' => 'formularium-label'],
68
                                $renderable[Renderable::LABEL] ?? ''
69
                            )
70
                        ]
71
                    )
72
                )
73
            )
74
        );
75
76
        return $container;
77
    }
78
79
    public function viewable($value, Field $field, HTMLElement $previous): HTMLElement
80
    {
81
        $tag = $field->getRenderable(Renderable::VIEWABLE_TAG, 'span');
82
        $atts = [
83
            'class' => 'formularium-viewable'
84
        ];
85
        $valueAtts = ['class' => 'formularium-value'];
86
87
        return HTMLElement::factory(
88
            'div',
89
            [],
90
            HTMLElement::factory(
91
                $tag,
92
                $atts,
93
                [
94
                    HTMLElement::factory(
95
                        'span',
96
                        ['class' => 'formularium-label'],
97
                        $field->getRenderable(\Formularium\Renderable::LABEL, '')
98
                    ),
99
                    HTMLElement::factory(
100
                        'span',
101
                        $valueAtts,
102
                        $value
103
                    )
104
                ]
105
            )
106
        );
107
    }
108
}
109