|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Services\Lookup; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use Wikibase\DataModel\Entity\PropertyId; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* PropertyDataTypeLookup that uses an in memory array to retrieve the requested information. |
|
10
|
|
|
* If the information is not set when requested an exception is thrown. |
|
11
|
|
|
* This class can be used as a fake in tests. |
|
12
|
|
|
* |
|
13
|
|
|
* @since 1.0 |
|
14
|
|
|
* |
|
15
|
|
|
* @license GPL-2.0-or-later |
|
16
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
17
|
|
|
*/ |
|
18
|
|
|
class InMemoryDataTypeLookup implements PropertyDataTypeLookup { |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string[] |
|
22
|
|
|
*/ |
|
23
|
|
|
private $dataTypeIds = []; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param PropertyId $propertyId |
|
27
|
|
|
* |
|
28
|
|
|
* @return string |
|
29
|
|
|
* @throws PropertyDataTypeLookupException |
|
30
|
|
|
*/ |
|
31
|
2 |
|
public function getDataTypeIdForProperty( PropertyId $propertyId ) { |
|
32
|
2 |
|
$this->verifyDataTypeIsSet( $propertyId ); |
|
33
|
|
|
|
|
34
|
1 |
|
return $this->dataTypeIds[$propertyId->getSerialization()]; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @since 1.0 |
|
39
|
|
|
* |
|
40
|
|
|
* @param PropertyId $propertyId |
|
41
|
|
|
* @param string $dataTypeId |
|
42
|
|
|
* |
|
43
|
|
|
* @throws InvalidArgumentException |
|
44
|
|
|
*/ |
|
45
|
1 |
|
public function setDataTypeForProperty( PropertyId $propertyId, $dataTypeId ) { |
|
46
|
1 |
|
$this->verifyDataTypeIdType( $dataTypeId ); |
|
47
|
1 |
|
$this->dataTypeIds[$propertyId->getSerialization()] = $dataTypeId; |
|
48
|
1 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param PropertyId $propertyId |
|
52
|
|
|
* |
|
53
|
|
|
* @throws PropertyDataTypeLookupException |
|
54
|
|
|
*/ |
|
55
|
2 |
|
private function verifyDataTypeIsSet( PropertyId $propertyId ) { |
|
56
|
2 |
|
$id = $propertyId->getSerialization(); |
|
57
|
|
|
|
|
58
|
2 |
|
if ( !array_key_exists( $id, $this->dataTypeIds ) ) { |
|
59
|
1 |
|
throw new PropertyDataTypeLookupException( $propertyId, "The DataType for property '$id' is not set" ); |
|
60
|
|
|
} |
|
61
|
1 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param string $dataTypeId |
|
65
|
|
|
* |
|
66
|
|
|
* @throws InvalidArgumentException |
|
67
|
|
|
*/ |
|
68
|
1 |
|
private function verifyDataTypeIdType( $dataTypeId ) { |
|
69
|
1 |
|
if ( !is_string( $dataTypeId ) ) { |
|
|
|
|
|
|
70
|
|
|
throw new InvalidArgumentException( '$dataTypeId must be a string; got ' . get_debug_type( $dataTypeId ) ); |
|
71
|
|
|
} |
|
72
|
1 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|