EntityRetrievingTermLookup::getAllLabels()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 4
cts 4
cp 1
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
namespace Wikibase\DataModel\Services\Lookup;
4
5
use Wikibase\DataModel\Entity\EntityDocument;
6
use Wikibase\DataModel\Entity\EntityId;
7
use Wikibase\DataModel\Term\DescriptionsProvider;
8
use Wikibase\DataModel\Term\LabelsProvider;
9
use Wikibase\DataModel\Term\TermList;
10
11
/**
12
 * @since 1.1
13
 *
14
 * @license GPL-2.0-or-later
15
 * @author Katie Filbert < [email protected] >
16
 * @author Addshore
17
 */
18
class EntityRetrievingTermLookup implements TermLookup {
19
20
	/**
21
	 * @var EntityLookup
22
	 */
23
	private $entityLookup;
24
25
	/**
26
	 * @var TermList[] Labels in all languages, indexed by entity ID serialization.
27
	 */
28
	private $labels;
29
30
	/**
31 14
	 * @var TermList[] Descriptions in all languages, indexed by entity ID serialization.
32 14
	 */
33 14
	private $descriptions;
34
35
	public function __construct( EntityLookup $entityLookup ) {
36
		$this->entityLookup = $entityLookup;
37
	}
38
39
	/**
40
	 * @see TermLookup::getLabel()
41
	 *
42
	 * @param EntityId $entityId
43
	 * @param string $languageCode
44 4
	 *
45 4
	 * @return string|null
46
	 * @throws TermLookupException
47 2
	 */
48 1
	public function getLabel( EntityId $entityId, $languageCode ) {
49
		$labels = $this->getAllLabels( $entityId, [ $languageCode ] );
50
51 1
		if ( $labels->hasTermForLanguage( $languageCode ) ) {
52
			return $labels->getByLanguage( $languageCode )->getText();
53
		}
54
55
		return null;
56
	}
57
58
	/**
59
	 * @see TermLookup::getLabels()
60
	 *
61
	 * @param EntityId $entityId
62
	 * @param string[] $languages
63 3
	 *
64 3
	 * @throws TermLookupException
65 3
	 * @return string[]
66
	 */
67 3
	public function getLabels( EntityId $entityId, array $languages ) {
68
		$labels = $this->getAllLabels( $entityId, $languages )->toTextArray();
69
70
		return array_intersect_key( $labels, array_flip( $languages ) );
71
	}
72
73
	/**
74
	 * @see TermLookup::getDescription()
75
	 *
76
	 * @param EntityId $entityId
77
	 * @param string $languageCode
78
	 *
79 4
	 * @throws TermLookupException
80 4
	 * @return string|null
81
	 */
82 2
	public function getDescription( EntityId $entityId, $languageCode ) {
83 1
		$descriptions = $this->getAllDescriptions( $entityId, [ $languageCode ] );
84
85
		if ( $descriptions->hasTermForLanguage( $languageCode ) ) {
86 1
			return $descriptions->getByLanguage( $languageCode )->getText();
87
		}
88
89
		return null;
90
	}
91
92
	/**
93
	 * @see TermLookup::getDescriptions()
94
	 *
95
	 * @param EntityId $entityId
96
	 * @param string[] $languages
97
	 *
98 3
	 * @throws TermLookupException
99 3
	 * @return string[]
100 3
	 */
101
	public function getDescriptions( EntityId $entityId, array $languages ) {
102 3
		$descriptions = $this->getAllDescriptions( $entityId, $languages )->toTextArray();
103
104
		return array_intersect_key( $descriptions, array_flip( $languages ) );
105
	}
106
107
	/**
108
	 * @param EntityId $entityId
109
	 * @param string[] $languageCodes Not used for filtering but in thrown exceptions.
110
	 *
111
	 * @throws TermLookupException
112 14
	 * @return TermList
113 14
	 */
114
	private function getAllLabels( EntityId $entityId, array $languageCodes ) {
115 14
		$id = $entityId->getSerialization();
116 14
117 10
		if ( !isset( $this->labels[$id] ) ) {
118
			$entity = $this->fetchEntity( $entityId, $languageCodes );
119 10
			$this->labels[$id] = $entity instanceof LabelsProvider
120
				? $entity->getLabels()
121
				: new TermList();
122
		}
123
124
		return $this->labels[$id];
125
	}
126
127
	/**
128
	 * @param EntityId $entityId
129 14
	 * @param string[] $languageCodes Not used for filtering but in thrown exceptions.
130
	 *
131 14
	 * @throws TermLookupException
132 14
	 * @return TermList
133 2
	 */
134
	private function getAllDescriptions( EntityId $entityId, array $languageCodes ) {
135
		$id = $entityId->getSerialization();
136 12
137 2
		if ( !isset( $this->descriptions[$id] ) ) {
138
			$entity = $this->fetchEntity( $entityId, $languageCodes );
139
			$this->descriptions[$id] = $entity instanceof DescriptionsProvider
140 10
				? $entity->getDescriptions()
141
				: new TermList();
142
		}
143
144
		return $this->descriptions[$id];
145
	}
146
147
	/**
148
	 * @param EntityId $entityId
149
	 * @param string[] $languages Not used for filtering but in thrown exceptions.
150
	 *
151
	 * @throws TermLookupException
152
	 * @return EntityDocument
153
	 */
154
	private function fetchEntity( EntityId $entityId, array $languages ) {
155
		try {
156
			$entity = $this->entityLookup->getEntity( $entityId );
157
		} catch ( EntityLookupException $ex ) {
158
			throw new TermLookupException( $entityId, $languages, 'The entity could not be loaded', $ex );
159
		}
160
161
		if ( $entity === null ) {
162
			throw new TermLookupException( $entityId, $languages, 'The entity could not be loaded' );
163
		}
164
165
		return $entity;
166
	}
167
168
}
169