| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Xml; |
| 24 |
|
| 25 |
use MediaWiki\Html\Html; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
class XmlSelect { |
| 31 |
|
| 32 |
protected $options = []; |
| 33 |
|
| 34 |
protected $default = false; |
| 35 |
|
| 36 |
protected $tagName = 'select'; |
| 37 |
|
| 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 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function setDefault( $default ) { |
| 58 |
$this->default = $default; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function setTagName( $tagName ) { |
| 65 |
$this->tagName = $tagName; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public function setAttribute( $name, $value ) { |
| 73 |
$this->attributes[$name] = $value; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public function getAttribute( $name ) { |
| 81 |
return $this->attributes[$name] ?? null; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public function addOption( $label, $value = false ) { |
| 89 |
$value = $value !== false ? $value : $label; |
| 90 |
$this->options[] = [ $label => $value ]; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public function addOptions( $options ) { |
| 101 |
$this->options[] = $options; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 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 |
|
| 122 |
|
| 123 |
|
| 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 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 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 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public static function parseOptionsMessage( string $msg ): array { |
| 155 |
$options = []; |
| 156 |
foreach ( explode( ',', $msg ) as $option ) { |
| 157 |
|
| 158 |
if ( strpos( $option, ':' ) === false ) { |
| 159 |
$option = "$option:$option"; |
| 160 |
} |
| 161 |
|
| 162 |
[ $label, $value ] = explode( ':', $option ); |
| 163 |
$options[ trim( $label ) ] = trim( $value ); |
| 164 |
} |
| 165 |
return $options; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
class_alias( XmlSelect::class, 'XmlSelect' ); |