AttributesMapper   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 34
eloc 106
c 1
b 0
f 0
dl 0
loc 125
rs 9.68

1 Method

Rating   Name   Duplication   Size   Complexity  
F filter() 0 99 34
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: sjhc1170
5
 * Date: 07/05/2018
6
 * Time: 14:20
7
 */
8
9
namespace Iriven\Plugins\Form\Core\Libs;
10
11
12
class AttributesMapper
13
{
14
    const COMMON = ['accesskey','class','contenteditable', 'contextmenu','dir','draggable','dropzone','hidden','id','lang','spellcheck','style','tabindex','title','translate'];
15
    const EVENTS = ['onblur','onchange','oncontextmenu','onfocus','oninput','oninvalid','onreset','onsearch','onselect','onsubmit','onkeydown','onkeypress','onkeyup','onclick','ondblclick','ondrag','ondragend','ondragenter','ondragleave','ondragover','ondragstart','ondrop','onmousedown','onmousemove','onmouseout','onmouseover','onmouseup','onmousewheel','onscroll','onwheel'];
16
    const BUTTON = ['autofocus','disabled','form','formaction','formenctype','formmethod','formnovalidate','formtarget','framename','name','type','value'];
17
    const CHECKBOX = ['autofocus','checked','defaultChecked','defaultvalue','disabled','form','indeterminate','name','required','type','value'];
18
    const COLOR = ['autocomplete','autofocus','defaultvalue','disabled','form','list','name','type','value'];
19
    const DATE = ['autocomplete','autofocus','defaultvalue','disabled','form','list','max','min','name','readonly','required','step','type','value'];
20
    const FIELDSET = ['disabled','form','name','legend','legend-attributes'];
21
    const FILE = ['accept','autofocus','defaultvalue','disabled','files','form','multiple','name','required','type','value', 'data-max-size'];
22
    const FORM = ['accept','accept-charset','action','autocomplete','enctype','method','name','novalidate','target'];
23
    const HIDDEN = ['form','name','type','value'];
24
    const IMAGE = ['alt','autofocus','defaultvalue','disabled','form','formaction','formenctype','formmethod','formnovalidate','formtarget','height','name','src','type','value','width'];
25
    const LABEL = ['for','form'];
26
    const OPTGROUP = ['disabled','label'];
27
    const OPTION = ['disabled','label','value','selected'];
28
    const RANGE = ['autocomplete','autofocus','defaultvalue','disabled','form','list','max','min','name','step','type','value'];
29
    const RESET = ['autofocus','defaultvalue','disabled','form','name','type','value'];
30
    const SELECT = ['autofocus','disabled','form','multiple','name','required','size','selected','optgroup-attributes','option-attributes','value'];
31
    const TEXT = ['autocomplete','autofocus','defaultvalue','disabled','form','list','minlength','maxlength','name','pattern','placeholder','readonly','required','size','type','value'];
32
    const TEXTAREA = ['autofocus','cols','dirname','disabled','form','maxlength','name','placeholder','readonly','required','rows','wrap','value'];
33
34
    /**
35
     * @param AttributesBuilder $Attributes
36
     * @return array
37
     */
38
    public static function filter(AttributesBuilder $Attributes)
39
    {
40
        $aAttributes = array_change_key_case($Attributes->All(),CASE_LOWER);
41
        $Type = $Attributes->get('type','text');
42
        $AcceptedAttributes = array_merge(self::EVENTS, self::COMMON);
43
        switch ($Type):
44
            case 'date':
45
            case 'datetime':
46
            case 'datetime-local':
47
            case 'month':
48
            case 'number':
49
            case 'time':
50
            case 'week':
51
                if($Type === 'number') $AcceptedAttributes[]='placeholder';
52
                $AcceptedAttributes = array_merge($AcceptedAttributes,self::DATE);
53
                break;
54
            case 'color':
55
                $AcceptedAttributes = array_merge($AcceptedAttributes, self::COLOR);
56
                break;
57
            case 'file':
58
                $AcceptedAttributes = array_merge($AcceptedAttributes,self::FILE);
59
                break;
60
            case 'image':
61
            case 'video':
62
                $AcceptedAttributes = array_merge($AcceptedAttributes,self::IMAGE);
63
                break;
64
            case 'radio':
65
            case 'checkbox':
66
                $tmpAttributesList=self::CHECKBOX;
67
                if($Type === 'radio')
68
                {
69
                    $bannedIndex = array_search('indeterminate', $tmpAttributesList,true);
70
                    if(isset($tmpAttributesList[$bannedIndex])) unset($tmpAttributesList[$bannedIndex]);
71
                }
72
                $AcceptedAttributes = array_merge($AcceptedAttributes,array_values($tmpAttributesList));
73
                break;
74
75
            case 'submit':
76
            case 'button':
77
                $AcceptedAttributes = array_merge($AcceptedAttributes, self::BUTTON);
78
                break;
79
80
            case 'reset':
81
                $AcceptedAttributes = array_merge($AcceptedAttributes,self::RESET);
82
                break;
83
            case 'range':
84
                $AcceptedAttributes = array_merge($AcceptedAttributes,self::RANGE);
85
                break;
86
            case 'textarea':
87
                $AcceptedAttributes = array_merge($AcceptedAttributes,self::TEXTAREA);
88
                break;
89
            case 'optgroup':
90
                $AcceptedAttributes = array_merge($AcceptedAttributes,self::OPTGROUP);
91
                break;
92
            case 'option':
93
                $AcceptedAttributes = array_merge($AcceptedAttributes,self::OPTION);
94
                break;
95
            case 'label':
96
                $AcceptedAttributes = array_merge($AcceptedAttributes,self::LABEL);
97
                break;
98
            case 'select':
99
                $AcceptedAttributes[]='placeholder';
100
                $AcceptedAttributes = array_merge($AcceptedAttributes,self::SELECT);
101
                break;
102
            case 'form':
103
                $AcceptedAttributes = array_merge($AcceptedAttributes,self::FORM);
104
                break;
105
            case 'fieldset':
106
                $AcceptedAttributes = array_merge($AcceptedAttributes,self::FIELDSET);
107
                break;
108
            case 'hidden':
109
                $AcceptedAttributes = array_merge($AcceptedAttributes,self::HIDDEN);
110
                break;
111
            default:
112
                if($Type === 'email') $AcceptedAttributes[]='multiple';
113
                $AcceptedAttributes = array_merge($AcceptedAttributes,self::TEXT);
114
                break;
115
        endswitch;
116
117
        $AcceptedAttributes = array_unique($AcceptedAttributes);
118
        $attributesKeys = array_keys($aAttributes);
119
        $ignore = array_diff($attributesKeys,$AcceptedAttributes);
120
        foreach ($ignore as $key=>$attribute)
121
        {
122
            if(is_numeric($attribute))
123
            {
124
                unset($ignore[$key]);
125
                $attribute = $aAttributes[$attribute];
126
                $ignore[$key] = $attribute;
127
            }
128
            if(substr($attribute,0,5)==='data-')
129
            {
130
                $AcceptedAttributes[] += $attribute;
131
                unset($ignore[$key]);
132
                continue;
133
            }
134
        }
135
        if($ignore) $Attributes->Ignore($ignore);
136
        return $ignore;
137
    }
138
}