MediaWiki master
XmlSelect.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Xml;
10
12
16class XmlSelect {
18 private array $options = [];
20 private $default;
21 private string $tagName = 'select';
23 private array $attributes = [];
24
30 public function __construct( $name = false, $id = false, $default = false ) {
31 if ( $name ) {
32 $this->setAttribute( 'name', $name );
33 }
34
35 if ( $id ) {
36 $this->setAttribute( 'id', $id );
37 }
38
39 $this->default = $default;
40 }
41
45 public function setDefault( $default ): void {
46 $this->default = $default;
47 }
48
52 public function setTagName( string $tagName ): void {
53 wfDeprecated( __METHOD__, '1.45' );
54 $this->tagName = $tagName;
55 }
56
61 public function setAttribute( string $name, $value ): void {
62 $this->attributes[$name] = $value;
63 }
64
69 public function getAttribute( string $name ) {
70 return $this->attributes[$name] ?? null;
71 }
72
77 public function addOption( string $label, $value = false ): void {
78 $value = $value !== false ? $value : $label;
79 $this->options[] = [ $label => $value ];
80 }
81
89 public function addOptions( array $options ): void {
90 $this->options[] = $options;
91 }
92
102 public static function formatOptions( array $options, $default = false ): string {
103 $data = '';
104
105 foreach ( $options as $label => $value ) {
106 if ( is_array( $value ) ) {
107 $contents = self::formatOptions( $value, $default );
108 $data .= Html::rawElement( 'optgroup', [ 'label' => $label ], $contents ) . "\n";
109 } else {
110 // If $default is an array, then the <select> probably has the multiple attribute,
111 // so we should check if each $value is in $default, rather than checking if
112 // $value is equal to $default.
113 $selected = is_array( $default ) ? in_array( $value, $default ) : $value === $default;
114 $data .= Html::element(
115 'option',
116 [ 'value' => $value, 'selected' => $selected ],
117 $label
118 );
119 }
120 }
121
122 return $data;
123 }
124
125 public function getHTML(): string {
126 $contents = '';
127
128 foreach ( $this->options as $options ) {
129 $contents .= self::formatOptions( $options, $this->default );
130 }
131
132 return Html::rawElement( $this->tagName, $this->attributes, rtrim( $contents ) );
133 }
134
144 public static function parseOptionsMessage( string $msg ): array {
145 $options = [];
146 foreach ( explode( ',', $msg ) as $option ) {
147 $parts = explode( ':', $option, 2 );
148 // Normalize options that only have one part.
149 $options[ trim( $parts[0] ) ] = trim( $parts[1] ?? $parts[0] );
150 }
151 return $options;
152 }
153}
155class_alias( XmlSelect::class, 'XmlSelect' );
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
This class is a collection of static functions that serve two purposes:
Definition Html.php:43
Class for generating HTML <select> or <datalist> elements.
Definition XmlSelect.php:16
setAttribute(string $name, $value)
Definition XmlSelect.php:61
addOptions(array $options)
This accepts an array of form label => value label => ( label => value, label => value )
Definition XmlSelect.php:89
setTagName(string $tagName)
Definition XmlSelect.php:52
static formatOptions(array $options, $default=false)
This accepts an array of form: label => value label => ( label => value, label => value )
addOption(string $label, $value=false)
Definition XmlSelect.php:77
__construct( $name=false, $id=false, $default=false)
Definition XmlSelect.php:30
static parseOptionsMessage(string $msg)
Parse labels and values out of a comma- and colon-separated list of options, such as is used for expi...
getAttribute(string $name)
Definition XmlSelect.php:69