|
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-or-later |
|
20
|
|
|
* @author Thiemo Kreuz |
|
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 $entityType |
|
48
|
|
|
* @param mixed $uniquePart |
|
49
|
|
|
* |
|
50
|
|
|
* @throws InvalidArgumentException when the entity type is not known or the unique part is not |
|
51
|
|
|
* unique. |
|
52
|
|
|
* @throws UnexpectedValueException when the configured composer did not return an EntityId |
|
53
|
|
|
* object. |
|
54
|
|
|
* @return EntityId |
|
55
|
|
|
*/ |
|
56
|
|
|
public function composeEntityId( $entityType, $uniquePart ) { |
|
57
|
|
|
if ( !isset( $this->composers[$entityType] ) ) { |
|
58
|
|
|
throw new InvalidArgumentException( 'Unknown entity type ' . $entityType ); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$id = $this->composers[$entityType]( $uniquePart ); |
|
62
|
|
|
|
|
63
|
|
|
if ( !( $id instanceof EntityId ) ) { |
|
64
|
|
|
throw new UnexpectedValueException( 'Composer for ' . $entityType . ' is invalid' ); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $id; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|