Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
22 / 22 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
SenseLabelDescriptionLookup | |
100.00% |
22 / 22 |
|
100.00% |
3 / 3 |
6 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getLabel | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
getDescription | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | namespace Wikibase\Lexeme\Domain\Storage; |
4 | |
5 | use Wikibase\DataModel\Entity\EntityId; |
6 | use Wikibase\DataModel\Services\Lookup\EntityLookup; |
7 | use Wikibase\DataModel\Services\Lookup\LabelDescriptionLookup; |
8 | use Wikibase\DataModel\Term\Term; |
9 | use Wikibase\Lexeme\Domain\Model\Lexeme; |
10 | use Wikibase\Lexeme\Domain\Model\Sense; |
11 | use Wikibase\Lexeme\Domain\Model\SenseId; |
12 | use Wikibase\Lib\TermLanguageFallbackChain; |
13 | use Wikibase\View\LocalizedTextProvider; |
14 | use Wikimedia\Assert\Assert; |
15 | |
16 | /** |
17 | * A {@link LabelDescriptionLookup} for {@link Sense}s |
18 | * which returns the lemmas of the sense’s lexeme as the “label” |
19 | * and the gloss as the “description”. |
20 | * |
21 | * @license GPL-2.0-or-later |
22 | */ |
23 | class SenseLabelDescriptionLookup implements LabelDescriptionLookup { |
24 | |
25 | /** |
26 | * @var EntityLookup |
27 | */ |
28 | private $entityLookup; |
29 | |
30 | /** |
31 | * @var TermLanguageFallbackChain |
32 | */ |
33 | private $termLanguageFallbackChain; |
34 | |
35 | /** |
36 | * @var LocalizedTextProvider |
37 | */ |
38 | private $localizedTextProvider; |
39 | |
40 | public function __construct( |
41 | EntityLookup $entityLookup, |
42 | TermLanguageFallbackChain $termLanguageFallbackChain, |
43 | LocalizedTextProvider $localizedTextProvider |
44 | ) { |
45 | $this->entityLookup = $entityLookup; |
46 | $this->termLanguageFallbackChain = $termLanguageFallbackChain; |
47 | $this->localizedTextProvider = $localizedTextProvider; |
48 | } |
49 | |
50 | /** |
51 | * @param SenseId $entityId |
52 | * @return Term|null |
53 | */ |
54 | public function getLabel( EntityId $entityId ) { |
55 | Assert::parameterType( SenseId::class, $entityId, '$entityId' ); |
56 | $lexemeId = $entityId->getLexemeId(); |
57 | |
58 | if ( !$this->entityLookup->hasEntity( $lexemeId ) ) { |
59 | return null; |
60 | } |
61 | |
62 | /** @var Lexeme $lexeme */ |
63 | $lexeme = $this->entityLookup->getEntity( $lexemeId ); |
64 | '@phan-var Lexeme $lexeme'; |
65 | $lemmas = $lexeme->getLemmas()->toTextArray(); |
66 | $separator = $this->localizedTextProvider->get( |
67 | 'wikibaselexeme-presentation-lexeme-display-label-separator-multiple-lemma' |
68 | ); |
69 | |
70 | // we have to pretend the result is a single term with a single language code :/ |
71 | return new Term( key( $lemmas ), implode( $separator, $lemmas ) ); |
72 | } |
73 | |
74 | /** |
75 | * @param SenseId $entityId |
76 | * @return Term|null |
77 | */ |
78 | public function getDescription( EntityId $entityId ) { |
79 | Assert::parameterType( SenseId::class, $entityId, '$entityId' ); |
80 | |
81 | if ( !$this->entityLookup->hasEntity( $entityId ) ) { |
82 | return null; |
83 | } |
84 | |
85 | /** @var Sense $sense */ |
86 | $sense = $this->entityLookup->getEntity( $entityId ); |
87 | '@phan-var Sense $sense'; |
88 | $glosses = $sense->getGlosses()->toTextArray(); |
89 | |
90 | $value = $this->termLanguageFallbackChain->extractPreferredValue( $glosses ); |
91 | if ( $value === null ) { |
92 | return null; |
93 | } |
94 | return new Term( $value['language'], $value['value'] ); |
95 | } |
96 | |
97 | } |