1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
|
<?php
namespace Wikimedia\Message;
use MediaWiki\Json\JsonDeserializable;
use MediaWiki\Json\JsonDeserializableTrait;
use MediaWiki\Json\JsonDeserializer;
use Stringable;
/**
* Value object representing a message for i18n.
*
* A MessageValue holds a key and an array of parameters. It can be converted
* to a string in a particular language using formatters obtained from an
* IMessageFormatterFactory.
*
* MessageValues are pure value objects and are newable and (de)serializable.
*
* @newable
*/
class MessageValue implements JsonDeserializable, MessageSpecifier {
use JsonDeserializableTrait;
/** @var string */
private $key;
/** @var MessageParam[] */
private $params;
/**
* @stable to call
*
* @param string $key
* @param (MessageParam|MessageSpecifier|string|int|float)[] $params Values that are not instances
* of MessageParam are wrapped using ParamType::TEXT.
*/
public function __construct( $key, $params = [] ) {
$this->key = $key;
$this->params = [];
$this->params( ...$params );
}
/**
* Static constructor for easier chaining of `->params()` methods
* @param string $key
* @param (MessageParam|MessageSpecifier|string|int|float)[] $params
* @return MessageValue
*/
public static function new( $key, $params = [] ) {
return new MessageValue( $key, $params );
}
/**
* Convert from any MessageSpecifier to a MessageValue.
*
* When the given object is an instance of MessageValue, the same object is returned.
*
* @since 1.43
* @param MessageSpecifier $spec
* @return MessageValue
*/
public static function newFromSpecifier( MessageSpecifier $spec ) {
if ( $spec instanceof MessageValue ) {
return $spec;
}
return new MessageValue( $spec->getKey(), $spec->getParams() );
}
/**
* Get the message key
*
* @return string
*/
public function getKey() {
return $this->key;
}
/**
* Get the parameter array
*
* @return MessageParam[]
*/
public function getParams() {
return $this->params;
}
/**
* Chainable mutator which adds text parameters and MessageParam parameters
*
* @param MessageParam|MessageSpecifier|string|int|float ...$values
* @return $this
*/
public function params( ...$values ) {
foreach ( $values as $value ) {
if ( $value instanceof MessageParam ) {
$this->params[] = $value;
} else {
$this->params[] = new ScalarParam( ParamType::TEXT, $value );
}
}
return $this;
}
/**
* Chainable mutator which adds text parameters with a common type
*
* @param string $type One of the ParamType constants
* @param MessageSpecifier|string|int|float ...$values Scalar values
* @return $this
*/
public function textParamsOfType( $type, ...$values ) {
foreach ( $values as $value ) {
$this->params[] = new ScalarParam( $type, $value );
}
return $this;
}
/**
* Chainable mutator which adds object parameters
*
* @deprecated since 1.43
* @param Stringable ...$values stringable object values
* @return $this
*/
public function objectParams( ...$values ) {
wfDeprecated( __METHOD__, '1.43' );
foreach ( $values as $value ) {
$this->params[] = new ScalarParam( ParamType::OBJECT, $value );
}
return $this;
}
/**
* Chainable mutator which adds list parameters with a common type
*
* @param string $listType One of the ListType constants
* @param (MessageParam|MessageSpecifier|string|int|float)[] ...$values Each value
* is an array of items suitable to pass as $params to ListParam::__construct()
* @return $this
*/
public function listParamsOfType( $listType, ...$values ) {
foreach ( $values as $value ) {
$this->params[] = new ListParam( $listType, $value );
}
return $this;
}
/**
* Chainable mutator which adds parameters of type text (ParamType::TEXT).
*
* @param MessageSpecifier|string|int|float ...$values
* @return $this
*/
public function textParams( ...$values ) {
return $this->textParamsOfType( ParamType::TEXT, ...$values );
}
/**
* Chainable mutator which adds numeric parameters (ParamType::NUM).
*
* @param int|float ...$values
* @return $this
*/
public function numParams( ...$values ) {
return $this->textParamsOfType( ParamType::NUM, ...$values );
}
/**
* Chainable mutator which adds parameters which are a duration specified
* in seconds (ParamType::DURATION_LONG).
*
* This is similar to shorDurationParams() except that the result will be
* more verbose.
*
* @param int|float ...$values
* @return $this
*/
public function longDurationParams( ...$values ) {
return $this->textParamsOfType( ParamType::DURATION_LONG, ...$values );
}
/**
* Chainable mutator which adds parameters which are a duration specified
* in seconds (ParamType::DURATION_SHORT).
*
* This is similar to longDurationParams() except that the result will be more
* compact.
*
* @param int|float ...$values
* @return $this
*/
public function shortDurationParams( ...$values ) {
return $this->textParamsOfType( ParamType::DURATION_SHORT, ...$values );
}
/**
* Chainable mutator which adds parameters which are an expiry timestamp (ParamType::EXPIRY).
*
* @param string ...$values Timestamp as accepted by the Wikimedia\Timestamp library,
* or "infinity"
* @return $this
*/
public function expiryParams( ...$values ) {
return $this->textParamsOfType( ParamType::EXPIRY, ...$values );
}
/**
* Chainable mutator which adds parameters which are a date-time timestamp (ParamType::DATETIME).
*
* @since 1.36
* @param string ...$values Timestamp as accepted by the Wikimedia\Timestamp library.
* @return $this
*/
public function dateTimeParams( ...$values ) {
return $this->textParamsOfType( ParamType::DATETIME, ...$values );
}
/**
* Chainable mutator which adds parameters which are a date timestamp (ParamType::DATE).
*
* @since 1.36
* @param string ...$values Timestamp as accepted by the Wikimedia\Timestamp library.
* @return $this
*/
public function dateParams( ...$values ) {
return $this->textParamsOfType( ParamType::DATE, ...$values );
}
/**
* Chainable mutator which adds parameters which are a time timestamp (ParamType::TIME).
*
* @since 1.36
* @param string ...$values Timestamp as accepted by the Wikimedia\Timestamp library.
* @return $this
*/
public function timeParams( ...$values ) {
return $this->textParamsOfType( ParamType::TIME, ...$values );
}
/**
* Chainable mutator which adds parameters which are a user group (ParamType::GROUP).
*
* @since 1.38
* @param string ...$values User Groups
* @return $this
*/
public function userGroupParams( ...$values ) {
return $this->textParamsOfType( ParamType::GROUP, ...$values );
}
/**
* Chainable mutator which adds parameters which are a number of bytes (ParamType::SIZE).
*
* @param int ...$values
* @return $this
*/
public function sizeParams( ...$values ) {
return $this->textParamsOfType( ParamType::SIZE, ...$values );
}
/**
* Chainable mutator which adds parameters which are a number of bits per
* second (ParamType::BITRATE).
*
* @param int|float ...$values
* @return $this
*/
public function bitrateParams( ...$values ) {
return $this->textParamsOfType( ParamType::BITRATE, ...$values );
}
/**
* Chainable mutator which adds "raw" parameters (ParamType::RAW).
*
* Raw parameters are substituted after formatter processing. The caller is responsible
* for ensuring that the value will be safe for the intended output format, and
* documenting what that intended output format is.
*
* @param string ...$values
* @return $this
*/
public function rawParams( ...$values ) {
return $this->textParamsOfType( ParamType::RAW, ...$values );
}
/**
* Chainable mutator which adds plaintext parameters (ParamType::PLAINTEXT).
*
* Plaintext parameters are substituted after formatter processing. The value
* will be escaped by the formatter as appropriate for the target output format
* so as to be represented as plain text rather than as any sort of markup.
*
* @param string ...$values
* @return $this
*/
public function plaintextParams( ...$values ) {
return $this->textParamsOfType( ParamType::PLAINTEXT, ...$values );
}
/**
* Chainable mutator which adds comma lists (ListType::COMMA).
*
* The list parameters thus created are formatted as a comma-separated list,
* or some local equivalent.
*
* @param (MessageParam|MessageSpecifier|string|int|float)[] ...$values Each value
* is an array of items suitable to pass as $params to ListParam::__construct()
* @return $this
*/
public function commaListParams( ...$values ) {
return $this->listParamsOfType( ListType::COMMA, ...$values );
}
/**
* Chainable mutator which adds semicolon lists (ListType::SEMICOLON).
*
* The list parameters thus created are formatted as a semicolon-separated
* list, or some local equivalent.
*
* @param (MessageParam|MessageSpecifier|string|int|float)[] ...$values Each value
* is an array of items suitable to pass as $params to ListParam::__construct()
* @return $this
*/
public function semicolonListParams( ...$values ) {
return $this->listParamsOfType( ListType::SEMICOLON, ...$values );
}
/**
* Chainable mutator which adds pipe lists (ListType::PIPE).
*
* The list parameters thus created are formatted as a pipe ("|") -separated
* list, or some local equivalent.
*
* @param (MessageParam|MessageSpecifier|string|int|float)[] ...$values Each value
* is an array of items suitable to pass as $params to ListParam::__construct()
* @return $this
*/
public function pipeListParams( ...$values ) {
return $this->listParamsOfType( ListType::PIPE, ...$values );
}
/**
* Chainable mutator which adds natural-language lists (ListType::AND).
*
* The list parameters thus created, when formatted, are joined as in natural
* language. In English, this means a comma-separated list, with the last
* two elements joined with "and".
*
* @param (MessageParam|string)[] ...$values
* @return $this
*/
public function textListParams( ...$values ) {
return $this->listParamsOfType( ListType::AND, ...$values );
}
/**
* Dump the object for testing/debugging
*
* @return string
*/
public function dump() {
$contents = '';
foreach ( $this->params as $param ) {
$contents .= $param->dump();
}
return '<message key="' . htmlspecialchars( $this->key ) . '">' .
$contents . '</message>';
}
protected function toJsonArray(): array {
// WARNING: When changing how this class is serialized, follow the instructions
// at <https://www.mediawiki.org/wiki/Manual:Parser_cache/Serialization_compatibility>!
return [
'key' => $this->key,
'params' => $this->params,
];
}
public static function newFromJsonArray( JsonDeserializer $deserializer, array $json ) {
// WARNING: When changing how this class is serialized, follow the instructions
// at <https://www.mediawiki.org/wiki/Manual:Parser_cache/Serialization_compatibility>!
return new self( $json['key'], $json['params'] );
}
}
|