1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PPP\Wikidata\TreeSimplifier; |
4
|
|
|
|
5
|
|
|
use PPP\DataModel\ResourceListNode; |
6
|
|
|
use PPP\Wikidata\WikibaseResourceNode; |
7
|
|
|
use Wikibase\DataModel\Entity\EntityDocument; |
8
|
|
|
use Wikibase\DataModel\Entity\EntityId; |
9
|
|
|
use Wikibase\DataModel\Entity\PropertyId; |
10
|
|
|
use Wikibase\DataModel\Snak\PropertyValueSnak; |
11
|
|
|
use Wikibase\DataModel\Statement\StatementListProvider; |
12
|
|
|
use Wikibase\EntityStore\EntityStore; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Simplifies a triple node when the object is missing. |
16
|
|
|
* |
17
|
|
|
* @licence AGPLv3+ |
18
|
|
|
* @author Thomas Pellissier Tanon |
19
|
|
|
*/ |
20
|
|
|
class ResourceListForEntityProperty { |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var EntityStore |
24
|
|
|
*/ |
25
|
|
|
private $entityStore; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param EntityStore $entityStore |
29
|
|
|
*/ |
30
|
4 |
|
public function __construct(EntityStore $entityStore) { |
31
|
4 |
|
$this->entityStore = $entityStore; |
32
|
4 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param EntityId $entityId |
36
|
|
|
* @param PropertyId $propertyId |
37
|
|
|
* @return ResourceListNode |
38
|
|
|
*/ |
39
|
4 |
|
public function getForEntityProperty(EntityId $entityId, PropertyId $propertyId) { |
40
|
4 |
|
$entity = $this->entityStore->getEntityDocumentLookup()->getEntityDocumentForId($entityId); |
41
|
|
|
|
42
|
4 |
|
if($entity === null) { |
43
|
|
|
return array(); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
4 |
|
$snaks = $this->getSnaksForProperty($entity, $propertyId); |
47
|
4 |
|
return new ResourceListNode($this->snaksToNodes($snaks, $entityId, $propertyId)); |
48
|
|
|
} |
49
|
|
|
|
50
|
4 |
|
private function getSnaksForProperty(EntityDocument $entity, PropertyId $propertyId) { |
51
|
4 |
|
if(!$entity instanceof StatementListProvider) { |
52
|
|
|
return array(); |
53
|
|
|
} |
54
|
|
|
|
55
|
4 |
|
return $entity->getStatements()->getByPropertyId($propertyId)->getBestStatements()->getMainSnaks(); |
56
|
|
|
} |
57
|
|
|
|
58
|
4 |
|
private function snaksToNodes(array $snaks, EntityId $fromSubject, PropertyId $fromProperty) { |
59
|
4 |
|
$nodes = array(); |
60
|
|
|
|
61
|
4 |
|
foreach($snaks as $snak) { |
62
|
3 |
|
if($snak instanceof PropertyValueSnak) { |
63
|
2 |
|
$nodes[] = new WikibaseResourceNode('', $snak->getDataValue(), $fromSubject, $fromProperty); |
64
|
2 |
|
} |
65
|
|
|
//TODO case of PropertySomeValueSnak (MissingNode) and PropertyNoValueSnak (return the negation of the triple?) |
66
|
4 |
|
} |
67
|
|
|
|
68
|
4 |
|
return $nodes; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.