Passed
Push — master ( 4ada59...3080fc )
by Bruno
09:58
created

Renderable::getInput()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
eloc 8
c 1
b 1
f 0
nc 3
nop 1
dl 0
loc 15
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Formularium\Frontend\HTMLValidation;
4
5
use Formularium\Field;
6
use Formularium\HTMLNode;
7
use Formularium\Validator\MaxLength;
8
use Formularium\Validator\MinLength;
9
use Formularium\Validator\Regex;
10
11
class Renderable extends \Formularium\Renderable
12
{
13
    public function viewable($value, Field $field, HTMLNode $previous): HTMLNode
14
    {
15
        return $previous;
16
    }
17
18
    public function editable($value, Field $field, HTMLNode $previous): HTMLNode
19
    {
20
        $validators = $field->getValidators();
21
22
        $element = $this->getInput($previous);
23
        if (!$element) {
0 ignored issues
show
introduced by
$element is of type Formularium\HTMLNode, thus it always evaluated to true.
Loading history...
24
            return $previous;
25
        }
26
27
        $datatype = $field->getDatatype();
0 ignored issues
show
Unused Code introduced by
The assignment to $datatype is dead and can be removed.
Loading history...
28
29
        foreach ($validators as $validator => $data) {
30
            switch ($validator) {
31
            case MinLength::class:
32
                $element->setAttribute('minlength', $field->getValidatorOption($validator, 'value', ''));
33
                break;
34
            case MaxLength::class:
35
                $element->setAttribute('maxlength', $field->getValidatorOption($validator, 'value', ''));
36
                break;
37
            case Regex::class:
38
                $element->setAttribute('pattern', $field->getValidatorOption($validator, 'value', ''));
39
                break;
40
            default:
41
                break;
42
            }
43
        }
44
45
        return $previous;
46
    }
47
48
    protected function getInput(HTMLNode $previous): ?HTMLNode
49
    {
50
        /**
51
         * @var HTMLNode $element
52
         */
53
        $element = null;
54
        $input = $previous->get('input');
55
        if (count($input)) {
56
            return $input[0];
57
        }
58
        $input = $previous->get('textarea');
59
        if (count($input)) {
60
            $element = $input[0];
61
        }
62
        return $element;
63
    }
64
}
65