MediaWiki
master
Toggle main menu visibility
XmlSelect.php
Go to the documentation of this file.
1
<?php
8
9
namespace
MediaWiki\Xml;
10
11
use MediaWiki\Html\Html;
12
16
class
XmlSelect
{
18
private
array $options = [];
20
private
$default;
22
private
array $attributes = [];
23
29
public
function
__construct
( $name =
false
, $id =
false
, $default =
false
) {
30
if
( $name ) {
31
$this->
setAttribute
(
'name'
, $name );
32
}
33
34
if
( $id ) {
35
$this->
setAttribute
(
'id'
, $id );
36
}
37
38
$this->
default
= $default;
39
}
40
44
public
function
setDefault
( $default ): void {
45
$this->default = $default;
46
}
47
52
public
function
setAttribute
(
string
$name, $value ): void {
53
$this->attributes[$name] = $value;
54
}
55
60
public
function
getAttribute
(
string
$name ) {
61
return
$this->attributes[$name] ??
null
;
62
}
63
68
public
function
addOption
(
string
$label, $value =
false
): void {
69
$value = $value !== false ? $value : $label;
70
$this->options[] = [ $label => $value ];
71
}
72
80
public
function
addOptions
( array $options ): void {
81
$this->options[] = $options;
82
}
83
93
public
static
function
formatOptions
( array $options, $default =
false
): string {
94
$data =
''
;
95
96
foreach
( $options as $label => $value ) {
97
if
( is_array( $value ) ) {
98
$contents = self::formatOptions( $value, $default );
99
$data .= Html::rawElement(
'optgroup'
, [
'label'
=> $label ], $contents ) .
"\n"
;
100
}
else
{
101
// If $default is an array, then the <select> probably has the multiple attribute,
102
// so we should check if each $value is in $default, rather than checking if
103
// $value is equal to $default.
104
$selected = is_array( $default ) ? in_array( $value, $default ) : $value === $default;
105
$data .=
Html::element
(
106
'option'
,
107
[
'value'
=> $value,
'selected'
=> $selected ],
108
$label
109
);
110
}
111
}
112
113
return
$data;
114
}
115
116
public
function
getHTML
(): string {
117
$contents =
''
;
118
119
foreach
( $this->options as $options ) {
120
$contents .= self::formatOptions( $options, $this->
default
);
121
}
122
123
return
Html::rawElement(
'select'
, $this->attributes, rtrim( $contents ) );
124
}
125
135
public
static
function
parseOptionsMessage
(
string
$msg ): array {
136
$options = [];
137
foreach
( explode(
','
, $msg ) as $option ) {
138
$parts = explode(
':'
, $option, 2 );
139
// Normalize options that only have one part.
140
$options[ trim( $parts[0] ) ] = trim( $parts[1] ?? $parts[0] );
141
}
142
return
$options;
143
}
144
}
145
146
class_alias( XmlSelect::class,
'XmlSelect'
);
MediaWiki\Xml\XmlSelect
Class for generating HTML <select> or <datalist> elements.
Definition
XmlSelect.php:16
MediaWiki\Xml\XmlSelect\setAttribute
setAttribute(string $name, $value)
Definition
XmlSelect.php:52
MediaWiki\Xml\XmlSelect\addOptions
addOptions(array $options)
This accepts an array of form label => value label => ( label => value, label => value ).
Definition
XmlSelect.php:80
MediaWiki\Xml\XmlSelect\getHTML
getHTML()
Definition
XmlSelect.php:116
MediaWiki\Xml\XmlSelect\setDefault
setDefault( $default)
Definition
XmlSelect.php:44
MediaWiki\Xml\XmlSelect\formatOptions
static formatOptions(array $options, $default=false)
This accepts an array of form: label => value label => ( label => value, label => value ).
Definition
XmlSelect.php:93
MediaWiki\Xml\XmlSelect\addOption
addOption(string $label, $value=false)
Definition
XmlSelect.php:68
MediaWiki\Xml\XmlSelect\__construct
__construct( $name=false, $id=false, $default=false)
Definition
XmlSelect.php:29
MediaWiki\Xml\XmlSelect\parseOptionsMessage
static parseOptionsMessage(string $msg)
Parse labels and values out of a comma- and colon-separated list of options, such as is used for expi...
Definition
XmlSelect.php:135
MediaWiki\Xml\XmlSelect\getAttribute
getAttribute(string $name)
Definition
XmlSelect.php:60
MediaWiki\Html\element
element(SerializerNode $parent, SerializerNode $node, $contents)
Definition
HtmlHelperTrait.php:38
includes
Xml
XmlSelect.php
Generated on
for MediaWiki by
1.18.0