Merge "Http::getProxy() method to get proxy configuration"
[lhc/web/wiklou.git] / includes / htmlform / HTMLTextField.php
1 <?php
2
3 class HTMLTextField extends HTMLFormField {
4 function getSize() {
5 return isset( $this->mParams['size'] ) ? $this->mParams['size'] : 45;
6 }
7
8 function getSpellCheck() {
9 $val = isset( $this->mParams['spellcheck'] ) ? $this->mParams['spellcheck'] : null;
10 if ( is_bool( $val ) ) {
11 // "spellcheck" attribute literally requires "true" or "false" to work.
12 return $val === true ? 'true' : 'false';
13 }
14 return null;
15 }
16
17 function getInputHTML( $value ) {
18 $attribs = [
19 'id' => $this->mID,
20 'name' => $this->mName,
21 'size' => $this->getSize(),
22 'value' => $value,
23 'dir' => $this->mDir,
24 'spellcheck' => $this->getSpellCheck(),
25 ] + $this->getTooltipAndAccessKey() + $this->getDataAttribs();
26
27 if ( $this->mClass !== '' ) {
28 $attribs['class'] = $this->mClass;
29 }
30
31 # @todo Enforce pattern, step, required, readonly on the server side as
32 # well
33 $allowedParams = [
34 'type',
35 'min',
36 'max',
37 'pattern',
38 'title',
39 'step',
40 'placeholder',
41 'list',
42 'maxlength',
43 'tabindex',
44 'disabled',
45 'required',
46 'autofocus',
47 'multiple',
48 'readonly'
49 ];
50
51 $attribs += $this->getAttributes( $allowedParams );
52
53 # Extract 'type'
54 $type = $this->getType( $attribs );
55 return Html::input( $this->mName, $value, $type, $attribs );
56 }
57
58 protected function getType( &$attribs ) {
59 $type = isset( $attribs['type'] ) ? $attribs['type'] : 'text';
60 unset( $attribs['type'] );
61
62 # Implement tiny differences between some field variants
63 # here, rather than creating a new class for each one which
64 # is essentially just a clone of this one.
65 if ( isset( $this->mParams['type'] ) ) {
66 switch ( $this->mParams['type'] ) {
67 case 'int':
68 $type = 'number';
69 break;
70 case 'float':
71 $type = 'number';
72 $attribs['step'] = 'any';
73 break;
74 # Pass through
75 case 'email':
76 case 'password':
77 case 'file':
78 case 'url':
79 $type = $this->mParams['type'];
80 break;
81 }
82 }
83
84 return $type;
85 }
86
87 function getInputOOUI( $value ) {
88 $attribs = $this->getTooltipAndAccessKey();
89
90 if ( $this->mClass !== '' ) {
91 $attribs['classes'] = [ $this->mClass ];
92 }
93
94 # @todo Enforce pattern, step, required, readonly on the server side as
95 # well
96 $allowedParams = [
97 'autofocus',
98 'autosize',
99 'disabled',
100 'flags',
101 'indicator',
102 'maxlength',
103 'placeholder',
104 'readonly',
105 'required',
106 'tabindex',
107 'type',
108 ];
109
110 $attribs += OOUI\Element::configFromHtmlAttributes(
111 $this->getAttributes( $allowedParams )
112 );
113
114 $type = $this->getType( $attribs );
115
116 return $this->getInputWidget( [
117 'id' => $this->mID,
118 'name' => $this->mName,
119 'value' => $value,
120 'type' => $type,
121 ] + $attribs );
122 }
123
124 protected function getInputWidget( $params ) {
125 return new OOUI\TextInputWidget( $params );
126 }
127
128 /**
129 * Returns an array of data-* attributes to add to the field.
130 *
131 * @return array
132 */
133 protected function getDataAttribs() {
134 return [];
135 }
136 }