1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Internal; |
4
|
|
|
|
5
|
|
|
use Wikibase\DataModel\Facet\FacetContainer; |
6
|
|
|
use Wikibase\DataModel\Facet\MismatchingFacetException; |
7
|
|
|
use Wikibase\DataModel\Facet\NoSuchFacetException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Helper object for managing facet objects. |
11
|
|
|
* |
12
|
|
|
* @todo This should perhaps be a trait. |
13
|
|
|
* |
14
|
|
|
* @since 5.0 |
15
|
|
|
* |
16
|
|
|
* @licence GNU GPL v2+ |
17
|
|
|
* @author Daniel Kinzler |
18
|
|
|
*/ |
19
|
|
|
class FacetManager implements FacetContainer { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var object[] Facet objects, by name. |
23
|
|
|
*/ |
24
|
|
|
private $facets = array(); |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @see FacetContainer::hasFacet |
28
|
|
|
* |
29
|
|
|
* @param string $name |
30
|
|
|
* |
31
|
|
|
* @return boolean |
32
|
|
|
*/ |
33
|
1 |
|
public function hasFacet( $name ) { |
34
|
1 |
|
return isset( $this->facets[$name] ); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @see FacetContainer::listFacets |
39
|
|
|
* |
40
|
|
|
* @return string[] |
41
|
|
|
*/ |
42
|
1 |
|
public function listFacets() { |
43
|
1 |
|
return array_keys( $this->facets ); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @see FacetContainer::getFacet |
48
|
|
|
* |
49
|
|
|
* @param string $name |
50
|
|
|
* @param string|null $type The desired type |
51
|
|
|
* |
52
|
|
|
* @return object |
53
|
|
|
*/ |
54
|
2 |
|
public function getFacet( $name, $type = null ) { |
55
|
2 |
|
if ( !isset( $this->facets[$name] ) ) { |
56
|
1 |
|
throw new NoSuchFacetException( $name ); |
57
|
|
|
} |
58
|
|
|
|
59
|
2 |
|
$facet = $this->facets[$name]; |
60
|
|
|
|
61
|
|
|
// TODO: if $facet is callable, call it, and replace $this->facets[$name] with the result. |
62
|
|
|
|
63
|
2 |
|
if ( $type !== null && !is_a( $facet, $type ) ) { |
64
|
1 |
|
throw new MismatchingFacetException( $name, $type ); |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
return $facet; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @see FacetContainer::addFacet |
72
|
|
|
* |
73
|
|
|
* @param string $name |
74
|
|
|
* @param object $facetObject |
75
|
|
|
*/ |
76
|
4 |
|
public function addFacet( $name, $facetObject ) { |
77
|
4 |
|
$this->facets[$name] = $facetObject; |
78
|
4 |
|
} |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|