aboutsummaryrefslogtreecommitdiffstats
path: root/includes/Message
diff options
context:
space:
mode:
authorTim Starling <tstarling@wikimedia.org>2019-07-15 20:24:38 +1000
committerTim Starling <tstarling@wikimedia.org>2019-08-28 12:28:05 +1000
commit09cd8eb0807ee7b0a94c674766e2ea201e5d071b (patch)
tree50cefe6e78377459c32e1fadbc6b56fc1fed0800 /includes/Message
parent3bb3c8b0ae092b2e667f6f33abfc98f9c205d53c (diff)
downloadmediawikicore-09cd8eb0807ee7b0a94c674766e2ea201e5d071b.tar.gz
mediawikicore-09cd8eb0807ee7b0a94c674766e2ea201e5d071b.zip
MessageFormatterFactory
An injectable service interface for message formatting, somewhat narrowed compared to Message. Only the text format is implemented in this framework so far, with getTextFormatter() returning a formatter that converts to the text format. Other formatters could be added to MessageFormatterFactory. Bug: T226598 Change-Id: Id053074c1dbcb692e8309fdca602f94a385bca0c
Diffstat (limited to 'includes/Message')
-rw-r--r--includes/Message/MessageFormatterFactory.php29
-rw-r--r--includes/Message/TextFormatter.php74
2 files changed, 103 insertions, 0 deletions
diff --git a/includes/Message/MessageFormatterFactory.php b/includes/Message/MessageFormatterFactory.php
new file mode 100644
index 000000000000..101224a6a82e
--- /dev/null
+++ b/includes/Message/MessageFormatterFactory.php
@@ -0,0 +1,29 @@
+<?php
+
+namespace MediaWiki\Message;
+
+use Wikimedia\Message\IMessageFormatterFactory;
+use Wikimedia\Message\ITextFormatter;
+
+/**
+ * The MediaWiki-specific implementation of IMessageFormatterFactory
+ */
+class MessageFormatterFactory implements IMessageFormatterFactory {
+ private $textFormatters = [];
+
+ /**
+ * Required parameters may be added to this function without deprecation.
+ * External callers should use MediaWikiServices::getMessageFormatterFactory().
+ *
+ * @internal
+ */
+ public function __construct() {
+ }
+
+ public function getTextFormatter( $langCode ): ITextFormatter {
+ if ( !isset( $this->textFormatters[$langCode] ) ) {
+ $this->textFormatters[$langCode] = new TextFormatter( $langCode );
+ }
+ return $this->textFormatters[$langCode];
+ }
+}
diff --git a/includes/Message/TextFormatter.php b/includes/Message/TextFormatter.php
new file mode 100644
index 000000000000..f5eeb16f2473
--- /dev/null
+++ b/includes/Message/TextFormatter.php
@@ -0,0 +1,74 @@
+<?php
+
+namespace MediaWiki\Message;
+
+use Wikimedia\Message\ITextFormatter;
+use Wikimedia\Message\ListParam;
+use Wikimedia\Message\MessageParam;
+use Wikimedia\Message\MessageValue;
+use Wikimedia\Message\ParamType;
+use Message;
+
+/**
+ * The MediaWiki-specific implementation of ITextFormatter
+ */
+class TextFormatter implements ITextFormatter {
+ /** @var string */
+ private $langCode;
+
+ /**
+ * Construct a TextFormatter.
+ *
+ * The type signature may change without notice as dependencies are added
+ * to the constructor. External callers should use
+ * MediaWikiServices::getMessageFormatterFactory()
+ *
+ * @internal
+ */
+ public function __construct( $langCode ) {
+ $this->langCode = $langCode;
+ }
+
+ /**
+ * Allow the Message class to be mocked in tests by constructing objects in
+ * a protected method.
+ *
+ * @internal
+ * @param string $key
+ * @return Message
+ */
+ protected function createMessage( $key ) {
+ return new Message( $key );
+ }
+
+ public function getLangCode() {
+ return $this->langCode;
+ }
+
+ private static function convertParam( MessageParam $param ) {
+ if ( $param instanceof ListParam ) {
+ $convertedElements = [];
+ foreach ( $param->getValue() as $element ) {
+ $convertedElements[] = self::convertParam( $element );
+ }
+ return Message::listParam( $convertedElements, $param->getListType() );
+ } elseif ( $param instanceof MessageParam ) {
+ if ( $param->getType() === ParamType::TEXT ) {
+ return $param->getValue();
+ } else {
+ return [ $param->getType() => $param->getValue() ];
+ }
+ } else {
+ throw new \InvalidArgumentException( 'Invalid message parameter type' );
+ }
+ }
+
+ public function format( MessageValue $mv ) {
+ $message = $this->createMessage( $mv->getKey() );
+ foreach ( $mv->getParams() as $param ) {
+ $message->params( self::convertParam( $param ) );
+ }
+ $message->inLanguage( $this->langCode );
+ return $message->text();
+ }
+}