1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Services\EntityId; |
4
|
|
|
|
5
|
|
|
use Wikibase\DataModel\Entity\EntityIdParser; |
6
|
|
|
use Wikimedia\Assert\Assert; |
7
|
|
|
use Wikimedia\Assert\ParameterAssertionException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @since 3.7 |
11
|
|
|
* |
12
|
|
|
* @license GPL-2.0-or-later |
13
|
|
|
*/ |
14
|
|
|
class PrefixMappingEntityIdParserFactory { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string[] |
18
|
|
|
*/ |
19
|
|
|
private $idPrefixMapping; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var EntityIdParser |
23
|
|
|
*/ |
24
|
|
|
private $parser; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var PrefixMappingEntityIdParser[] |
28
|
|
|
*/ |
29
|
|
|
private $parsers = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @since 3.7 |
33
|
|
|
* |
34
|
|
|
* @param EntityIdParser $parser |
35
|
|
|
* @param array[] $idPrefixMapping An associative array mapping repository names (strings) to id serialization |
36
|
|
|
* prefix mappings specific to the particular repository (@see PrefixMappingEntityIdParser). |
37
|
|
|
* If an empty-string key is provided in the mapping for some repository, its value must be the same |
38
|
|
|
* as the repository name. |
39
|
|
|
* |
40
|
|
|
* @throws ParameterAssertionException |
41
|
|
|
*/ |
42
|
|
|
public function __construct( EntityIdParser $parser, array $idPrefixMapping ) { |
43
|
|
|
Assert::parameterElementType( 'string', array_keys( $idPrefixMapping ), 'array_keys( $idPrefixMapping)' ); |
44
|
|
|
foreach ( $idPrefixMapping as $repositoryName => $mapping ) { |
45
|
|
|
Assert::parameter( |
46
|
|
|
strpos( $repositoryName, ':' ) === false, |
47
|
|
|
'keys in $idPrefixMapping', |
48
|
|
|
'must not contain a colon' |
49
|
|
|
); |
50
|
|
|
Assert::parameterType( 'array', $mapping, '$idPrefixMapping[' . $repositoryName . ']' ); |
51
|
|
|
Assert::parameterElementType( 'string', $mapping, '$idPrefixMapping[' . $repositoryName . ']' ); |
52
|
|
|
Assert::parameterElementType( |
53
|
|
|
'string', |
54
|
|
|
array_keys( $mapping ), |
55
|
|
|
'array_keys( $idPrefixMapping[' . $repositoryName . '] )' |
56
|
|
|
); |
57
|
|
|
Assert::parameter( |
58
|
|
|
!array_key_exists( '', $mapping ) || $mapping[''] === $repositoryName, |
59
|
|
|
'$idPrefixMapping[' . $repositoryName . '] )', |
60
|
|
|
'must either not contain empty-string prefix mapping or it must be equal to repository name' |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->idPrefixMapping = $idPrefixMapping; |
|
|
|
|
65
|
|
|
$this->parser = $parser; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Create a PrefixMappingEntityIdParser for the particular repository using id prefix mappings |
70
|
|
|
* defined in the constructor. |
71
|
|
|
* |
72
|
|
|
* @since 3.7 |
73
|
|
|
* |
74
|
|
|
* @param string $repository |
75
|
|
|
* |
76
|
|
|
* @return PrefixMappingEntityIdParser |
77
|
|
|
* @throws ParameterAssertionException |
78
|
|
|
*/ |
79
|
|
|
public function getIdParser( $repository ) { |
80
|
|
|
if ( !isset( $this->parsers[$repository] ) ) { |
81
|
|
|
$this->parsers[$repository] = $this->newIdParserForRepository( $repository ); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this->parsers[$repository]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $repository |
89
|
|
|
* |
90
|
|
|
* @return PrefixMappingEntityIdParser |
91
|
|
|
* @throws ParameterAssertionException |
92
|
|
|
*/ |
93
|
|
|
private function newIdParserForRepository( $repository ) { |
94
|
|
|
Assert::parameterType( 'string', $repository, '$repository' ); |
95
|
|
|
Assert::parameter( strpos( $repository, ':' ) === false, '$repository', 'must not contain a colon' ); |
96
|
|
|
$mapping = [ '' => $repository ]; |
97
|
|
|
if ( isset( $this->idPrefixMapping[$repository] ) ) { |
98
|
|
|
$mapping = array_merge( $mapping, $this->idPrefixMapping[$repository] ); |
99
|
|
|
} |
100
|
|
|
return new PrefixMappingEntityIdParser( $mapping, $this->parser ); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
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..