MediaWiki  1.34.0
MessageValue.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Wikimedia\Message;
4 
14 class MessageValue {
16  private $key;
17 
19  private $params;
20 
26  public function __construct( $key, $params = [] ) {
27  $this->key = $key;
28  $this->params = [];
29  $this->params( ...$params );
30  }
31 
37  public function getKey() {
38  return $this->key;
39  }
40 
46  public function getParams() {
47  return $this->params;
48  }
49 
56  public function params( ...$values ) {
57  foreach ( $values as $value ) {
58  if ( $value instanceof MessageParam ) {
59  $this->params[] = $value;
60  } else {
61  $this->params[] = new ScalarParam( ParamType::TEXT, $value );
62  }
63  }
64  return $this;
65  }
66 
74  public function textParamsOfType( $type, ...$values ) {
75  foreach ( $values as $value ) {
76  $this->params[] = new ScalarParam( $type, $value );
77  }
78  return $this;
79  }
80 
89  public function listParamsOfType( $listType, ...$values ) {
90  foreach ( $values as $value ) {
91  $this->params[] = new ListParam( $listType, $value );
92  }
93  return $this;
94  }
95 
102  public function textParams( ...$values ) {
103  return $this->textParamsOfType( ParamType::TEXT, ...$values );
104  }
105 
112  public function numParams( ...$values ) {
113  return $this->textParamsOfType( ParamType::NUM, ...$values );
114  }
115 
126  public function longDurationParams( ...$values ) {
127  return $this->textParamsOfType( ParamType::DURATION_LONG, ...$values );
128  }
129 
140  public function shortDurationParams( ...$values ) {
141  return $this->textParamsOfType( ParamType::DURATION_SHORT, ...$values );
142  }
143 
151  public function expiryParams( ...$values ) {
152  return $this->textParamsOfType( ParamType::EXPIRY, ...$values );
153  }
154 
161  public function sizeParams( ...$values ) {
162  return $this->textParamsOfType( ParamType::SIZE, ...$values );
163  }
164 
172  public function bitrateParams( ...$values ) {
173  return $this->textParamsOfType( ParamType::BITRATE, ...$values );
174  }
175 
186  public function rawParams( ...$values ) {
187  return $this->textParamsOfType( ParamType::RAW, ...$values );
188  }
189 
200  public function plaintextParams( ...$values ) {
201  return $this->textParamsOfType( ParamType::PLAINTEXT, ...$values );
202  }
203 
214  public function commaListParams( ...$values ) {
215  return $this->listParamsOfType( ListType::COMMA, ...$values );
216  }
217 
228  public function semicolonListParams( ...$values ) {
229  return $this->listParamsOfType( ListType::SEMICOLON, ...$values );
230  }
231 
242  public function pipeListParams( ...$values ) {
243  return $this->listParamsOfType( ListType::PIPE, ...$values );
244  }
245 
256  public function textListParams( ...$values ) {
257  return $this->listParamsOfType( ListType::AND, ...$values );
258  }
259 
265  public function dump() {
266  $contents = '';
267  foreach ( $this->params as $param ) {
268  $contents .= $param->dump();
269  }
270  return '<message key="' . htmlspecialchars( $this->key ) . '">' .
271  $contents . '</message>';
272  }
273 }
Wikimedia\Message\ParamType\PLAINTEXT
const PLAINTEXT
A text parameter which is substituted after formatter processing.
Definition: ParamType.php:64
Wikimedia\Message\ParamType\EXPIRY
const EXPIRY
An expiry time.
Definition: ParamType.php:39
Wikimedia\Message\MessageValue\sizeParams
sizeParams(... $values)
Chainable mutator which adds parameters which are a number of bytes (ParamType::SIZE).
Definition: MessageValue.php:161
Wikimedia\Message\MessageValue\pipeListParams
pipeListParams(... $values)
Chainable mutator which adds pipe lists (ListType::PIPE).
Definition: MessageValue.php:242
Wikimedia\Message\MessageValue\textParams
textParams(... $values)
Chainable mutator which adds parameters of type text (ParamType::TEXT).
Definition: MessageValue.php:102
Wikimedia\Message\MessageValue\textListParams
textListParams(... $values)
Chainable mutator which adds natural-language lists (ListType::AND).
Definition: MessageValue.php:256
Wikimedia\Message\MessageValue\getKey
getKey()
Get the message key.
Definition: MessageValue.php:37
Wikimedia\Message\ParamType\SIZE
const SIZE
A number of bytes.
Definition: ParamType.php:42
Wikimedia\Message\MessageValue\commaListParams
commaListParams(... $values)
Chainable mutator which adds comma lists (ListType::COMMA).
Definition: MessageValue.php:214
Wikimedia\Message\ScalarParam
Value object representing a message parameter holding a single value.
Definition: ScalarParam.php:10
Wikimedia\Message\MessageValue\numParams
numParams(... $values)
Chainable mutator which adds numeric parameters (ParamType::NUM).
Definition: MessageValue.php:112
Wikimedia\Message\ListType\COMMA
const COMMA
A comma-separated list.
Definition: ListType.php:11
Wikimedia\Message\MessageValue
Value object representing a message for i18n.
Definition: MessageValue.php:14
Wikimedia\Message
Definition: IMessageFormatterFactory.php:3
Wikimedia\Message\MessageValue\getParams
getParams()
Get the parameter array.
Definition: MessageValue.php:46
Wikimedia\Message\ParamType\BITRATE
const BITRATE
A number of bits per second.
Definition: ParamType.php:45
Wikimedia\Message\MessageValue\dump
dump()
Dump the object for testing/debugging.
Definition: MessageValue.php:265
Wikimedia\Message\MessageValue\bitrateParams
bitrateParams(... $values)
Chainable mutator which adds parameters which are a number of bits per second (ParamType::BITRATE).
Definition: MessageValue.php:172
Wikimedia\Message\MessageValue\textParamsOfType
textParamsOfType( $type,... $values)
Chainable mutator which adds text parameters with a common type.
Definition: MessageValue.php:74
Wikimedia\Message\MessageValue\params
params(... $values)
Chainable mutator which adds text parameters and MessageParam parameters.
Definition: MessageValue.php:56
Wikimedia\Message\ParamType\DURATION_LONG
const DURATION_LONG
A number of seconds, to be formatted as natural language text.
Definition: ParamType.php:22
Wikimedia\Message\MessageValue\$params
MessageParam[] $params
Definition: MessageValue.php:19
Wikimedia\Message\ParamType\RAW
const RAW
A text parameter which is substituted after formatter processing.
Definition: ParamType.php:57
Wikimedia\Message\MessageValue\longDurationParams
longDurationParams(... $values)
Chainable mutator which adds parameters which are a duration specified in seconds (ParamType::DURATIO...
Definition: MessageValue.php:126
Wikimedia\Message\MessageValue\plaintextParams
plaintextParams(... $values)
Chainable mutator which adds plaintext parameters (ParamType::PLAINTEXT).
Definition: MessageValue.php:200
Wikimedia\Message\MessageValue\shortDurationParams
shortDurationParams(... $values)
Chainable mutator which adds parameters which are a duration specified in seconds (ParamType::DURATIO...
Definition: MessageValue.php:140
Wikimedia\Message\MessageValue\listParamsOfType
listParamsOfType( $listType,... $values)
Chainable mutator which adds list parameters with a common type.
Definition: MessageValue.php:89
Wikimedia\Message\ListParam
Value object representing a message parameter that consists of a list of values.
Definition: ListParam.php:10
Wikimedia\Message\ListType\PIPE
const PIPE
A pipe-separated list.
Definition: ListType.php:17
Wikimedia\Message\MessageValue\__construct
__construct( $key, $params=[])
Definition: MessageValue.php:26
Wikimedia\Message\ParamType\NUM
const NUM
A number, to be formatted using local digits and separators.
Definition: ParamType.php:16
Wikimedia\Message\ParamType\TEXT
const TEXT
A simple text string or another MessageValue, not otherwise formatted.
Definition: ParamType.php:13
Wikimedia\Message\ParamType\DURATION_SHORT
const DURATION_SHORT
A number of seconds, to be formatted as natural language text in an abbreviated way.
Definition: ParamType.php:28
Wikimedia\Message\ListType\AND
const AND
A natural-language list separated by "and".
Definition: ListType.php:20
Wikimedia\Message\MessageValue\semicolonListParams
semicolonListParams(... $values)
Chainable mutator which adds semicolon lists (ListType::SEMICOLON).
Definition: MessageValue.php:228
Wikimedia\Message\MessageValue\expiryParams
expiryParams(... $values)
Chainable mutator which adds parameters which are an expiry timestamp (ParamType::EXPIRY).
Definition: MessageValue.php:151
Wikimedia\Message\ListType\SEMICOLON
const SEMICOLON
A semicolon-separated list.
Definition: ListType.php:14
Wikimedia\Message\MessageValue\rawParams
rawParams(... $values)
Chainable mutator which adds "raw" parameters (ParamType::RAW).
Definition: MessageValue.php:186
Wikimedia\Message\MessageValue\$key
string $key
Definition: MessageValue.php:16
Wikimedia\Message\MessageParam
Value object representing a message parameter that consists of a list of values.
Definition: MessageParam.php:10
$type
$type
Definition: testCompression.php:48