Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.65% covered (success)
95.65%
22 / 23
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
FormLinkFormatter
95.65% covered (success)
95.65%
22 / 23
80.00% covered (warning)
80.00%
4 / 5
7
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
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
 getRepresentations
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 getTitleAttribute
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFragment
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace Wikibase\Lexeme\MediaWiki\EntityLinkFormatters;
4
5use HtmlArmor;
6use MediaWiki\Language\Language;
7use Wikibase\DataModel\Entity\EntityId;
8use Wikibase\DataModel\Services\Lookup\EntityLookup;
9use Wikibase\DataModel\Term\TermList;
10use Wikibase\Lexeme\Domain\Model\FormId;
11use Wikibase\Lexeme\Presentation\Formatters\LexemeTermFormatter;
12use Wikibase\Repo\Hooks\Formatters\DefaultEntityLinkFormatter;
13use Wikibase\Repo\Hooks\Formatters\EntityLinkFormatter;
14use Wikimedia\Assert\Assert;
15
16/**
17 * @license GPL-2.0-or-later
18 */
19class FormLinkFormatter implements EntityLinkFormatter {
20
21    /**
22     * @var EntityLookup
23     */
24    private $entityLookup;
25
26    /**
27     * @var DefaultEntityLinkFormatter
28     */
29    private $linkFormatter;
30
31    /**
32     * @var LexemeTermFormatter
33     */
34    private $representationsFormatter;
35
36    /**
37     * @var Language
38     */
39    private $language;
40
41    public function __construct(
42        EntityLookup $entityLookup,
43        DefaultEntityLinkFormatter $linkFormatter,
44        LexemeTermFormatter $representationsFormatter,
45        Language $language
46    ) {
47        $this->entityLookup = $entityLookup;
48        $this->linkFormatter = $linkFormatter;
49        $this->language = $language;
50        $this->representationsFormatter = $representationsFormatter;
51    }
52
53    public function getHtml( EntityId $entityId, ?array $labelData = null ) {
54        Assert::parameterType( FormId::class, $entityId, '$entityId' );
55        '@phan-var FormId $entityId';
56
57        return $this->linkFormatter->getHtml(
58            $entityId,
59            [
60                'language' => $this->language->getCode(),
61                'value' => new HtmlArmor(
62                    $this->representationsFormatter->format( $this->getRepresentations( $entityId ) )
63                ),
64            ]
65        );
66    }
67
68    /**
69     * @param FormId $formId
70     * @return TermList
71     * @suppress PhanUndeclaredMethod
72     */
73    private function getRepresentations( FormId $formId ): TermList {
74        $form = $this->entityLookup->getEntity( $formId );
75
76        if ( $form === null ) {
77            return new TermList();
78
79        }
80        return $form->getRepresentations();
81    }
82
83    /**
84     * @inheritDoc
85     */
86    public function getTitleAttribute(
87        EntityId $entityId,
88        ?array $labelData = null,
89        ?array $descriptionData = null
90    ) {
91        return $entityId->getSerialization();
92    }
93
94    /**
95     * @param FormId $entityId
96     * @param string $fragment
97     * @return string
98     */
99    public function getFragment( EntityId $entityId, $fragment ) {
100        Assert::parameterType( FormId::class, $entityId, '$entityId' );
101
102        if ( $fragment === $entityId->getSerialization() ) {
103            return $entityId->getIdSuffix();
104        } else {
105            return $fragment;
106        }
107    }
108
109}