blob: da7cc09a1d99cbf1d3908683eaf5c45565115b8a (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
<?php
namespace MediaWiki\Message;
use InvalidArgumentException;
use Message;
use ReflectionClass;
use Wikimedia\Message\ListParam;
use Wikimedia\Message\MessageParam;
use Wikimedia\Message\MessageValue;
use Wikimedia\Message\ParamType;
use Wikimedia\Message\ScalarParam;
/**
* Converter between Message and MessageValue
* @since 1.35
*/
class Converter {
/** @var string[]|null ParamType constants */
private static $constants = null;
/**
* Return the ParamType constants
* @return string[]
*/
private static function getTypes() {
if ( self::$constants === null ) {
$rc = new ReflectionClass( ParamType::class );
self::$constants = array_values( $rc->getConstants() );
}
return self::$constants;
}
/**
* Allow the Message class to be mocked in tests by constructing objects in
* a protected method.
*
* @internal
* @param string $key
* @return Message
*/
public function createMessage( $key ) {
return new Message( $key );
}
/**
* Convert a Message to a MessageValue
* @param Message $m
* @return MessageValue
*/
public function convertMessage( Message $m ) {
$mv = new MessageValue( $m->getKey() );
foreach ( $m->getParams() as $param ) {
$mv->params( $this->convertParam( $param ) );
}
return $mv;
}
/**
* Convert a Message parameter to a MessageParam
* @param array|string|int $param
* @return MessageParam
*/
private function convertParam( $param ) {
if ( $param instanceof Message ) {
return new ScalarParam( ParamType::TEXT, $this->convertMessage( $param ) );
}
if ( !is_array( $param ) ) {
return new ScalarParam( ParamType::TEXT, $param );
}
if ( isset( $param['list'] ) && isset( $param['type'] ) ) {
$convertedElements = [];
foreach ( $param['list'] as $element ) {
$convertedElements[] = $this->convertParam( $element );
}
return new ListParam( $param['type'], $convertedElements );
}
foreach ( self::getTypes() as $type ) {
if ( $type !== ParamType::LIST && isset( $param[$type] ) ) {
return new ScalarParam( $type, $param[$type] );
}
}
throw new InvalidArgumentException( "Unrecognized Message param: " . json_encode( $param ) );
}
/**
* Convert a MessageValue to a Message
* @param MessageValue $mv
* @return Message
*/
public function convertMessageValue( MessageValue $mv ) {
$m = $this->createMessage( $mv->getKey() );
foreach ( $mv->getParams() as $param ) {
$m->params( $this->convertMessageParam( $param ) );
}
return $m;
}
/**
* Convert a MessageParam to a Message parameter
* @param MessageParam $param
* @return array|string|int
*/
private function convertMessageParam( MessageParam $param ) {
if ( $param instanceof ListParam ) {
$convertedElements = [];
foreach ( $param->getValue() as $element ) {
$convertedElements[] = $this->convertMessageParam( $element );
}
return Message::listParam( $convertedElements, $param->getListType() );
}
$value = $param->getValue();
if ( $value instanceof MessageValue ) {
$value = $this->convertMessageValue( $value );
}
if ( $param->getType() === ParamType::TEXT ) {
return $value;
}
return [ $param->getType() => $value ];
}
}
|