Passed
Push — master ( 439363...848c74 )
by Bruno
03:23
created

Renderable_uuid   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 11
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 11
rs 10
wmc 1
1
<?php
2
3
namespace Formularium\Frontend\HTML\Renderable;
4
5
use Formularium\Field;
6
use Formularium\HTMLElement;
7
8
class Renderable_number extends \Formularium\Renderable implements \Formularium\Frontend\HTML\RenderableInterface
9
{
10
    public const STEP = 'step';
11
    public const MIN = "min";
12
    public const MAX = "max";
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
            'type' => ($extensions[static::HIDDEN] ?? false ? 'hidden' : 'number'),
26
            'name' => $f->getName(),
27
            'class' => '',
28
            'data-attribute' => $f->getName(),
29
            'data-datatype' => $datatype->getName(),
30
            'data-basetype' => $datatype->getBasetype(),
31
            'value' => $value,
32
            'title' => $f->getExtension(static::LABEL, '')
33
        ]);
34
35
        if (isset($extensions[static::PLACEHOLDER])) {
36
            $input->setAttribute('placeholder', $extensions[static::PLACEHOLDER]);
37
        }
38
        foreach ([static::DISABLED, static::READONLY, static::REQUIRED] as $v) {
39
            if ($f->getExtension($v, false)) {
40
                $input->setAttribute($v, $v);
41
            }
42
        }
43
    
44
        if (array_key_exists(static::STEP, $validators)) {
45
            $input->setAttribute('step', $validators[static::STEP]);
46
        }
47
        if (isset($extensions[static::NO_AUTOCOMPLETE])) {
48
            $input->setAttribute('autocomplete', 'off');
49
        }
50
    
51
        $container = new HTMLElement('div', [], $input);
52
        if (array_key_exists('label', $extensions)) {
53
            $container->prependContent(new HTMLElement('label', [], $extensions['label']));
54
        }
55
        return $container;
56
    }
57
}
58