MediaWiki  master
Converter.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Message;
4 
5 use InvalidArgumentException;
6 use Message;
7 use ReflectionClass;
13 
18 class Converter {
19 
21  private static $constants = null;
22 
27  private static function getTypes() {
28  if ( self::$constants === null ) {
29  $rc = new ReflectionClass( ParamType::class );
30  self::$constants = array_values( $rc->getConstants() );
31  }
32 
33  return self::$constants;
34  }
35 
44  public function createMessage( $key ) {
45  return new Message( $key );
46  }
47 
53  public function convertMessage( Message $m ) {
54  $mv = new MessageValue( $m->getKey() );
55  foreach ( $m->getParams() as $param ) {
56  $mv->params( $this->convertParam( $param ) );
57  }
58  return $mv;
59  }
60 
66  private function convertParam( $param ) {
67  if ( $param instanceof Message ) {
68  return new ScalarParam( ParamType::TEXT, $this->convertMessage( $param ) );
69  }
70  if ( !is_array( $param ) ) {
71  return new ScalarParam( ParamType::TEXT, $param );
72  }
73 
74  if ( isset( $param['list'] ) && isset( $param['type'] ) ) {
75  $convertedElements = [];
76  foreach ( $param['list'] as $element ) {
77  $convertedElements[] = $this->convertParam( $element );
78  }
79  return new ListParam( $param['type'], $convertedElements );
80  }
81 
82  foreach ( self::getTypes() as $type ) {
83  if ( $type !== ParamType::LIST && isset( $param[$type] ) ) {
84  return new ScalarParam( $type, $param[$type] );
85  }
86  }
87 
88  throw new InvalidArgumentException( "Unrecognized Message param: " . json_encode( $param ) );
89  }
90 
96  public function convertMessageValue( MessageValue $mv ) {
97  $m = $this->createMessage( $mv->getKey() );
98  foreach ( $mv->getParams() as $param ) {
99  $m->params( $this->convertMessageParam( $param ) );
100  }
101  return $m;
102  }
103 
109  private function convertMessageParam( MessageParam $param ) {
110  if ( $param instanceof ListParam ) {
111  $convertedElements = [];
112  foreach ( $param->getValue() as $element ) {
113  $convertedElements[] = $this->convertMessageParam( $element );
114  }
115  return Message::listParam( $convertedElements, $param->getListType() );
116  }
117  $value = $param->getValue();
118  if ( $value instanceof MessageValue ) {
119  $value = $this->convertMessageValue( $value );
120  }
121 
122  if ( $param->getType() === ParamType::TEXT ) {
123  return $value;
124  }
125  return [ $param->getType() => $value ];
126  }
127 
128 }
Converter between Message and MessageValue.
Definition: Converter.php:18
createMessage( $key)
Allow the Message class to be mocked in tests by constructing objects in a protected method.
Definition: Converter.php:44
convertMessage(Message $m)
Convert a Message to a MessageValue.
Definition: Converter.php:53
convertMessageValue(MessageValue $mv)
Convert a MessageValue to a Message.
Definition: Converter.php:96
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition: Message.php:144
static listParam(array $list, $type='text')
Definition: Message.php:1278
getParams()
Returns the message parameters.
Definition: Message.php:375
string $key
The message key.
Definition: Message.php:186
getKey()
Returns the message key.
Definition: Message.php:364
Value object representing a message parameter that consists of a list of values.
Definition: ListParam.php:12
Value object representing a message parameter that consists of a list of values.
Value object representing a message for i18n.
The constants used to specify parameter types.
Definition: ParamType.php:11
const LIST
A list of values.
Definition: ParamType.php:81
const TEXT
A simple text string or another MessageValue, not otherwise formatted.
Definition: ParamType.php:13
Value object representing a message parameter holding a single value.
Definition: ScalarParam.php:14