MediaWiki  master
XmlSelect.php
Go to the documentation of this file.
1 <?php
24 
28 class XmlSelect {
29  protected $options = [];
30  protected $default = false;
31  protected $tagName = 'select';
32  protected $attributes = [];
33 
34  public function __construct( $name = false, $id = false, $default = false ) {
35  if ( $name ) {
36  $this->setAttribute( 'name', $name );
37  }
38 
39  if ( $id ) {
40  $this->setAttribute( 'id', $id );
41  }
42 
43  if ( $default !== false ) {
44  $this->default = $default;
45  }
46  }
47 
51  public function setDefault( $default ) {
52  $this->default = $default;
53  }
54 
58  public function setTagName( $tagName ) {
59  $this->tagName = $tagName;
60  }
61 
66  public function setAttribute( $name, $value ) {
67  $this->attributes[$name] = $value;
68  }
69 
74  public function getAttribute( $name ) {
75  return $this->attributes[$name] ?? null;
76  }
77 
82  public function addOption( $label, $value = false ) {
83  $value = $value !== false ? $value : $label;
84  $this->options[] = [ $label => $value ];
85  }
86 
94  public function addOptions( $options ) {
95  $this->options[] = $options;
96  }
97 
107  public static function formatOptions( $options, $default = false ) {
108  $data = '';
109 
110  foreach ( $options as $label => $value ) {
111  if ( is_array( $value ) ) {
112  $contents = self::formatOptions( $value, $default );
113  $data .= Html::rawElement( 'optgroup', [ 'label' => $label ], $contents ) . "\n";
114  } else {
115  // If $default is an array, then the <select> probably has the multiple attribute,
116  // so we should check if each $value is in $default, rather than checking if
117  // $value is equal to $default.
118  $selected = is_array( $default ) ? in_array( $value, $default ) : $value === $default;
119  $data .= Xml::option( $label, $value, $selected ) . "\n";
120  }
121  }
122 
123  return $data;
124  }
125 
129  public function getHTML() {
130  $contents = '';
131 
132  foreach ( $this->options as $options ) {
133  $contents .= self::formatOptions( $options, $this->default );
134  }
135 
136  return Html::rawElement( $this->tagName, $this->attributes, rtrim( $contents ) );
137  }
138 
148  public static function parseOptionsMessage( string $msg ): array {
149  $options = [];
150  foreach ( explode( ',', $msg ) as $option ) {
151  // Normalize options that only have one part.
152  if ( strpos( $option, ':' ) === false ) {
153  $option = "$option:$option";
154  }
155  // Extract the two parts.
156  [ $label, $value ] = explode( ':', $option );
157  $options[ trim( $label ) ] = trim( $value );
158  }
159  return $options;
160  }
161 }
This class is a collection of static functions that serve two purposes:
Definition: Html.php:57
Class for generating HTML <select> or <datalist> elements.
Definition: XmlSelect.php:28
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:148
setTagName( $tagName)
Definition: XmlSelect.php:58
getAttribute( $name)
Definition: XmlSelect.php:74
setDefault( $default)
Definition: XmlSelect.php:51
setAttribute( $name, $value)
Definition: XmlSelect.php:66
__construct( $name=false, $id=false, $default=false)
Definition: XmlSelect.php:34
static formatOptions( $options, $default=false)
This accepts an array of form: label => value label => ( label => value, label => value )
Definition: XmlSelect.php:107
addOption( $label, $value=false)
Definition: XmlSelect.php:82
addOptions( $options)
This accepts an array of form label => value label => ( label => value, label => value )
Definition: XmlSelect.php:94
static option( $text, $value=null, $selected=false, $attribs=[])
Convenience function to build an HTML drop-down list item.
Definition: Xml.php:499