MediaWiki master
Converter.php
Go to the documentation of this file.
1<?php
2
4
5use InvalidArgumentException;
12
17class Converter {
18
27 public function createMessage( $key ) {
28 return new Message( $key );
29 }
30
36 public function convertMessage( MessageSpecifier $m ) {
37 $mv = new MessageValue( $m->getKey() );
38 foreach ( $m->getParams() as $param ) {
39 $mv->params( $this->convertParam( $param ) );
40 }
41 return $mv;
42 }
43
49 private function convertParam( $param ) {
50 if ( $param instanceof MessageSpecifier ) {
51 return new ScalarParam( ParamType::TEXT, $this->convertMessage( $param ) );
52 }
53 if ( !is_array( $param ) ) {
54 return new ScalarParam( ParamType::TEXT, $param );
55 }
56
57 if ( isset( $param['list'] ) && isset( $param['type'] ) ) {
58 $convertedElements = [];
59 foreach ( $param['list'] as $element ) {
60 $convertedElements[] = $this->convertParam( $element );
61 }
62 return new ListParam( $param['type'], $convertedElements );
63 }
64
65 foreach ( ParamType::cases() as $type ) {
66 if ( $type !== ParamType::LIST && isset( $param[$type] ) ) {
67 return new ScalarParam( $type, $param[$type] );
68 }
69 }
70
71 throw new InvalidArgumentException( "Unrecognized Message param: " . json_encode( $param ) );
72 }
73
79 public function convertMessageValue( MessageValue $mv ) {
80 $m = $this->createMessage( $mv->getKey() );
81 foreach ( $mv->getParams() as $param ) {
82 $m->params( $this->convertMessageParam( $param ) );
83 }
84 return $m;
85 }
86
92 private function convertMessageParam( MessageParam $param ) {
93 if ( $param instanceof ListParam ) {
94 $convertedElements = [];
95 foreach ( $param->getValue() as $element ) {
96 $convertedElements[] = $this->convertMessageParam( $element );
97 }
98 return Message::listParam( $convertedElements, $param->getListType() );
99 }
100 $value = $param->getValue();
101 if ( $value instanceof MessageValue ) {
102 $value = $this->convertMessageValue( $value );
103 }
104
105 if ( $param->getType() === ParamType::TEXT ) {
106 return $value;
107 }
108 return [ $param->getType() => $value ];
109 }
110
111}
Converter between Message and MessageValue.
Definition Converter.php:17
convertMessage(MessageSpecifier $m)
Convert a Message to a MessageValue.
Definition Converter.php:36
createMessage( $key)
Allow the Message class to be mocked in tests by constructing objects in a protected method.
Definition Converter.php:27
convertMessageValue(MessageValue $mv)
Convert a MessageValue to a Message.
Definition Converter.php:79
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:150
static listParam(array $list, $type='text')
Definition Message.php:1341
Value object representing a message parameter that consists of a list of values.
Definition ListParam.php:15
Value object representing a message parameter that consists of a list of values.
getValue()
Get the input value of the parameter.
getType()
Get the type of the parameter.
Value object representing a message for i18n.
getKey()
Get the message key.
getParams()
Get the parameter array.
The constants used to specify parameter types.
Definition ParamType.php:11
Value object representing a message parameter holding a single value.
getParams()
Returns the message parameters.
getKey()
Returns the message key.