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