File: InputWidget.php

package info (click to toggle)
mediawiki 1%3A1.39.13-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 320,416 kB
  • sloc: php: 815,516; javascript: 601,264; sql: 11,218; python: 4,863; xml: 3,080; sh: 990; ruby: 82; makefile: 78
file content (177 lines) | stat: -rw-r--r-- 3,986 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php

namespace OOUI;

/**
 * Base class for input widgets.
 *
 * @abstract
 */
class InputWidget extends Widget {
	use TabIndexedElement;
	use TitledElement;
	use AccessKeyedElement;

	/* Properties */

	/**
	 * Input element.
	 *
	 * @var Tag
	 */
	protected $input;

	/**
	 * Input value.
	 *
	 * @var string
	 */
	protected $value = '';

	/**
	 * @param array $config Configuration options
	 *      - string $config['name'] HTML input name (default: '')
	 *      - string $config['value'] Input value (default: '')
	 *      - string $config['dir'] The directionality of the input (ltr/rtl)
	 *      - string $config['inputId'] The value of the input’s HTML `id` attribute.
	 */
	public function __construct( array $config = [] ) {
		// Parent constructor
		parent::__construct( $config );

		// Properties
		$this->input = $this->getInputElement( $config );

		// Traits
		$this->initializeTabIndexedElement(
			array_merge( [ 'tabIndexed' => $this->input ], $config )
		);
		$this->initializeTitledElement(
			array_merge( [ 'titled' => $this->input ], $config )
		);
		$this->initializeAccessKeyedElement(
			array_merge( [ 'accessKeyed' => $this->input ], $config )
		);

		// Initialization
		if ( isset( $config['name'] ) ) {
			$this->input->setAttributes( [ 'name' => $config['name'] ] );
		}
		if ( $this->isDisabled() ) {
			$this->input->setAttributes( [ 'disabled' => 'disabled' ] );
		}
		$this
			->addClasses( [ 'oo-ui-inputWidget' ] )
			->appendContent( $this->input );
		$this->input->addClasses( [ 'oo-ui-inputWidget-input' ] );
		$this->setValue( $config['value'] ?? null );
		if ( isset( $config['dir'] ) ) {
			$this->setDir( $config['dir'] );
		}
		if ( isset( $config['inputId'] ) ) {
			$this->setInputId( $config['inputId'] );
		}
	}

	/**
	 * Get input element.
	 *
	 * @param array $config Configuration options
	 * @return Tag Input element
	 */
	protected function getInputElement( $config ) {
		return new Tag( 'input' );
	}

	/**
	 * Get the value of the input.
	 *
	 * @return string Input value
	 */
	public function getValue() {
		return $this->value;
	}

	/**
	 * Set the directionality of the input.
	 *
	 * @param string $dir Text directionality: 'ltr', 'rtl' or 'auto'
	 * @return $this
	 */
	public function setDir( $dir ) {
		$this->input->setAttributes( [ 'dir' => $dir ] );
		return $this;
	}

	/**
	 * Set the value of the input.
	 *
	 * @param mixed $value New value should usually be a string
	 * @return $this
	 */
	public function setValue( $value ) {
		$this->value = $this->cleanUpValue( $value );
		$this->input->setValue( $this->value );
		return $this;
	}

	/**
	 * Clean up incoming value.
	 *
	 * Ensures value is a string, and converts null to empty string.
	 *
	 * @param mixed $value Original value
	 * @return string Cleaned up value
	 */
	protected function cleanUpValue( $value ) {
		if ( $value === null ) {
			return '';
		} else {
			return (string)$value;
		}
	}

	/** @inheritDoc */
	public function setDisabled( $disabled ) {
		parent::setDisabled( $disabled );
		if ( isset( $this->input ) ) {
			if ( $this->isDisabled() ) {
				$this->input->setAttributes( [ 'disabled' => 'disabled' ] );
			} else {
				$this->input->removeAttributes( [ 'disabled' ] );
			}
		}
		return $this;
	}

	/**
	 * Set the 'id' attribute of the `<input>` element.
	 *
	 * @param string $id The ID of the input element
	 * @return $this
	 */
	public function setInputId( $id ) {
		$this->input->setAttributes( [ 'id' => $id ] );
		return $this;
	}

	/** @inheritDoc */
	public function getConfig( &$config ) {
		$name = $this->input->getAttribute( 'name' );
		if ( $name !== null ) {
			$config['name'] = $name;
		}
		if ( $this->value !== '' ) {
			$config['value'] = $this->value;
		}
		$dir = $this->input->getAttribute( 'dir' );
		if ( $dir !== null ) {
			$config['dir'] = $dir;
		}
		$id = $this->input->getAttribute( 'id' );
		if ( $id !== null ) {
			$config['inputId'] = $id;
		}
		return parent::getConfig( $config );
	}
}