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 }
params(... $values)
Chainable mutator which adds text parameters and MessageParam parameters.
const SIZE
A number of bytes.
Definition: ParamType.php:42
const NUM
A number, to be formatted using local digits and separators.
Definition: ParamType.php:16
plaintextParams(... $values)
Chainable mutator which adds plaintext parameters (ParamType::PLAINTEXT).
const PLAINTEXT
A text parameter which is substituted after formatter processing.
Definition: ParamType.php:64
const COMMA
A comma-separated list.
Definition: ListType.php:11
Value object representing a message for i18n.
bitrateParams(... $values)
Chainable mutator which adds parameters which are a number of bits per second (ParamType::BITRATE).
semicolonListParams(... $values)
Chainable mutator which adds semicolon lists (ListType::SEMICOLON).
shortDurationParams(... $values)
Chainable mutator which adds parameters which are a duration specified in seconds (ParamType::DURATIO...
listParamsOfType( $listType,... $values)
Chainable mutator which adds list parameters with a common type.
const EXPIRY
An expiry time.
Definition: ParamType.php:39
textListParams(... $values)
Chainable mutator which adds natural-language lists (ListType::AND).
const AND
A natural-language list separated by "and".
Definition: ListType.php:20
const RAW
A text parameter which is substituted after formatter processing.
Definition: ParamType.php:57
numParams(... $values)
Chainable mutator which adds numeric parameters (ParamType::NUM).
const BITRATE
A number of bits per second.
Definition: ParamType.php:45
const SEMICOLON
A semicolon-separated list.
Definition: ListType.php:14
textParams(... $values)
Chainable mutator which adds parameters of type text (ParamType::TEXT).
sizeParams(... $values)
Chainable mutator which adds parameters which are a number of bytes (ParamType::SIZE).
__construct( $key, $params=[])
textParamsOfType( $type,... $values)
Chainable mutator which adds text parameters with a common type.
rawParams(... $values)
Chainable mutator which adds "raw" parameters (ParamType::RAW).
const TEXT
A simple text string or another MessageValue, not otherwise formatted.
Definition: ParamType.php:13
expiryParams(... $values)
Chainable mutator which adds parameters which are an expiry timestamp (ParamType::EXPIRY).
getKey()
Get the message key.
const PIPE
A pipe-separated list.
Definition: ListType.php:17
dump()
Dump the object for testing/debugging.
getParams()
Get the parameter array.
Value object representing a message parameter that consists of a list of values.
Definition: ListParam.php:10
const DURATION_LONG
A number of seconds, to be formatted as natural language text.
Definition: ParamType.php:22
longDurationParams(... $values)
Chainable mutator which adds parameters which are a duration specified in seconds (ParamType::DURATIO...
Value object representing a message parameter that consists of a list of values.
pipeListParams(... $values)
Chainable mutator which adds pipe lists (ListType::PIPE).
Value object representing a message parameter holding a single value.
Definition: ScalarParam.php:10
const DURATION_SHORT
A number of seconds, to be formatted as natural language text in an abbreviated way.
Definition: ParamType.php:28
commaListParams(... $values)
Chainable mutator which adds comma lists (ListType::COMMA).