MediaWiki master
Converter.php
Go to the documentation of this file.
1<?php
2
4
5use InvalidArgumentException;
7use ReflectionClass;
13
18class 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( MessageSpecifier $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 MessageSpecifier ) {
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
convertMessage(MessageSpecifier $m)
Convert a Message to a MessageValue.
Definition Converter.php:53
createMessage( $key)
Allow the Message class to be mocked in tests by constructing objects in a protected method.
Definition Converter.php:44
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:158
static listParam(array $list, $type='text')
Definition Message.php:1338
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.
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.