FormElement::Attributes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: sjhc1170
5
 * Date: 07/05/2018
6
 * Time: 09:36
7
 */
8
9
namespace Iriven\Plugins\Form\Core;
10
11
use \Iriven\Plugins\Form\Core\Libs\Collection;
0 ignored issues
show
Bug introduced by
The type \Iriven\Plugins\Form\Core\Libs\Collection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use \Iriven\Plugins\Form\Core\Interfaces\FormElementInterface;
0 ignored issues
show
Bug introduced by
The type \Iriven\Plugins\Form\Cor...es\FormElementInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use \Iriven\Plugins\Form\Core\Libs\AttributesBuilder;
0 ignored issues
show
Bug introduced by
The type \Iriven\Plugins\Form\Core\Libs\AttributesBuilder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use \Iriven\Plugins\Form\Core\Libs\Traits\KeyNormalizer;
0 ignored issues
show
Bug introduced by
The type \Iriven\Plugins\Form\Cor...bs\Traits\KeyNormalizer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use \Iriven\Plugins\Form\Core\Libs\LabelGenerator;
0 ignored issues
show
Bug introduced by
The type \Iriven\Plugins\Form\Core\Libs\LabelGenerator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
17
class FormElement implements FormElementInterface
18
{
19
    use KeyNormalizer;
20
    private   $label;
21
    protected $attributes;
22
    private   $Types;
23
24
    /**
25
     * FormElement constructor.
26
     * @param LabelGenerator|string $label
27
     * @param AttributesBuilder|array $attributes
28
     */
29
    public function __construct($label, $attributes=[])
30
    {
31
        if(!$label instanceof LabelGenerator)
32
            $label  = new LabelGenerator($label,[]);
33
        $this->label = $label;
34
35
        if(!$attributes instanceof AttributesBuilder)
36
            $attributes  = new AttributesBuilder($attributes);
37
        $this->attributes = $attributes;
38
39
        if(!$this->attributes->has('name'))
40
            $this->attributes->set('name',$this->normalize($this->label->getItem()));
41
42
        if(!$this->attributes->has('type'))
43
            $this->attributes->set('type','text');
44
        if (!$this->attributes->has('id'))
45
            $this->attributes->createElementID($this->attributes->get('name', $this->label->getItemID()));
46
47
        $this->attributes->set('autocomplete','off');
48
49
        $this->Types = new Collection(array_flip([
50
            'button',
51
            'checkbox',
52
            'color',
53
            'date',
54
            'datetime',
55
            'datetime-local',
56
            'email',
57
            'file',
58
            'hidden',
59
            'image',
60
            'month',
61
            'number',
62
            'password',
63
            'radio',
64
            'range',
65
            'reset',
66
            'search',
67
            'submit',
68
            'tel',
69
            'text',
70
            'time',
71
            'url',
72
            'week']));
73
    }
74
75
    /**
76
     * @return LabelGenerator|string
77
     */
78
    public function Label()
79
    {
80
        return $this->label;
81
    }
82
83
    /**
84
     * @return Collection
85
     */
86
    protected function Types()
87
    {
88
        return $this->Types;
89
    }
90
    /**
91
     * @return AttributesBuilder
92
     */
93
    public function Attributes()
94
    {
95
        return $this->attributes;
96
    }
97
98
    /**
99
     * @return string
100
     */
101
    public function RenderHtml()
102
    {
103
        $html = '';
104
        if($this->Types->has($this->attributes->get('type')))
105
        {
106
            $this->label->Attribute()->set('fieldtype',$this->attributes->get('type'));
107
            $this->label->Attribute()->set('for',$this->attributes->get('id'));
108
109
            $html .= $this->Label()->RenderHtml();
110
            $html .= '<input';
111
            $html .= $this->attributes->RenderHtml();
112
            $html .= ' >';
113
        }
114
        return $html;
115
    }
116
117
}
118