EntitySource::getEntityTypes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\DataAccess;
4
5
use Wikimedia\Assert\Assert;
6
7
/**
8
 * An EntitySource includes information needed to interact with one or more entity types at a given source.
9
 * EntitySource can only currently be used via direct database access.
10
 *
11
 * @see EntitySourceDefinitions for defining multiple EntitySources within a single site.
12
 *
13
 * @license GPL-2.0-or-later
14
 */
15
class EntitySource {
16
17
	/**
18
	 * @var string
19
	 */
20
	private $sourceName;
21
22
	/**
23
	 * @var string|false The name of the database to use (use false for the local db)
24
	 */
25
	private $databaseName;
26
27
	/**
28
	 * @var string[]
29
	 */
30
	private $entityTypes;
31
32
	/**
33
	 * @var int[]
34
	 */
35
	private $entityNamespaceIds;
36
37
	/**
38
	 * @var string[]
39
	 */
40
	private $entitySlots;
41
42
	/**
43
	 * @var string
44
	 */
45
	private $conceptBaseUri;
46
47
	private $rdfNodeNamespacePrefix;
48
49
	private $rdfPredicateNamespacePrefix;
50
51
	/**
52
	 * @var string
53
	 */
54
	private $interwikiPrefix;
55
56
	/**
57
	 * @param string $name Unique name for the source for a given configuration / site, used for indexing the sources internally.
58
	 *        This does not have to be a wikiname, sitename or dbname, it can for example just be 'properties'.
59
	 * @param string|false $databaseName The name of the database to use (use false for the local db)
60
	 * @param array $entityNamespaceIdsAndSlots Associative array indexed by entity type (string), values are
61
	 * array of form [ 'namespaceId' => int, 'slot' => string ]
62
	 * @param string $conceptBaseUri
63
	 * @param string $rdfNodeNamespacePrefix
64
	 * @param string $rdfPredicateNamespacePrefix
65
	 * @param string $interwikiPrefix
66
	 */
67
	public function __construct(
68
		$name,
69
		$databaseName,
70
		array $entityNamespaceIdsAndSlots,
71
		$conceptBaseUri,
72
		$rdfNodeNamespacePrefix,
73
		$rdfPredicateNamespacePrefix,
74
		$interwikiPrefix
75
	) {
76
		Assert::parameterType( 'string', $name, '$name' );
77
		Assert::parameter( is_string( $databaseName ) || $databaseName === false, '$databaseName', 'must be a string or false' );
78
		Assert::parameterType( 'string', $conceptBaseUri, '$conceptBaseUri' );
79
		Assert::parameterType( 'string', $rdfNodeNamespacePrefix, '$rdfNodeNamespacePrefix' );
80
		Assert::parameterType( 'string', $rdfPredicateNamespacePrefix, '$rdfPredicateNamespacePrefix' );
81
		Assert::parameterType( 'string', $interwikiPrefix, '$interwikiPrefix' );
82
		$this->assertEntityNamespaceIdsAndSlots( $entityNamespaceIdsAndSlots );
83
84
		$this->sourceName = $name;
85
		$this->databaseName = $databaseName;
86
		$this->conceptBaseUri = $conceptBaseUri;
87
		$this->rdfNodeNamespacePrefix = $rdfNodeNamespacePrefix;
88
		$this->rdfPredicateNamespacePrefix = $rdfPredicateNamespacePrefix;
89
		$this->interwikiPrefix = $interwikiPrefix;
90
91
		$this->setEntityTypeData( $entityNamespaceIdsAndSlots );
92
	}
93
94
	private function assertEntityNamespaceIdsAndSlots( array $entityNamespaceIdsAndSlots ) {
95
		foreach ( $entityNamespaceIdsAndSlots as $entityType => $namespaceIdAndSlot ) {
96
			if ( !is_string( $entityType ) ) {
97
				throw new \InvalidArgumentException( 'Entity type name not a string: ' . $entityType );
98
			}
99
			if ( !is_array( $namespaceIdAndSlot ) ) {
100
				throw new \InvalidArgumentException( 'Namespace and slot not defined for entity type: ' . $entityType );
101
			}
102
			if ( !array_key_exists( 'namespaceId', $namespaceIdAndSlot ) ) {
103
				throw new \InvalidArgumentException( 'Namespace ID not defined for entity type: ' . $entityType );
104
			}
105
			if ( !array_key_exists( 'slot', $namespaceIdAndSlot ) ) {
106
				throw new \InvalidArgumentException( 'Slot not defined for entity type: ' . $entityType );
107
			}
108
			if ( !is_int( $namespaceIdAndSlot['namespaceId'] ) ) {
109
				throw new \InvalidArgumentException( 'Namespace ID for entity type must be an integer: ' . $entityType );
110
			}
111
			if ( !is_string( $namespaceIdAndSlot['slot'] ) ) {
112
				throw new \InvalidArgumentException( 'Slot for entity type must be a string: ' . $entityType );
113
			}
114
		}
115
	}
116
117
	private function setEntityTypeData( array $entityNamespaceIdsAndSlots ) {
118
		$this->entityTypes = array_keys( $entityNamespaceIdsAndSlots );
0 ignored issues
show
Documentation Bug introduced by
It seems like array_keys($entityNamespaceIdsAndSlots) of type array<integer,integer|string> is incompatible with the declared type array<integer,string> of property $entityTypes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
119
		$this->entityNamespaceIds = array_map(
120
			function ( $x ) {
121
				return $x['namespaceId'];
122
			},
123
			$entityNamespaceIdsAndSlots
124
		);
125
		$this->entitySlots = array_map(
126
			function ( $x ) {
127
				return $x['slot'];
128
			},
129
			$entityNamespaceIdsAndSlots
130
		);
131
	}
132
133
	/**
134
	 * @return string|false The name of the database to use (use false for the local db)
135
	 */
136
	public function getDatabaseName() {
137
		return $this->databaseName;
138
	}
139
140
	public function getSourceName() {
141
		return $this->sourceName;
142
	}
143
144
	public function getEntityTypes() {
145
		return $this->entityTypes;
146
	}
147
148
	public function getEntityNamespaceIds() {
149
		return $this->entityNamespaceIds;
150
	}
151
152
	public function getEntitySlotNames() {
153
		return $this->entitySlots;
154
	}
155
156
	public function getConceptBaseUri() {
157
		return $this->conceptBaseUri;
158
	}
159
160
	public function getRdfNodeNamespacePrefix() {
161
		return $this->rdfNodeNamespacePrefix;
162
	}
163
164
	public function getRdfPredicateNamespacePrefix() {
165
		return $this->rdfPredicateNamespacePrefix;
166
	}
167
168
	public function getInterwikiPrefix() {
169
		return $this->interwikiPrefix;
170
	}
171
172
}
173