Fix two problems that made it difficult to convert between Message and MessageValue, or to write code that could accept both of them, as exemplified by the StatusValue class: * Implement a common interface * Use the same internal format for message parameters While these changes should be compatible with most of existing code, where the authors were courteous enough to simply call methods such as Message::numParam() and not look inside the values they return, it is potentially a breaking change for anything that depended on the formatted params being arrays or accessed their keys. Example patches: https://gerrit.wikimedia.org/r/q/topic:message-param Notable changes: * Message and MessageValue now both implement MessageSpecifier (only Message implemented it before). * Message::numParam() and other static methods for encoding params now return MessageParam objects, instead of special arrays. Use these MessageParam objects internally in Message. * Narrow down the return type of MessageSpecifier::getParams() (it was just `array`, allowing any type in the array). Narrow down the types for Message::params() and MessageValue::params() to match. * Deprecate MediaWiki\Message\Converter. As a replacement add MessageValue::newFromSpecifier(), which is analogous to Message::newFromSpecifier(), but without weird legacy edge cases. * Make StatusValue::getMessages() return MessageValues. Remove code that converted between Message and MessageValue, no longer needed. * Update many type declarations and comments to use MessageSpecifier instead of MessageValue, as well as a couple of tests that depended on implementation details. Bug: T358779 Change-Id: I625a48a6ecd3fad5c2ed76b23343a0fef91e1b83
58 lines
1.3 KiB
PHP
58 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Message;
|
|
|
|
use Wikimedia\Message\ITextFormatter;
|
|
use Wikimedia\Message\MessageSpecifier;
|
|
|
|
/**
|
|
* The MediaWiki-specific implementation of ITextFormatter
|
|
*/
|
|
class TextFormatter implements ITextFormatter {
|
|
/** @var string */
|
|
private $langCode;
|
|
|
|
/** @var string */
|
|
private $format;
|
|
|
|
/**
|
|
* Construct a TextFormatter.
|
|
*
|
|
* The type signature may change without notice as dependencies are added
|
|
* to the constructor. External callers should use
|
|
* MediaWikiServices::getMessageFormatterFactory()
|
|
*
|
|
* @internal
|
|
* @param string $langCode
|
|
* @param string $format
|
|
*/
|
|
public function __construct(
|
|
string $langCode,
|
|
string $format = Message::FORMAT_TEXT
|
|
) {
|
|
$this->langCode = $langCode;
|
|
$this->format = $format;
|
|
}
|
|
|
|
public function getLangCode() {
|
|
return $this->langCode;
|
|
}
|
|
|
|
/**
|
|
* Allow the Message class to be mocked in tests by constructing objects in
|
|
* a protected method.
|
|
*
|
|
* @internal
|
|
* @param MessageSpecifier $spec
|
|
* @return Message
|
|
*/
|
|
protected function createMessage( MessageSpecifier $spec ) {
|
|
return Message::newFromSpecifier( $spec );
|
|
}
|
|
|
|
public function format( MessageSpecifier $mv ): string {
|
|
$message = $this->createMessage( $mv );
|
|
$message->inLanguage( $this->langCode );
|
|
return $message->toString( $this->format );
|
|
}
|
|
}
|