Passed
Push — master ( f360f0...1bc594 )
by Bruno
06:10
created

Renderable_associationSelect::editable()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 5
eloc 20
c 1
b 1
f 0
nc 12
nop 3
dl 0
loc 33
rs 9.2888
1
<?php declare(strict_types=1);
2
3
namespace Formularium\Frontend\HTML\Renderable;
4
5
use Formularium\Datatype;
6
use Formularium\Datatype\Datatype_association;
7
use Formularium\Field;
8
use Formularium\Frontend\HTML\Framework;
9
use Formularium\HTMLElement;
10
11
class Renderable_associationSelect extends Renderable_association
12
{
13
    public function viewable($value, Field $field, HTMLElement $previous): HTMLElement
14
    {
15
        return $previous;
16
    }
17
18
    public function editable($value, Field $field, HTMLElement $previous): HTMLElement
19
    {
20
        $input = new HTMLElement('select');
21
    
22
        $extensions = $field->getExtensions();
23
        $validators = $field->getValidators();
24
        $input->setAttributes([
25
                'id' => $field->getName() . Framework::counter(),
26
                'name' => $field->getName(),
27
                'class' => '',
28
                'data-attribute' => $field->getName(),
29
                'data-datatype' => $field->getDatatype()->getName(),
30
                'data-basetype' => $field->getDatatype()->getBasetype(),
31
                'title' => $field->getExtension(static::LABEL, ''),
32
                'autocomplete' => 'off'
33
            ]);
34
    
35
        if (isset($extensions[static::PLACEHOLDER])) {
36
            $input->setAttribute('placeholder', $extensions[static::PLACEHOLDER]);
37
        }
38
        if ($validators[Datatype::REQUIRED] ?? false) {
39
            $input->setAttribute('required', 'required');
40
        }
41
        /* TODO if ($validators[Datatype_association::MULTIPLE] ?? false) {
42
            $input->setAttribute('multiple', 'multiple');
43
        } */
44
        foreach ([static::DISABLED, static::READONLY] as $v) {
45
            if ($field->getExtension($v, false)) {
46
                $input->setAttribute($v, $v);
47
            }
48
        }
49
    
50
        return $this->container($input, $field);
51
    }
52
}
53