1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Services\EntityId; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use UnexpectedValueException; |
7
|
|
|
use Wikibase\DataModel\Entity\EntityId; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Constructs EntityId objects from entity type identifiers and unique parts of entity ID |
11
|
|
|
* serializations. The unique part is typically the numeric part of an entity ID, excluding the |
12
|
|
|
* static part that's the same for all IDs of that type. |
13
|
|
|
* |
14
|
|
|
* Meant to be the counterpart for @see Int32EntityId::getNumericId, as well as an extensible |
15
|
|
|
* replacement for @see LegacyIdInterpreter::newIdFromTypeAndNumber. |
16
|
|
|
* |
17
|
|
|
* @since 3.9 |
18
|
|
|
* |
19
|
|
|
* @license GPL-2.0+ |
20
|
|
|
* @author Thiemo Mättig |
21
|
|
|
*/ |
22
|
|
|
class EntityIdComposer { |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var callable[] |
26
|
|
|
*/ |
27
|
|
|
private $composers; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param callable[] $composers Array mapping entity type identifiers to callables accepting a |
31
|
|
|
* single mixed value, representing the unique part of an entity ID serialization, and |
32
|
|
|
* returning an EntityId object. |
33
|
|
|
* |
34
|
|
|
* @throws InvalidArgumentException |
35
|
|
|
*/ |
36
|
|
|
public function __construct( array $composers ) { |
37
|
|
|
foreach ( $composers as $entityType => $composer ) { |
38
|
|
|
if ( !is_string( $entityType ) || $entityType === '' || !is_callable( $composer ) ) { |
39
|
|
|
throw new InvalidArgumentException( '$composers must map non-empty strings to callables' ); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->composers = $composers; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $repositoryName |
48
|
|
|
* @param string $entityType |
49
|
|
|
* @param mixed $uniquePart |
50
|
|
|
* |
51
|
|
|
* @throws InvalidArgumentException when the entity type is not known or the unique part is not |
52
|
|
|
* unique. |
53
|
|
|
* @throws UnexpectedValueException when the configured composer did not return an EntityId |
54
|
|
|
* object. |
55
|
|
|
* @return EntityId |
56
|
|
|
*/ |
57
|
|
|
public function composeEntityId( $repositoryName, $entityType, $uniquePart ) { |
58
|
|
|
if ( !isset( $this->composers[$entityType] ) ) { |
59
|
|
|
throw new InvalidArgumentException( 'Unknown entity type ' . $entityType ); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$id = $this->composers[$entityType]( $repositoryName, $uniquePart ); |
63
|
|
|
|
64
|
|
|
if ( !( $id instanceof EntityId ) ) { |
65
|
|
|
throw new UnexpectedValueException( 'Composer for ' . $entityType . ' is invalid' ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $id; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|