MediaWiki  master
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 
38  public static function new( $key, $params = [] ) {
39  return new MessageValue( $key, $params );
40  }
41 
47  public function getKey() {
48  return $this->key;
49  }
50 
56  public function getParams() {
57  return $this->params;
58  }
59 
66  public function params( ...$values ) {
67  foreach ( $values as $value ) {
68  if ( $value instanceof MessageParam ) {
69  $this->params[] = $value;
70  } else {
71  $this->params[] = new ScalarParam( ParamType::TEXT, $value );
72  }
73  }
74  return $this;
75  }
76 
84  public function textParamsOfType( $type, ...$values ) {
85  foreach ( $values as $value ) {
86  $this->params[] = new ScalarParam( $type, $value );
87  }
88  return $this;
89  }
90 
99  public function listParamsOfType( $listType, ...$values ) {
100  foreach ( $values as $value ) {
101  $this->params[] = new ListParam( $listType, $value );
102  }
103  return $this;
104  }
105 
112  public function textParams( ...$values ) {
113  return $this->textParamsOfType( ParamType::TEXT, ...$values );
114  }
115 
122  public function numParams( ...$values ) {
123  return $this->textParamsOfType( ParamType::NUM, ...$values );
124  }
125 
136  public function longDurationParams( ...$values ) {
137  return $this->textParamsOfType( ParamType::DURATION_LONG, ...$values );
138  }
139 
150  public function shortDurationParams( ...$values ) {
151  return $this->textParamsOfType( ParamType::DURATION_SHORT, ...$values );
152  }
153 
161  public function expiryParams( ...$values ) {
162  return $this->textParamsOfType( ParamType::EXPIRY, ...$values );
163  }
164 
171  public function sizeParams( ...$values ) {
172  return $this->textParamsOfType( ParamType::SIZE, ...$values );
173  }
174 
182  public function bitrateParams( ...$values ) {
183  return $this->textParamsOfType( ParamType::BITRATE, ...$values );
184  }
185 
196  public function rawParams( ...$values ) {
197  return $this->textParamsOfType( ParamType::RAW, ...$values );
198  }
199 
210  public function plaintextParams( ...$values ) {
211  return $this->textParamsOfType( ParamType::PLAINTEXT, ...$values );
212  }
213 
224  public function commaListParams( ...$values ) {
225  return $this->listParamsOfType( ListType::COMMA, ...$values );
226  }
227 
238  public function semicolonListParams( ...$values ) {
239  return $this->listParamsOfType( ListType::SEMICOLON, ...$values );
240  }
241 
252  public function pipeListParams( ...$values ) {
253  return $this->listParamsOfType( ListType::PIPE, ...$values );
254  }
255 
266  public function textListParams( ...$values ) {
267  return $this->listParamsOfType( ListType::AND, ...$values );
268  }
269 
275  public function dump() {
276  $contents = '';
277  foreach ( $this->params as $param ) {
278  $contents .= $param->dump();
279  }
280  return '<message key="' . htmlspecialchars( $this->key ) . '">' .
281  $contents . '</message>';
282  }
283 }
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:171
Wikimedia\Message\MessageValue\pipeListParams
pipeListParams(... $values)
Chainable mutator which adds pipe lists (ListType::PIPE).
Definition: MessageValue.php:252
Wikimedia\Message\MessageValue\textParams
textParams(... $values)
Chainable mutator which adds parameters of type text (ParamType::TEXT).
Definition: MessageValue.php:112
Wikimedia\Message\MessageValue\textListParams
textListParams(... $values)
Chainable mutator which adds natural-language lists (ListType::AND).
Definition: MessageValue.php:266
Wikimedia\Message\MessageValue\getKey
getKey()
Get the message key.
Definition: MessageValue.php:47
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:224
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:122
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: DataMessageValue.php:3
Wikimedia\Message\MessageValue\getParams
getParams()
Get the parameter array.
Definition: MessageValue.php:56
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:275
Wikimedia\Message\MessageValue\bitrateParams
bitrateParams(... $values)
Chainable mutator which adds parameters which are a number of bits per second (ParamType::BITRATE).
Definition: MessageValue.php:182
Wikimedia\Message\MessageValue\textParamsOfType
textParamsOfType( $type,... $values)
Chainable mutator which adds text parameters with a common type.
Definition: MessageValue.php:84
Wikimedia\Message\MessageValue\params
params(... $values)
Chainable mutator which adds text parameters and MessageParam parameters.
Definition: MessageValue.php:66
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:136
Wikimedia\Message\MessageValue\plaintextParams
plaintextParams(... $values)
Chainable mutator which adds plaintext parameters (ParamType::PLAINTEXT).
Definition: MessageValue.php:210
Wikimedia\Message\MessageValue\shortDurationParams
shortDurationParams(... $values)
Chainable mutator which adds parameters which are a duration specified in seconds (ParamType::DURATIO...
Definition: MessageValue.php:150
Wikimedia\Message\MessageValue\listParamsOfType
listParamsOfType( $listType,... $values)
Chainable mutator which adds list parameters with a common type.
Definition: MessageValue.php:99
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:238
Wikimedia\Message\MessageValue\expiryParams
expiryParams(... $values)
Chainable mutator which adds parameters which are an expiry timestamp (ParamType::EXPIRY).
Definition: MessageValue.php:161
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:196
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:52