Passed
Push — master ( bc0996...c35245 )
by Bruno
05:47
created

Renderable_number   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 53
ccs 0
cts 37
cp 0
rs 10
wmc 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C editable() 0 47 10
1
<?php
2
3
namespace Formularium\Frontend\HTML\Renderable;
4
5
use Formularium\Datatype;
6
use Formularium\Field;
7
use Formularium\Frontend\HTML\Framework;
8
use Formularium\HTMLElement;
9
10
class Renderable_number extends \Formularium\Renderable implements \Formularium\Frontend\HTML\RenderableInterface
11
{
12
    public const STEP = 'step';
13
14
    use \Formularium\Frontend\HTML\RenderableViewableTrait;
15
16
    public function editable($value, Field $f, HTMLElement $previous): HTMLElement
17
    {
18
        $input = new HTMLElement('input');
19
        /** @var \Formularium\Datatype\Datatype_number $datatype */
20
        $datatype = $f->getDatatype();
21
    
22
        $extensions = $f->getExtensions();
23
        $validators = $f->getValidators();
24
        $input->setAttributes([
25
            'id' => $f->getName() . Framework::counter(),
26
            'type' => ($extensions[static::HIDDEN] ?? false ? 'hidden' : 'number'),
27
            'name' => $f->getName(),
28
            'class' => '',
29
            'data-attribute' => $f->getName(),
30
            'data-datatype' => $datatype->getName(),
31
            'data-basetype' => $datatype->getBasetype(),
32
            'value' => $value,
33
            'title' => $f->getExtension(static::LABEL, '')
34
        ]);
35
36
        if (isset($extensions[static::PLACEHOLDER])) {
37
            $input->setAttribute('placeholder', $extensions[static::PLACEHOLDER]);
38
        }
39
        if ($validators[Datatype::REQUIRED] ?? false) {
40
            $input->setAttribute('required', 'required');
41
        }
42
        foreach ([static::DISABLED, static::READONLY] as $v) {
43
            if ($f->getExtension($v, false)) {
44
                $input->setAttribute($v, $v);
45
            }
46
        }
47
    
48
        if (array_key_exists(static::STEP, $validators)) {
49
            $input->setAttribute('step', $validators[static::STEP]);
50
        }
51
        if (isset($extensions[static::NO_AUTOCOMPLETE])) {
52
            $input->setAttribute('autocomplete', 'off');
53
        }
54
    
55
        $container = new HTMLElement(Framework::getEditableContainerTag(), [], $input);
56
        if (array_key_exists('label', $extensions)) {
57
            $container->prependContent(new HTMLElement('label', ['for' => $input->getAttribute('id')], $extensions['label']));
58
        }
59
        if (array_key_exists('comment', $extensions)) {
60
            $container->appendContent(new HTMLElement('div', ['class' => 'comment'], $extensions['comment']));
61
        }
62
        return $container;
63
    }
64
}
65