MediaWiki REL1_34
TextFormatter.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Message;
4
10use Message;
11
15class 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}
The MediaWiki-specific implementation of ITextFormatter.
__construct( $langCode)
Construct a TextFormatter.
createMessage( $key)
Allow the Message class to be mocked in tests by constructing objects in a protected method.
getLangCode()
Get the internal language code in which format() is.
convertParam(MessageParam $param)
format(MessageValue $mv)
Convert a MessageValue to text.
The Message class provides methods which fulfil two basic services:
Definition Message.php:162
static listParam(array $list, $type='text')
Definition Message.php:1115
Value object representing a message parameter that consists of a list of values.
Definition ListParam.php:10
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
const TEXT
A simple text string or another MessageValue, not otherwise formatted.
Definition ParamType.php:13