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 }
Message\getParams
getParams()
Returns the message parameters.
Definition: Message.php:376
Wikimedia\Message\ScalarParam
Value object representing a message parameter holding a single value.
Definition: ScalarParam.php:10
Message\Converter\convertMessageParam
convertMessageParam(MessageParam $param)
Convert a MessageParam to a Message parameter.
Definition: Converter.php:109
Message\listParam
static listParam(array $list, $type='text')
Definition: Message.php:1123
Wikimedia\Message\MessageValue
Value object representing a message for i18n.
Definition: MessageValue.php:14
Message\Converter\convertMessage
convertMessage(Message $m)
Convert a Message to a MessageValue.
Definition: Converter.php:53
Message\getKey
getKey()
Returns the message key.
Definition: Message.php:365
Message\Converter\createMessage
createMessage( $key)
Allow the Message class to be mocked in tests by constructing objects in a protected method.
Definition: Converter.php:44
Message\Converter
Converter between Message and MessageValue.
Definition: Converter.php:18
MediaWiki\Message
Definition: Converter.php:3
Message\Converter\$constants
static string[] null $constants
ParamType constants.
Definition: Converter.php:21
Wikimedia\Message\ListParam
Value object representing a message parameter that consists of a list of values.
Definition: ListParam.php:10
Wikimedia\Message\ParamType\LIST
const LIST
A list of values.
Definition: ParamType.php:48
Message\Converter\convertParam
convertParam( $param)
Convert a Message parameter to a MessageParam.
Definition: Converter.php:66
Message\Converter\convertMessageValue
convertMessageValue(MessageValue $mv)
Convert a MessageValue to a Message.
Definition: Converter.php:96
Message
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition: Message.php:160
Wikimedia\Message\ParamType\TEXT
const TEXT
A simple text string or another MessageValue, not otherwise formatted.
Definition: ParamType.php:13
Wikimedia\Message\MessageParam
Value object representing a message parameter that consists of a list of values.
Definition: MessageParam.php:10
Message\Converter\getTypes
static getTypes()
Return the ParamType constants.
Definition: Converter.php:27
Wikimedia\Message\ParamType
The constants used to specify parameter types.
Definition: ParamType.php:11
$type
$type
Definition: testCompression.php:52