Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.44% covered (success)
94.44%
17 / 18
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
LexemeLinkFormatter
94.44% covered (success)
94.44%
17 / 18
75.00% covered (warning)
75.00%
3 / 4
4.00
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 getHtml
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 getTitleAttribute
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getFragment
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3declare( strict_types = 1 );
4
5namespace Wikibase\Lexeme\MediaWiki\EntityLinkFormatters;
6
7use HtmlArmor;
8use Language;
9use Wikibase\DataModel\Entity\EntityId;
10use Wikibase\Lexeme\DataAccess\Store\LemmaLookup;
11use Wikibase\Lexeme\Domain\Model\LexemeId;
12use Wikibase\Lexeme\Presentation\Formatters\LexemeTermFormatter;
13use Wikibase\Lib\Store\EntityTitleTextLookup;
14use Wikibase\Repo\Hooks\Formatters\DefaultEntityLinkFormatter;
15use Wikibase\Repo\Hooks\Formatters\EntityLinkFormatter;
16use Wikimedia\Assert\Assert;
17
18/**
19 * @license GPL-2.0-or-later
20 */
21class LexemeLinkFormatter implements EntityLinkFormatter {
22
23    private LemmaLookup $lemmaLookup;
24
25    private DefaultEntityLinkFormatter $linkFormatter;
26
27    private Language $language;
28
29    private LexemeTermFormatter $lemmaFormatter;
30
31    private EntityTitleTextLookup $entityTitleTextLookup;
32
33    public function __construct(
34        EntityTitleTextLookup $entityTitleTextLookup,
35        LemmaLookup $lemmaLookup,
36        EntityLinkFormatter $linkFormatter,
37        LexemeTermFormatter $lemmaFormatter,
38        Language $language
39    ) {
40        $this->entityTitleTextLookup = $entityTitleTextLookup;
41        $this->lemmaLookup = $lemmaLookup;
42        $this->linkFormatter = $linkFormatter;
43        $this->lemmaFormatter = $lemmaFormatter;
44        $this->language = $language;
45    }
46
47    /**
48     * @inheritDoc
49     */
50    public function getHtml( EntityId $entityId, array $labelData = null ): string {
51        Assert::parameterType( LexemeId::class, $entityId, '$entityId' );
52        '@phan-var LexemeId $entityId';
53
54        return $this->linkFormatter->getHtml(
55            $entityId,
56            [
57                'language' => $this->language->getCode(),
58                'value' => new HtmlArmor(
59                    $this->lemmaFormatter->format( $this->lemmaLookup->getLemmas( $entityId ) )
60                ),
61            ]
62        );
63    }
64
65    /**
66     * @inheritDoc
67     */
68    public function getTitleAttribute(
69        EntityId $entityId,
70        array $labelData = null,
71        array $descriptionData = null
72    ): string {
73        // TODO Can't this use $entityId->getSerialization() directly?
74        //      It may have only used the Title text for historical reasons.
75        return $this->entityTitleTextLookup->getPrefixedText( $entityId )
76            ?? $entityId->getSerialization();
77    }
78
79    public function getFragment( EntityId $entityId, $fragment ): string {
80        return $fragment;
81    }
82
83}