MediaWiki master
XmlSelect.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Xml;
24
26
30class XmlSelect {
32 protected $options = [];
34 protected $default = false;
36 protected $tagName = 'select';
38 protected $attributes = [];
39
40 public function __construct( $name = false, $id = false, $default = false ) {
41 if ( $name ) {
42 $this->setAttribute( 'name', $name );
43 }
44
45 if ( $id ) {
46 $this->setAttribute( 'id', $id );
47 }
48
49 if ( $default !== false ) {
50 $this->default = $default;
51 }
52 }
53
57 public function setDefault( $default ) {
58 $this->default = $default;
59 }
60
64 public function setTagName( $tagName ) {
65 $this->tagName = $tagName;
66 }
67
72 public function setAttribute( $name, $value ) {
73 $this->attributes[$name] = $value;
74 }
75
80 public function getAttribute( $name ) {
81 return $this->attributes[$name] ?? null;
82 }
83
88 public function addOption( $label, $value = false ) {
89 $value = $value !== false ? $value : $label;
90 $this->options[] = [ $label => $value ];
91 }
92
100 public function addOptions( $options ) {
101 $this->options[] = $options;
102 }
103
113 public static function formatOptions( $options, $default = false ) {
114 $data = '';
115
116 foreach ( $options as $label => $value ) {
117 if ( is_array( $value ) ) {
118 $contents = self::formatOptions( $value, $default );
119 $data .= Html::rawElement( 'optgroup', [ 'label' => $label ], $contents ) . "\n";
120 } else {
121 // If $default is an array, then the <select> probably has the multiple attribute,
122 // so we should check if each $value is in $default, rather than checking if
123 // $value is equal to $default.
124 $selected = is_array( $default ) ? in_array( $value, $default ) : $value === $default;
125 $data .= Xml::option( $label, $value, $selected ) . "\n";
126 }
127 }
128
129 return $data;
130 }
131
135 public function getHTML() {
136 $contents = '';
137
138 foreach ( $this->options as $options ) {
139 $contents .= self::formatOptions( $options, $this->default );
140 }
141
142 return Html::rawElement( $this->tagName, $this->attributes, rtrim( $contents ) );
143 }
144
154 public static function parseOptionsMessage( string $msg ): array {
155 $options = [];
156 foreach ( explode( ',', $msg ) as $option ) {
157 // Normalize options that only have one part.
158 if ( strpos( $option, ':' ) === false ) {
159 $option = "$option:$option";
160 }
161 // Extract the two parts.
162 [ $label, $value ] = explode( ':', $option );
163 $options[ trim( $label ) ] = trim( $value );
164 }
165 return $options;
166 }
167}
169class_alias( XmlSelect::class, 'XmlSelect' );
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
Class for generating HTML <select> or <datalist> elements.
Definition XmlSelect.php:30
string array false $default
Definition XmlSelect.php:34
string int[] $attributes
Definition XmlSelect.php:38
string array $tagName
Definition XmlSelect.php:36
addOption( $label, $value=false)
Definition XmlSelect.php:88
static formatOptions( $options, $default=false)
This accepts an array of form: label => value label => ( label => value, label => value )
__construct( $name=false, $id=false, $default=false)
Definition XmlSelect.php:40
setAttribute( $name, $value)
Definition XmlSelect.php:72
static parseOptionsMessage(string $msg)
Parse labels and values out of a comma- and colon-separated list of options, such as is used for expi...
addOptions( $options)
This accepts an array of form label => value label => ( label => value, label => value )
static option( $text, $value=null, $selected=false, $attribs=[])
Convenience function to build an HTML drop-down list item.
Definition Xml.php:519