3 namespace Wikimedia\Message
;
6 * A MessageValue holds a key and an array of parameters
12 /** @var MessageParam[] */
17 * @param array $params Each element of the parameter array
18 * may be either a MessageParam or a scalar. If it is a scalar, it is
19 * converted to a parameter of type TEXT.
21 public function __construct( $key, $params = [] ) {
24 $this->params( ...$params );
32 public function getKey() {
37 * Get the parameter array
39 * @return MessageParam[]
41 public function getParams() {
46 * Chainable mutator which adds text parameters and MessageParam parameters
48 * @param mixed ...$values Scalar or MessageParam values
49 * @return MessageValue
51 public function params( ...$values ) {
52 foreach ( $values as $value ) {
53 if ( $value instanceof MessageParam
) {
54 $this->params
[] = $value;
56 $this->params
[] = new TextParam( ParamType
::TEXT
, $value );
63 * Chainable mutator which adds text parameters with a common type
65 * @param string $type One of the ParamType constants
66 * @param mixed ...$values Scalar values
67 * @return MessageValue
69 public function textParamsOfType( $type, ...$values ) {
70 foreach ( $values as $value ) {
71 $this->params
[] = new TextParam( $type, $value );
77 * Chainable mutator which adds list parameters with a common type
79 * @param string $listType One of the ListType constants
80 * @param array ...$values Each value should be an array of list items.
81 * @return MessageValue
83 public function listParamsOfType( $listType, ...$values ) {
84 foreach ( $values as $value ) {
85 $this->params
[] = new ListParam( $listType, $value );
91 * Chainable mutator which adds parameters of type text.
93 * @param string ...$values
94 * @return MessageValue
96 public function textParams( ...$values ) {
97 return $this->textParamsOfType( ParamType
::TEXT
, ...$values );
101 * Chainable mutator which adds numeric parameters
103 * @param mixed ...$values
104 * @return MessageValue
106 public function numParams( ...$values ) {
107 return $this->textParamsOfType( ParamType
::NUM
, ...$values );
111 * Chainable mutator which adds parameters which are a duration specified
112 * in seconds. This is similar to timePeriodParams() except that the result
113 * will be more verbose.
115 * @param int|float ...$values
116 * @return MessageValue
118 public function longDurationParams( ...$values ) {
119 return $this->textParamsOfType( ParamType
::DURATION_LONG
, ...$values );
123 * Chainable mutator which adds parameters which are a time period in seconds.
124 * This is similar to durationParams() except that the result will be more
127 * @param int|float ...$values
128 * @return MessageValue
130 public function shortDurationParams( ...$values ) {
131 return $this->textParamsOfType( ParamType
::DURATION_SHORT
, ...$values );
135 * Chainable mutator which adds parameters which are an expiry timestamp
136 * as used in the MediaWiki database schema.
138 * @param string ...$values
139 * @return MessageValue
141 public function expiryParams( ...$values ) {
142 return $this->textParamsOfType( ParamType
::EXPIRY
, ...$values );
146 * Chainable mutator which adds parameters which are a number of bytes.
148 * @param int ...$values
149 * @return MessageValue
151 public function sizeParams( ...$values ) {
152 return $this->textParamsOfType( ParamType
::SIZE
, ...$values );
156 * Chainable mutator which adds parameters which are a number of bits per
159 * @param int|float ...$values
160 * @return MessageValue
162 public function bitrateParams( ...$values ) {
163 return $this->textParamsOfType( ParamType
::BITRATE
, ...$values );
167 * Chainable mutator which adds parameters of type "raw".
169 * @param mixed ...$values
170 * @return MessageValue
172 public function rawParams( ...$values ) {
173 return $this->textParamsOfType( ParamType
::RAW
, ...$values );
177 * Chainable mutator which adds parameters of type "plaintext".
179 public function plaintextParams( ...$values ) {
180 return $this->textParamsOfType( ParamType
::PLAINTEXT
, ...$values );
184 * Chainable mutator which adds comma lists. Each comma list is an array of
185 * list elements, and each list element is either a MessageParam or a
186 * string. String parameters are converted to parameters of type "text".
188 * The list parameters thus created are formatted as a comma-separated list,
189 * or some local equivalent.
191 * @param (MessageParam|string)[] ...$values
192 * @return MessageValue
194 public function commaListParams( ...$values ) {
195 return $this->listParamsOfType( ListType
::COMMA
, ...$values );
199 * Chainable mutator which adds semicolon lists. Each semicolon list is an
200 * array of list elements, and each list element is either a MessageParam
201 * or a string. String parameters are converted to parameters of type
204 * The list parameters thus created are formatted as a semicolon-separated
205 * list, or some local equivalent.
207 * @param (MessageParam|string)[] ...$values
208 * @return MessageValue
210 public function semicolonListParams( ...$values ) {
211 return $this->listParamsOfType( ListType
::SEMICOLON
, ...$values );
215 * Chainable mutator which adds pipe lists. Each pipe list is an array of
216 * list elements, and each list element is either a MessageParam or a
217 * string. String parameters are converted to parameters of type "text".
219 * The list parameters thus created are formatted as a pipe ("|") -separated
220 * list, or some local equivalent.
222 * @param (MessageParam|string)[] ...$values
223 * @return MessageValue
225 public function pipeListParams( ...$values ) {
226 return $this->listParamsOfType( ListType
::PIPE
, ...$values );
230 * Chainable mutator which adds text lists. Each text list is an array of
231 * list elements, and each list element is either a MessageParam or a
232 * string. String parameters are converted to parameters of type "text".
234 * The list parameters thus created, when formatted, are joined as in natural
235 * language. In English, this means a comma-separated list, with the last
236 * two elements joined with "and".
238 * @param (MessageParam|string)[] ...$values
239 * @return MessageValue
241 public function textListParams( ...$values ) {
242 return $this->listParamsOfType( ListType
::AND, ...$values );
246 * Dump the object for testing/debugging
250 public function dump() {
252 foreach ( $this->params
as $param ) {
253 $contents .= $param->dump();
255 return '<message key="' . htmlspecialchars( $this->key
) . '">' .
256 $contents . '</message>';