Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
LexemeTermFormatter
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
3 / 3
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 format
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 getTermHtml
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Wikibase\Lexeme\Presentation\Formatters;
4
5use MediaWiki\Html\Html;
6use MediaWiki\MediaWikiServices;
7use Wikibase\DataModel\Term\Term;
8use Wikibase\DataModel\Term\TermList;
9
10/**
11 * @license GPL-2.0-or-later
12 */
13class LexemeTermFormatter {
14
15    /** @var string */
16    private $separator;
17
18    public function __construct( $separator ) {
19        $this->separator = $separator;
20    }
21
22    /**
23     * Formats lexeme terms (lemmas, representations) as HTML.
24     * Returns an empty string when given an empty TermList.
25     *
26     * @param TermList $terms
27     *
28     * @return string HTML
29     */
30    public function format( TermList $terms ) {
31        return implode(
32            $this->separator,
33            array_map(
34                [ $this, 'getTermHtml' ],
35                iterator_to_array( $terms->getIterator() )
36            )
37        );
38    }
39
40    private function getTermHtml( Term $term ) {
41        $language = MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( $term->getLanguageCode() );
42
43        return Html::element(
44            'span',
45            [
46                'class' => 'mw-content-' . $language->getDir(),
47                'dir' => $language->getDir(),
48                'lang' => $language->getHtmlCode(),
49            ],
50            $term->getText()
51        );
52    }
53
54}