1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Snak; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Wikibase\DataModel\Entity\EntityId; |
7
|
|
|
use Wikibase\DataModel\Entity\Property; |
8
|
|
|
use Wikibase\DataModel\Entity\PropertyId; |
9
|
|
|
use Wikibase\DataModel\Facet\FacetContainer; |
10
|
|
|
use Wikibase\DataModel\Facet\NoSuchFacetException; |
11
|
|
|
use Wikibase\DataModel\Internal\FacetManager; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Base class for snaks. |
15
|
|
|
* See https://www.mediawiki.org/wiki/Wikibase/DataModel#Snaks |
16
|
|
|
* |
17
|
|
|
* @since 0.1 |
18
|
|
|
* |
19
|
|
|
* @licence GNU GPL v2+ |
20
|
|
|
* @author Jeroen De Dauw < [email protected] > |
21
|
|
|
* @author Daniel Kinzler |
22
|
|
|
*/ |
23
|
|
|
abstract class SnakObject implements Snak, FacetContainer { |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var FacetManager|null |
27
|
|
|
*/ |
28
|
|
|
private $facetManager = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @since 0.1 |
32
|
|
|
* |
33
|
|
|
* @var PropertyId |
34
|
|
|
*/ |
35
|
|
|
protected $propertyId; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Support for passing in an EntityId instance that is not a PropertyId instance has |
39
|
|
|
* been deprecated since 0.5. |
40
|
|
|
* |
41
|
|
|
* @since 0.1 |
42
|
|
|
* |
43
|
|
|
* @param PropertyId|EntityId|int $propertyId |
44
|
|
|
* |
45
|
|
|
* @throws InvalidArgumentException |
46
|
|
|
*/ |
47
|
40 |
|
public function __construct( $propertyId ) { |
48
|
40 |
|
if ( is_int( $propertyId ) ) { |
49
|
4 |
|
$propertyId = PropertyId::newFromNumber( $propertyId ); |
50
|
4 |
|
} |
51
|
|
|
|
52
|
40 |
|
if ( !( $propertyId instanceof EntityId ) ) { |
53
|
9 |
|
throw new InvalidArgumentException( '$propertyId must be an instance of EntityId' ); |
54
|
|
|
} |
55
|
|
|
|
56
|
31 |
|
if ( $propertyId->getEntityType() !== Property::ENTITY_TYPE ) { |
57
|
3 |
|
throw new InvalidArgumentException( '$propertyId must have an entityType of ' . Property::ENTITY_TYPE ); |
58
|
|
|
} |
59
|
|
|
|
60
|
28 |
|
if ( !( $propertyId instanceof PropertyId ) ) { |
61
|
|
|
$propertyId = new PropertyId( $propertyId->getSerialization() ); |
62
|
|
|
} |
63
|
|
|
|
64
|
28 |
|
$this->propertyId = $propertyId; |
65
|
28 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @see PropertyIdProvider::getPropertyId |
69
|
|
|
* |
70
|
|
|
* @since 0.1 |
71
|
|
|
* |
72
|
|
|
* @return PropertyId |
73
|
|
|
*/ |
74
|
3 |
|
public function getPropertyId() { |
75
|
3 |
|
return $this->propertyId; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @see Hashable::getHash |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
16 |
|
public function getHash() { |
84
|
16 |
|
return sha1( serialize( $this ) ); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @see Comparable::equals |
89
|
|
|
* |
90
|
|
|
* @since 0.3 |
91
|
|
|
* |
92
|
|
|
* @param mixed $target |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
12 |
|
public function equals( $target ) { |
97
|
12 |
|
if ( $this === $target ) { |
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
|
101
|
12 |
|
return is_object( $target ) |
102
|
12 |
|
&& get_called_class() === get_class( $target ) |
103
|
12 |
|
&& $this->getHash() === $target->getHash(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @see Serializable::serialize |
108
|
|
|
* |
109
|
|
|
* @since 0.1 |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
12 |
|
public function serialize() { |
114
|
12 |
|
return serialize( $this->propertyId->getNumericId() ); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @see Serializable::unserialize |
119
|
|
|
* |
120
|
|
|
* @since 0.1 |
121
|
|
|
* |
122
|
|
|
* @param string $serialized |
123
|
|
|
*/ |
124
|
2 |
|
public function unserialize( $serialized ) { |
125
|
2 |
|
$this->propertyId = PropertyId::newFromNumber( unserialize( $serialized ) ); |
126
|
2 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $name |
130
|
|
|
* |
131
|
|
|
* @return boolean |
132
|
|
|
*/ |
133
|
|
|
public function hasFacet( $name ) { |
134
|
|
|
return $this->facetManager && $this->facetManager->hasFacet( $name ); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return string[] |
139
|
|
|
*/ |
140
|
|
|
public function listFacets() { |
141
|
|
|
return $this->facetManager ? $this->facetManager->listFacets() : array(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string $name |
146
|
|
|
* @param string|null $type The desired type |
147
|
|
|
* |
148
|
|
|
* @return object |
149
|
|
|
*/ |
150
|
|
|
public function getFacet( $name, $type = null ) { |
151
|
|
|
if ( !$this->facetManager ) { |
152
|
|
|
throw new NoSuchFacetException( $name ); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $this->facetManager->getFacet( $name, $type ); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param string $name |
160
|
|
|
* @param object $facetObject |
161
|
|
|
*/ |
162
|
|
|
public function addFacet( $name, $facetObject ) { |
163
|
|
|
if ( !$this->facetManager ) { |
164
|
|
|
$this->facetManager = new FacetManager(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$this->facetManager->addFacet( $name, $facetObject ); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
} |
171
|
|
|
|