1 | <?php |
||
5 | class Normalizer |
||
6 | { |
||
7 | const BOOLEAN_ATTRIBUTES = [ |
||
8 | 'autofocus', 'capture', 'checked', 'disabled', 'draggable', 'formnovalidate', 'hidden', |
||
9 | 'multiple', 'novalidate', 'readonly', 'required', 'selected', 'spellcheck', |
||
10 | 'webkitdirectory', |
||
11 | ]; |
||
12 | |||
13 | const FORM_ATTRIBUTES = [ |
||
14 | 'accept', 'accept-charset', 'action', 'autocapitalize', 'autocomplete', 'enctype', |
||
15 | 'method', 'name', 'novalidate', 'target', |
||
16 | ]; |
||
17 | |||
18 | const GLOBAL_ATTRIBUTES = [ |
||
19 | 'accesskey', 'class', 'contenteditable', 'contextmenu', 'dir', 'draggable', 'dropzone', |
||
20 | 'hidden', 'id', 'lang', 'spellcheck', 'style', 'tabindex', 'title', |
||
21 | ]; |
||
22 | |||
23 | const GLOBAL_WILDCARD_ATTRIBUTES = [ |
||
24 | 'aria-', 'data-', 'item', 'on', |
||
25 | ]; |
||
26 | |||
27 | const INPUT_ATTRIBUTES = [ |
||
28 | 'accept', 'autocapitalize', 'autocomplete', 'autocorrect', 'autofocus', 'capture', |
||
29 | 'checked', 'disabled', 'form', 'formaction', 'formenctype', 'formmethod', |
||
30 | 'formnovalidate', 'formtarget', 'height', 'incremental', 'inputmode', 'list', 'max', |
||
31 | 'maxlength', 'min', 'minlength', 'mozactionhint', 'multiple', 'name', 'pattern', |
||
32 | 'placeholder', 'readonly', 'required', 'results', 'selectionDirection', 'size', 'src', |
||
33 | 'step', 'type', 'value', 'webkitdirectory', 'width', 'x-moz-errormessage', |
||
34 | ]; |
||
35 | |||
36 | const INPUT_TYPES = [ |
||
37 | 'button', 'checkbox', 'color', 'date', 'datetime', 'datetime-local', 'email', 'file', |
||
38 | 'hidden', 'image', 'max', 'min', 'month', 'number', 'password', 'radio', 'range', |
||
39 | 'reset', 'search', 'step', 'submit', 'tel', 'text', 'time', 'url', 'value', 'week', |
||
40 | ]; |
||
41 | |||
42 | const SELECT_ATTRIBUTES = [ |
||
43 | 'autofocus', 'disabled', 'form', 'multiple', 'name', 'required', 'size', |
||
44 | ]; |
||
45 | |||
46 | const TEXTAREA_ATTRIBUTES = [ |
||
47 | 'autocapitalize', 'autocomplete', 'autofocus', 'cols', 'disabled', 'form', 'maxlength', |
||
48 | 'minlength', 'name', 'placeholder', 'readonly', 'required', 'rows', |
||
49 | 'selectionDirection', 'selectionEnd', 'selectionStart', 'wrap', |
||
50 | ]; |
||
51 | |||
52 | /** |
||
53 | * @var array |
||
54 | */ |
||
55 | protected $args; |
||
56 | |||
57 | public function __construct( array $args = [] ) |
||
61 | |||
62 | /** |
||
63 | * Normalize form attributes |
||
64 | * |
||
65 | * @return array|string |
||
66 | */ |
||
67 | public function form( array $args = [], $implode = false ) |
||
73 | |||
74 | /** |
||
75 | * Normalize input attributes |
||
76 | * |
||
77 | * @return array|string |
||
78 | */ |
||
79 | public function input( array $args = [], $implode = false ) |
||
87 | |||
88 | /** |
||
89 | * Possibly implode attributes into a string |
||
90 | * |
||
91 | * @param bool|string $implode |
||
92 | * |
||
93 | * @return array|string |
||
94 | */ |
||
95 | public function maybeImplode( array $attributes, $implode = true ) |
||
114 | |||
115 | /** |
||
116 | * Normalize select attributes |
||
117 | * |
||
118 | * @return array|string |
||
119 | */ |
||
120 | public function select( array $args = [], $implode = false ) |
||
126 | |||
127 | /** |
||
128 | * Normalize textarea attributes |
||
129 | * |
||
130 | * @return array|string |
||
131 | */ |
||
132 | public function textarea( array $args = [], $implode = false ) |
||
138 | |||
139 | /** |
||
140 | * Filter attributes by the provided attrribute keys and remove any non-boolean keys |
||
141 | * with empty values |
||
142 | * |
||
143 | * @return array |
||
144 | */ |
||
145 | protected function filterAttributes( array $attributeKeys ) |
||
171 | |||
172 | /** |
||
173 | * @return array |
||
174 | */ |
||
175 | protected function filterGlobalAttributes() |
||
206 | |||
207 | /** |
||
208 | * @return void |
||
209 | */ |
||
210 | protected function filterInputType() |
||
216 | |||
217 | /** |
||
218 | * @return array |
||
219 | */ |
||
220 | protected function parseAttributes( array $attributes, array $args = [] ) |
||
231 | } |
||
232 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: