Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
93.55% |
29 / 31 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
SenseIdTextFormatter | |
93.55% |
29 / 31 |
|
50.00% |
1 / 2 |
7.01 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
formatEntityId | |
92.86% |
26 / 28 |
|
0.00% |
0 / 1 |
6.01 |
1 | <?php |
2 | |
3 | declare( strict_types = 1 ); |
4 | |
5 | namespace Wikibase\Lexeme\Presentation\Formatters; |
6 | |
7 | use InvalidArgumentException; |
8 | use OutOfBoundsException; |
9 | use OutOfRangeException; |
10 | use Wikibase\DataModel\Entity\EntityId; |
11 | use Wikibase\DataModel\Services\EntityId\EntityIdFormatter; |
12 | use Wikibase\Lexeme\Domain\Model\Lexeme; |
13 | use Wikibase\Lexeme\Domain\Model\SenseId; |
14 | use Wikibase\Lib\Store\EntityRevisionLookup; |
15 | use Wikibase\Lib\Store\RevisionedUnresolvedRedirectException; |
16 | use Wikibase\Lib\Store\StorageException; |
17 | use Wikibase\View\LocalizedTextProvider; |
18 | |
19 | /** |
20 | * @license GPL-2.0-or-later |
21 | */ |
22 | class SenseIdTextFormatter implements EntityIdFormatter { |
23 | |
24 | private EntityRevisionLookup $revisionLookup; |
25 | private LocalizedTextProvider $localizedTextProvider; |
26 | private EntityIdFormatter $entityIdLabelFormatter; |
27 | |
28 | public function __construct( |
29 | EntityRevisionLookup $revisionLookup, |
30 | LocalizedTextProvider $localizedTextProvider, |
31 | EntityIdFormatter $entityIdLabelFormatter |
32 | ) { |
33 | $this->revisionLookup = $revisionLookup; |
34 | $this->localizedTextProvider = $localizedTextProvider; |
35 | $this->entityIdLabelFormatter = $entityIdLabelFormatter; |
36 | } |
37 | |
38 | /** |
39 | * @param SenseId $value |
40 | * |
41 | * @return string plain text |
42 | */ |
43 | public function formatEntityId( EntityId $value ): string { |
44 | if ( !( $value instanceof SenseId ) ) { |
45 | throw new InvalidArgumentException( |
46 | 'Attempted to format non-Sense entity as Sense: ' . $value->getSerialization() ); |
47 | } |
48 | try { |
49 | $lexemeRevision = $this->revisionLookup->getEntityRevision( $value->getLexemeId() ); |
50 | } catch ( RevisionedUnresolvedRedirectException | StorageException $e ) { |
51 | $lexemeRevision = null; // see fallback below |
52 | } |
53 | |
54 | if ( $lexemeRevision === null ) { |
55 | return $value->getSerialization(); |
56 | } |
57 | |
58 | /** @var Lexeme $lexeme */ |
59 | $lexeme = $lexemeRevision->getEntity(); |
60 | '@phan-var Lexeme $lexeme'; |
61 | try { |
62 | $sense = $lexeme->getSense( $value ); |
63 | } catch ( OutOfRangeException $e ) { |
64 | return $value->getSerialization(); |
65 | } |
66 | |
67 | $lexemeLanguageLabel = $this->entityIdLabelFormatter->formatEntityId( $lexeme->getLanguage() ); |
68 | $lemmas = implode( |
69 | $this->localizedTextProvider->get( |
70 | 'wikibaselexeme-presentation-lexeme-display-label-separator-multiple-lemma' |
71 | ), |
72 | $lexeme->getLemmas()->toTextArray() |
73 | ); |
74 | |
75 | $messageKey = 'wikibaselexeme-senseidformatter-layout'; |
76 | $languageCode = $this->localizedTextProvider->getLanguageOf( $messageKey ); |
77 | try { |
78 | // TODO language fallbacks (T200983) |
79 | $gloss = $sense->getGlosses()->getByLanguage( $languageCode )->getText(); |
80 | } catch ( OutOfBoundsException $e ) { |
81 | return $value->getSerialization(); |
82 | } |
83 | |
84 | return $this->localizedTextProvider->get( |
85 | $messageKey, |
86 | [ $lemmas, $gloss, $lexemeLanguageLabel ] |
87 | ); |
88 | } |
89 | |
90 | } |