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+ |
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
|
|
|
* @param EntityIdParser $parser |
28
|
|
|
* @param array $idPrefixMapping An associative array mapping repository names (strings) to id serialization |
29
|
|
|
* prefix mappings specific to the particular repository (@see PrefixMappingEntityIdParser). |
30
|
|
|
* If an empty-string key is provided in the mapping for some repository, its value must be the same |
31
|
|
|
* as the repository name. |
32
|
|
|
* |
33
|
|
|
* @throws ParameterAssertionException |
34
|
|
|
*/ |
35
|
|
|
public function __construct( EntityIdParser $parser, array $idPrefixMapping ) { |
36
|
|
|
Assert::parameterElementType( 'string', array_keys( $idPrefixMapping ), 'array_keys( $idPrefixMapping)' ); |
37
|
|
|
foreach ( $idPrefixMapping as $repositoryName => $mapping ) { |
38
|
|
|
Assert::parameter( |
39
|
|
|
strpos( $repositoryName, ':' ) === false, |
40
|
|
|
'keys in $idPrefixMapping', |
41
|
|
|
'must not contain a colon' |
42
|
|
|
); |
43
|
|
|
Assert::parameterType( 'array', $mapping, '$idPrefixMapping[' . $repositoryName .']' ); |
44
|
|
|
Assert::parameterElementType( 'string', $mapping, '$idPrefixMapping[' . $repositoryName .']' ); |
45
|
|
|
Assert::parameterElementType( |
46
|
|
|
'string', |
47
|
|
|
array_keys( $mapping ), |
48
|
|
|
'array_keys( $idPrefixMapping[' . $repositoryName .'] )' |
49
|
|
|
); |
50
|
|
|
Assert::parameter( |
51
|
|
|
!array_key_exists( '', $mapping ) || $mapping[''] === $repositoryName, |
52
|
|
|
'$idPrefixMapping[' . $repositoryName .'] )', |
53
|
|
|
'must either not contain empty-string prefix mapping or it must be equal to repository name' |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->idPrefixMapping = $idPrefixMapping; |
58
|
|
|
$this->parser = $parser; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Create a PrefixMappingEntityIdParser for the particular repository using id prefix mappings |
63
|
|
|
* defined in the constructor. |
64
|
|
|
* |
65
|
|
|
* @param string $repository |
66
|
|
|
* |
67
|
|
|
* @return PrefixMappingEntityIdParser |
68
|
|
|
* @throws ParameterAssertionException |
69
|
|
|
*/ |
70
|
|
|
public function getIdParser( $repository ) { |
71
|
|
|
Assert::parameterType( 'string', $repository, '$repository' ); |
72
|
|
|
Assert::parameter( strpos( $repository, ':' ) === false, '$repository', 'must not contain a colon' ); |
73
|
|
|
$mapping = [ '' => $repository ]; |
74
|
|
|
if ( isset( $this->idPrefixMapping[$repository] ) ) { |
75
|
|
|
$mapping = array_merge( $mapping, $this->idPrefixMapping[$repository] ); |
76
|
|
|
} |
77
|
|
|
return new PrefixMappingEntityIdParser( $mapping, $this->parser ); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|