MediaWiki  master
ScalarParam.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Wikimedia\Message;
4 
5 use Stringable;
6 
14 class ScalarParam extends MessageParam {
23  public function __construct( $type, $value ) {
24  if ( $type === ParamType::LIST ) {
25  throw new \InvalidArgumentException(
26  'ParamType::LIST cannot be used with ScalarParam; use ListParam instead'
27  );
28  }
29  if ( !is_string( $value ) && !is_numeric( $value ) &&
30  !$value instanceof MessageValue && !$value instanceof Stringable ) {
31  $type = is_object( $value ) ? get_class( $value ) : gettype( $value );
32  throw new \InvalidArgumentException(
33  "Scalar parameter must be a string, number, or MessageValue; got $type"
34  );
35  }
36 
37  $this->type = $type;
38  $this->value = $value;
39  }
40 
41  public function dump() {
42  if ( $this->value instanceof MessageValue ) {
43  $contents = $this->value->dump();
44  } else {
45  $contents = htmlspecialchars( (string)$this->value );
46  }
47  return "<{$this->type}>" . $contents . "</{$this->type}>";
48  }
49 }
Value object representing a message parameter that consists of a list of values.
Value object representing a message for i18n.
const LIST
A list of values.
Definition: ParamType.php:81
Value object representing a message parameter holding a single value.
Definition: ScalarParam.php:14
__construct( $type, $value)
Construct a text parameter.
Definition: ScalarParam.php:23
dump()
Dump the object for testing/debugging.
Definition: ScalarParam.php:41