1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Services; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use OutOfBoundsException; |
7
|
|
|
use Traversable; |
8
|
|
|
use Wikibase\DataModel\Entity\PropertyId; |
9
|
|
|
use Wikibase\DataModel\PropertyIdProvider; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Groups property id providers by their property id. |
13
|
|
|
* |
14
|
|
|
* @since 1.0 |
15
|
|
|
* |
16
|
|
|
* @license GPL-2.0-or-later |
17
|
|
|
* @author Bene* < [email protected] > |
18
|
|
|
*/ |
19
|
|
|
class ByPropertyIdGrouper { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array[] |
23
|
|
|
*/ |
24
|
|
|
private $byPropertyId = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param PropertyIdProvider[]|Traversable $propertyIdProviders |
28
|
|
|
* |
29
|
|
|
* @throws InvalidArgumentException |
30
|
|
|
*/ |
31
|
21 |
|
public function __construct( $propertyIdProviders ) { |
32
|
21 |
|
$this->assertArePropertyIdProviders( $propertyIdProviders ); |
33
|
15 |
|
$this->indexPropertyIdProviders( $propertyIdProviders ); |
34
|
15 |
|
} |
35
|
|
|
|
36
|
21 |
|
private function assertArePropertyIdProviders( $propertyIdProviders ) { |
37
|
21 |
|
if ( !is_array( $propertyIdProviders ) && !( $propertyIdProviders instanceof Traversable ) ) { |
38
|
2 |
|
throw new InvalidArgumentException( '$propertyIdProviders must be an array or an instance of Traversable' ); |
39
|
|
|
} |
40
|
|
|
|
41
|
19 |
|
foreach ( $propertyIdProviders as $propertyIdProvider ) { |
42
|
16 |
|
if ( !( $propertyIdProvider instanceof PropertyIdProvider ) ) { |
43
|
4 |
|
throw new InvalidArgumentException( |
44
|
|
|
'Every element in $propertyIdProviders must be an instance of PropertyIdProvider' |
45
|
4 |
|
); |
46
|
|
|
} |
47
|
15 |
|
} |
48
|
15 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param PropertyIdProvider[]|Traversable $propertyIdProviders |
52
|
|
|
*/ |
53
|
15 |
|
private function indexPropertyIdProviders( $propertyIdProviders ) { |
54
|
15 |
|
foreach ( $propertyIdProviders as $propertyIdProvider ) { |
55
|
12 |
|
$this->addPropertyIdProvider( $propertyIdProvider ); |
56
|
15 |
|
} |
57
|
15 |
|
} |
58
|
|
|
|
59
|
12 |
|
private function addPropertyIdProvider( PropertyIdProvider $propertyIdProvider ) { |
60
|
12 |
|
$idSerialization = $propertyIdProvider->getPropertyId()->getSerialization(); |
61
|
|
|
$this->byPropertyId[$idSerialization][] = $propertyIdProvider; |
62
|
12 |
|
} |
63
|
9 |
|
|
64
|
9 |
|
/** |
65
|
12 |
|
* Returns all property ids which were found. |
66
|
|
|
* |
67
|
12 |
|
* @since 1.0 |
68
|
|
|
* |
69
|
|
|
* @return PropertyId[] |
70
|
|
|
*/ |
71
|
|
|
public function getPropertyIds() { |
72
|
|
|
return array_map( |
73
|
|
|
function( $propertyId ) { |
74
|
|
|
return new PropertyId( $propertyId ); |
75
|
|
|
}, |
76
|
7 |
|
array_keys( $this->byPropertyId ) |
77
|
7 |
|
); |
78
|
7 |
|
} |
79
|
4 |
|
|
80
|
7 |
|
/** |
81
|
7 |
|
* Returns the PropertyIdProvider instances for the given PropertyId. |
82
|
7 |
|
* |
83
|
|
|
* @since 1.0 |
84
|
|
|
* |
85
|
|
|
* @param PropertyId $propertyId |
86
|
|
|
* |
87
|
|
|
* @throws OutOfBoundsException |
88
|
|
|
* @return PropertyIdProvider[] |
89
|
|
|
*/ |
90
|
|
|
public function getByPropertyId( PropertyId $propertyId ) { |
91
|
|
|
$idSerialization = $propertyId->getSerialization(); |
92
|
|
|
|
93
|
|
|
if ( !isset( $this->byPropertyId[$idSerialization] ) ) { |
94
|
|
|
throw new OutOfBoundsException( 'PropertyIdProvider with propertyId "' . $idSerialization . '" not found' ); |
95
|
3 |
|
} |
96
|
3 |
|
|
97
|
|
|
return $this->byPropertyId[$idSerialization]; |
98
|
3 |
|
} |
99
|
1 |
|
|
100
|
|
|
/** |
101
|
|
|
* Checks if there are PropertyIdProvider instances for the given PropertyId. |
102
|
2 |
|
* |
103
|
|
|
* @since 1.0 |
104
|
|
|
* |
105
|
|
|
* @param PropertyId $propertyId |
106
|
|
|
* |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
|
|
public function hasPropertyId( PropertyId $propertyId ) { |
110
|
|
|
return isset( $this->byPropertyId[$propertyId->getSerialization()] ); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
} |
114
|
|
|
|