MediaWiki  master
MessageValue.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Wikimedia\Message;
4 
16 class MessageValue {
18  private $key;
19 
21  private $params;
22 
30  public function __construct( $key, $params = [] ) {
31  $this->key = $key;
32  $this->params = [];
33  $this->params( ...$params );
34  }
35 
42  public static function new( $key, $params = [] ) {
43  return new MessageValue( $key, $params );
44  }
45 
51  public function getKey() {
52  return $this->key;
53  }
54 
60  public function getParams() {
61  return $this->params;
62  }
63 
70  public function params( ...$values ) {
71  foreach ( $values as $value ) {
72  if ( $value instanceof MessageParam ) {
73  $this->params[] = $value;
74  } else {
75  $this->params[] = new ScalarParam( ParamType::TEXT, $value );
76  }
77  }
78  return $this;
79  }
80 
88  public function textParamsOfType( $type, ...$values ) {
89  foreach ( $values as $value ) {
90  $this->params[] = new ScalarParam( $type, $value );
91  }
92  return $this;
93  }
94 
103  public function listParamsOfType( $listType, ...$values ) {
104  foreach ( $values as $value ) {
105  $this->params[] = new ListParam( $listType, $value );
106  }
107  return $this;
108  }
109 
116  public function textParams( ...$values ) {
117  return $this->textParamsOfType( ParamType::TEXT, ...$values );
118  }
119 
126  public function numParams( ...$values ) {
127  return $this->textParamsOfType( ParamType::NUM, ...$values );
128  }
129 
140  public function longDurationParams( ...$values ) {
141  return $this->textParamsOfType( ParamType::DURATION_LONG, ...$values );
142  }
143 
154  public function shortDurationParams( ...$values ) {
155  return $this->textParamsOfType( ParamType::DURATION_SHORT, ...$values );
156  }
157 
165  public function expiryParams( ...$values ) {
166  return $this->textParamsOfType( ParamType::EXPIRY, ...$values );
167  }
168 
176  public function dateTimeParams( ...$values ) {
177  return $this->textParamsOfType( ParamType::DATETIME, ...$values );
178  }
179 
187  public function dateParams( ...$values ) {
188  return $this->textParamsOfType( ParamType::DATE, ...$values );
189  }
190 
198  public function timeParams( ...$values ) {
199  return $this->textParamsOfType( ParamType::TIME, ...$values );
200  }
201 
209  public function userGroupParams( ...$values ) {
210  return $this->textParamsOfType( ParamType::GROUP, ...$values );
211  }
212 
219  public function sizeParams( ...$values ) {
220  return $this->textParamsOfType( ParamType::SIZE, ...$values );
221  }
222 
230  public function bitrateParams( ...$values ) {
231  return $this->textParamsOfType( ParamType::BITRATE, ...$values );
232  }
233 
244  public function rawParams( ...$values ) {
245  return $this->textParamsOfType( ParamType::RAW, ...$values );
246  }
247 
258  public function plaintextParams( ...$values ) {
259  return $this->textParamsOfType( ParamType::PLAINTEXT, ...$values );
260  }
261 
272  public function commaListParams( ...$values ) {
273  return $this->listParamsOfType( ListType::COMMA, ...$values );
274  }
275 
286  public function semicolonListParams( ...$values ) {
287  return $this->listParamsOfType( ListType::SEMICOLON, ...$values );
288  }
289 
300  public function pipeListParams( ...$values ) {
301  return $this->listParamsOfType( ListType::PIPE, ...$values );
302  }
303 
314  public function textListParams( ...$values ) {
315  return $this->listParamsOfType( ListType::AND, ...$values );
316  }
317 
323  public function dump() {
324  $contents = '';
325  foreach ( $this->params as $param ) {
326  $contents .= $param->dump();
327  }
328  return '<message key="' . htmlspecialchars( $this->key ) . '">' .
329  $contents . '</message>';
330  }
331 }
Wikimedia\Message\ParamType\PLAINTEXT
const PLAINTEXT
A text parameter which is substituted after formatter processing.
Definition: ParamType.php:92
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:219
Wikimedia\Message\MessageValue\pipeListParams
pipeListParams(... $values)
Chainable mutator which adds pipe lists (ListType::PIPE).
Definition: MessageValue.php:300
Wikimedia\Message\MessageValue\textParams
textParams(... $values)
Chainable mutator which adds parameters of type text (ParamType::TEXT).
Definition: MessageValue.php:116
Wikimedia\Message\MessageValue\textListParams
textListParams(... $values)
Chainable mutator which adds natural-language lists (ListType::AND).
Definition: MessageValue.php:314
Wikimedia\Message\MessageValue\getKey
getKey()
Get the message key.
Definition: MessageValue.php:51
Wikimedia\Message\ParamType\SIZE
const SIZE
A number of bytes.
Definition: ParamType.php:70
Wikimedia\Message\MessageValue\commaListParams
commaListParams(... $values)
Chainable mutator which adds comma lists (ListType::COMMA).
Definition: MessageValue.php:272
Wikimedia\Message\ScalarParam
Value object representing a message parameter holding a single value.
Definition: ScalarParam.php:12
Wikimedia\Message\MessageValue\numParams
numParams(... $values)
Chainable mutator which adds numeric parameters (ParamType::NUM).
Definition: MessageValue.php:126
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:16
Wikimedia\Message
Definition: DataMessageValue.php:3
Wikimedia\Message\MessageValue\getParams
getParams()
Get the parameter array.
Definition: MessageValue.php:60
Wikimedia\Message\ParamType\BITRATE
const BITRATE
A number of bits per second.
Definition: ParamType.php:73
Wikimedia\Message\MessageValue\dateParams
dateParams(... $values)
Chainable mutator which adds parameters which are a date timestamp (ParamType::DATE).
Definition: MessageValue.php:187
Wikimedia\Message\MessageValue\dump
dump()
Dump the object for testing/debugging.
Definition: MessageValue.php:323
Wikimedia\Message\MessageValue\bitrateParams
bitrateParams(... $values)
Chainable mutator which adds parameters which are a number of bits per second (ParamType::BITRATE).
Definition: MessageValue.php:230
Wikimedia\Message\MessageValue\textParamsOfType
textParamsOfType( $type,... $values)
Chainable mutator which adds text parameters with a common type.
Definition: MessageValue.php:88
Wikimedia\Message\MessageValue\params
params(... $values)
Chainable mutator which adds text parameters and MessageParam parameters.
Definition: MessageValue.php:70
Wikimedia\Message\ParamType\TIME
const TIME
A time in one of the formats accepted by the Wikimedia\Timestamp library.
Definition: ParamType.php:60
Wikimedia\Message\MessageValue\userGroupParams
userGroupParams(... $values)
Chainable mutator which adds parameters which are a user group (ParamType::GROUP).
Definition: MessageValue.php:209
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:21
Wikimedia\Message\ParamType\RAW
const RAW
A text parameter which is substituted after formatter processing.
Definition: ParamType.php:85
Wikimedia\Message\MessageValue\longDurationParams
longDurationParams(... $values)
Chainable mutator which adds parameters which are a duration specified in seconds (ParamType::DURATIO...
Definition: MessageValue.php:140
Wikimedia\Message\MessageValue\dateTimeParams
dateTimeParams(... $values)
Chainable mutator which adds parameters which are a date-time timestamp (ParamType::DATETIME).
Definition: MessageValue.php:176
Wikimedia\Message\MessageValue\plaintextParams
plaintextParams(... $values)
Chainable mutator which adds plaintext parameters (ParamType::PLAINTEXT).
Definition: MessageValue.php:258
Wikimedia\Message\MessageValue\shortDurationParams
shortDurationParams(... $values)
Chainable mutator which adds parameters which are a duration specified in seconds (ParamType::DURATIO...
Definition: MessageValue.php:154
Wikimedia\Message\ParamType\GROUP
const GROUP
Definition: ParamType.php:67
Wikimedia\Message\ParamType\DATE
const DATE
A date in one of the formats accepted by the Wikimedia\Timestamp library.
Definition: ParamType.php:53
Wikimedia\Message\MessageValue\listParamsOfType
listParamsOfType( $listType,... $values)
Chainable mutator which adds list parameters with a common type.
Definition: MessageValue.php:103
Wikimedia\Message\ListParam
Value object representing a message parameter that consists of a list of values.
Definition: ListParam.php:12
Wikimedia\Message\ListType\PIPE
const PIPE
A pipe-separated list.
Definition: ListType.php:17
Wikimedia\Message\MessageValue\__construct
__construct( $key, $params=[])
Definition: MessageValue.php:30
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\ParamType\DATETIME
const DATETIME
A date time in one of the formats accepted by the Wikimedia\Timestamp library.
Definition: ParamType.php:46
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:286
Wikimedia\Message\MessageValue\expiryParams
expiryParams(... $values)
Chainable mutator which adds parameters which are an expiry timestamp (ParamType::EXPIRY).
Definition: MessageValue.php:165
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:244
Wikimedia\Message\MessageValue\$key
string $key
Definition: MessageValue.php:18
Wikimedia\Message\MessageValue\timeParams
timeParams(... $values)
Chainable mutator which adds parameters which are a time timestamp (ParamType::TIME).
Definition: MessageValue.php:198
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:52