Debian Codegraph @

includes/Message/TextFormatter.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Message;
4
5 use Wikimedia\Message\ITextFormatter;
6 use Wikimedia\Message\MessageSpecifier;
7
8 /**
9 * The MediaWiki-specific implementation of ITextFormatter
10 */
11 class TextFormatter implements ITextFormatter {
12 /** @var string */
13 private $langCode;
14
15 /** @var string */
16 private $format;
17
18 /**
19 * Construct a TextFormatter.
20 *
21 * The type signature may change without notice as dependencies are added
22 * to the constructor. External callers should use
23 * MediaWikiServices::getMessageFormatterFactory()
24 *
25 * @internal
26 * @param string $langCode
27 * @param string $format
28 */
29 public function __construct(
30 string $langCode,
31 string $format = Message::FORMAT_TEXT
32 ) {
33 $this->langCode = $langCode;
34 $this->format = $format;
35 }
36
37 public function getLangCode() {
38 return $this->langCode;
39 }
40
41 /**
42 * Allow the Message class to be mocked in tests by constructing objects in
43 * a protected method.
44 *
45 * @internal
46 * @param MessageSpecifier $spec
47 * @return Message
48 */
49 protected function createMessage( MessageSpecifier $spec ) {
50 return Message::newFromSpecifier( $spec );
51 }
52
53 public function format( MessageSpecifier $mv ): string {
54 $message = $this->createMessage( $mv );
55 $message->inLanguage( $this->langCode );
56 return $message->toString( $this->format );
57 }
58 }