2019-10-18 19:32:48 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Message;
|
|
|
|
|
|
|
|
|
|
use InvalidArgumentException;
|
|
|
|
|
use Wikimedia\Message\ListParam;
|
|
|
|
|
use Wikimedia\Message\MessageParam;
|
2024-07-28 12:06:24 +00:00
|
|
|
use Wikimedia\Message\MessageSpecifier;
|
2019-10-18 19:32:48 +00:00
|
|
|
use Wikimedia\Message\MessageValue;
|
|
|
|
|
use Wikimedia\Message\ParamType;
|
|
|
|
|
use Wikimedia\Message\ScalarParam;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Converter between Message and MessageValue
|
|
|
|
|
* @since 1.35
|
|
|
|
|
*/
|
|
|
|
|
class Converter {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
2024-03-08 16:57:29 +00:00
|
|
|
* @param MessageSpecifier $m
|
2019-10-18 19:32:48 +00:00
|
|
|
* @return MessageValue
|
|
|
|
|
*/
|
2024-03-08 16:57:29 +00:00
|
|
|
public function convertMessage( MessageSpecifier $m ) {
|
2019-10-18 19:32:48 +00:00
|
|
|
$mv = new MessageValue( $m->getKey() );
|
|
|
|
|
foreach ( $m->getParams() as $param ) {
|
|
|
|
|
$mv->params( $this->convertParam( $param ) );
|
|
|
|
|
}
|
|
|
|
|
return $mv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert a Message parameter to a MessageParam
|
2020-01-09 23:38:41 +00:00
|
|
|
* @param array|string|int $param
|
2019-10-18 19:32:48 +00:00
|
|
|
* @return MessageParam
|
|
|
|
|
*/
|
|
|
|
|
private function convertParam( $param ) {
|
2024-03-08 16:57:29 +00:00
|
|
|
if ( $param instanceof MessageSpecifier ) {
|
2019-10-18 19:32:48 +00:00
|
|
|
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 );
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-21 04:37:57 +00:00
|
|
|
foreach ( ParamType::cases() as $type ) {
|
2019-10-18 19:32:48 +00:00
|
|
|
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 ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|