Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.71% covered (warning)
85.71%
6 / 7
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
TextFormatter
85.71% covered (warning)
85.71%
6 / 7
75.00% covered (warning)
75.00%
3 / 4
4.05
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getLangCode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createMessage
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 format
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace MediaWiki\Message;
4
5use Wikimedia\Message\ITextFormatter;
6use Wikimedia\Message\MessageSpecifier;
7
8/**
9 * The MediaWiki-specific implementation of ITextFormatter
10 *
11 * To obtain an instance, use \MediaWiki\MediaWikiServices::getMessageFormatterFactory()
12 * and call MessageFormatterFactory::getTextFormatter.
13 *
14 * @ingroup Language
15 */
16class TextFormatter implements ITextFormatter {
17    /** @var string */
18    private $langCode;
19
20    /** @var string */
21    private $format;
22
23    /**
24     * @internal For use by ServiceWiring only
25     * @param string $langCode
26     * @param string $format
27     */
28    public function __construct(
29        string $langCode,
30        string $format = Message::FORMAT_TEXT
31    ) {
32        $this->langCode = $langCode;
33        $this->format = $format;
34    }
35
36    public function getLangCode(): string {
37        return $this->langCode;
38    }
39
40    /**
41     * Allow the Message class to be mocked in tests by constructing objects in
42     * a protected method.
43     *
44     * @internal
45     * @param MessageSpecifier $spec
46     * @return Message
47     */
48    protected function createMessage( MessageSpecifier $spec ) {
49        return Message::newFromSpecifier( $spec );
50    }
51
52    public function format( MessageSpecifier $mv ): string {
53        $message = $this->createMessage( $mv );
54        $message->inLanguage( $this->langCode );
55        return $message->toString( $this->format );
56    }
57}