|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PPP\Wikidata\TreeSimplifier; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use PPP\DataModel\AbstractNode; |
|
7
|
|
|
use PPP\DataModel\MissingNode; |
|
8
|
|
|
use PPP\DataModel\ResourceListNode; |
|
9
|
|
|
use PPP\DataModel\TripleNode; |
|
10
|
|
|
use PPP\Module\TreeSimplifier\NodeSimplifier; |
|
11
|
|
|
use PPP\Wikidata\ValueParsers\ResourceListNodeParser; |
|
12
|
|
|
use PPP\Wikidata\WikibaseResourceNode; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Simplifies a triple node when the object is missing. |
|
16
|
|
|
* |
|
17
|
|
|
* @licence GPLv2+ |
|
18
|
|
|
* @author Thomas Pellissier Tanon |
|
19
|
|
|
*/ |
|
20
|
|
|
class MissingObjectTripleNodeSimplifier implements NodeSimplifier { |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var ResourceListNodeParser |
|
24
|
|
|
*/ |
|
25
|
|
|
private $resourceListNodeParser; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var ResourceListForEntityProperty |
|
29
|
|
|
*/ |
|
30
|
|
|
private $resourceListForEntityProperty; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param ResourceListNodeParser $resourceListNodeParser |
|
34
|
|
|
* @param ResourceListForEntityProperty $resourceListForEntityProperty |
|
35
|
|
|
*/ |
|
36
|
13 |
|
public function __construct(ResourceListNodeParser $resourceListNodeParser, ResourceListForEntityProperty $resourceListForEntityProperty) { |
|
37
|
13 |
|
$this->resourceListNodeParser = $resourceListNodeParser; |
|
38
|
13 |
|
$this->resourceListForEntityProperty = $resourceListForEntityProperty; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @see NodeSimplifier::isSimplifierFor |
|
43
|
|
|
*/ |
|
44
|
12 |
|
public function isSimplifierFor(AbstractNode $node) { |
|
45
|
12 |
|
return $node instanceof TripleNode && |
|
46
|
|
|
$node->getSubject() instanceof ResourceListNode && |
|
47
|
|
|
$node->getPredicate() instanceof ResourceListNode && |
|
48
|
6 |
|
$node->getObject() instanceof MissingNode; |
|
49
|
12 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @see NodeSimplifier::doSimplification |
|
53
|
|
|
*/ |
|
54
|
7 |
|
public function simplify(AbstractNode $node) { |
|
55
|
|
|
if(!$this->isSimplifierFor($node)) { |
|
56
|
|
|
throw new InvalidArgumentException('MissingObjectTripleNodeSimplifier can only simplify TripleNode with a missing object'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $this->doSimplification($node); |
|
|
|
|
|
|
60
|
7 |
|
} |
|
61
|
|
|
|
|
62
|
4 |
|
private function doSimplification(TripleNode $node) { |
|
63
|
|
|
$subjectNodes = $this->resourceListNodeParser->parse($node->getSubject(), 'wikibase-item'); |
|
|
|
|
|
|
64
|
|
|
$propertyNodes = $this->resourceListNodeParser->parse($node->getPredicate(), 'wikibase-property'); |
|
|
|
|
|
|
65
|
4 |
|
$results = array(); |
|
66
|
|
|
|
|
67
|
|
|
foreach($subjectNodes as $subject) { |
|
68
|
|
|
foreach($propertyNodes as $predicate) { |
|
69
|
|
|
$results[] = $this->getNodesForObject($subject, $predicate); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$node = new ResourceListNode($results); |
|
74
|
4 |
|
return $node; |
|
75
|
4 |
|
} |
|
76
|
|
|
|
|
77
|
4 |
|
protected function getNodesForObject(WikibaseResourceNode $subject, WikibaseResourceNode $predicate) { |
|
78
|
4 |
|
return $this->resourceListForEntityProperty->getForEntityProperty( |
|
79
|
|
|
$subject->getDataValue()->getEntityId(), |
|
|
|
|
|
|
80
|
|
|
$predicate->getDataValue()->getEntityId() |
|
|
|
|
|
|
81
|
|
|
); |
|
82
|
4 |
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.