MediaWiki  1.34.0
TextFormatter.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Message;
4 
10 use Message;
11 
15 class TextFormatter implements ITextFormatter {
17  private $langCode;
18 
28  public function __construct( $langCode ) {
29  $this->langCode = $langCode;
30  }
31 
40  protected function createMessage( $key ) {
41  return new Message( $key );
42  }
43 
44  public function getLangCode() {
45  return $this->langCode;
46  }
47 
48  private function convertParam( MessageParam $param ) {
49  if ( $param instanceof ListParam ) {
50  $convertedElements = [];
51  foreach ( $param->getValue() as $element ) {
52  $convertedElements[] = $this->convertParam( $element );
53  }
54  return Message::listParam( $convertedElements, $param->getListType() );
55  } elseif ( $param instanceof MessageParam ) {
56  $value = $param->getValue();
57  if ( $value instanceof MessageValue ) {
58  $mv = $value;
59  $value = $this->createMessage( $mv->getKey() );
60  foreach ( $mv->getParams() as $mvParam ) {
61  $value->params( $this->convertParam( $mvParam ) );
62  }
63  }
64 
65  if ( $param->getType() === ParamType::TEXT ) {
66  return $value;
67  } else {
68  return [ $param->getType() => $value ];
69  }
70  } else {
71  throw new \InvalidArgumentException( 'Invalid message parameter type' );
72  }
73  }
74 
75  public function format( MessageValue $mv ) {
76  $message = $this->createMessage( $mv->getKey() );
77  foreach ( $mv->getParams() as $param ) {
78  $message->params( $this->convertParam( $param ) );
79  }
80  $message->inLanguage( $this->langCode );
81  return $message->text();
82  }
83 }
Wikimedia\Message\ITextFormatter
Definition: ITextFormatter.php:18
Message
Wikimedia\Message\MessageValue
Value object representing a message for i18n.
Definition: MessageValue.php:14
Message\TextFormatter\$langCode
string $langCode
Definition: TextFormatter.php:17
Message\TextFormatter\__construct
__construct( $langCode)
Construct a TextFormatter.
Definition: TextFormatter.php:28
Message\TextFormatter
The MediaWiki-specific implementation of ITextFormatter.
Definition: TextFormatter.php:15
Message\TextFormatter\convertParam
convertParam(MessageParam $param)
Definition: TextFormatter.php:48
Message\TextFormatter\format
format(MessageValue $mv)
Definition: TextFormatter.php:75
Message\TextFormatter\getLangCode
getLangCode()
Get the internal language code in which format() is.
Definition: TextFormatter.php:44
MediaWiki\Message
Definition: MessageFormatterFactory.php:3
Message\TextFormatter\createMessage
createMessage( $key)
Allow the Message class to be mocked in tests by constructing objects in a protected method.
Definition: TextFormatter.php:40
Wikimedia\Message\ListParam
Value object representing a message parameter that consists of a list of values.
Definition: ListParam.php:10
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
Wikimedia\Message\ParamType
The constants used to specify parameter types.
Definition: ParamType.php:11