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 AGPLv3+ |
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
|
13 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @see NodeSimplifier::isSimplifierFor |
43
|
|
|
*/ |
44
|
12 |
|
public function isSimplifierFor(AbstractNode $node) { |
45
|
12 |
|
return $node instanceof TripleNode && |
46
|
12 |
|
$node->getSubject() instanceof ResourceListNode && |
47
|
12 |
|
$node->getPredicate() instanceof ResourceListNode && |
48
|
12 |
|
$node->getObject() instanceof MissingNode; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @see NodeSimplifier::doSimplification |
53
|
|
|
*/ |
54
|
7 |
|
public function simplify(AbstractNode $node) { |
55
|
7 |
|
if(!$this->isSimplifierFor($node)) { |
56
|
3 |
|
throw new InvalidArgumentException('MissingObjectTripleNodeSimplifier can only simplify TripleNode with a missing object'); |
57
|
|
|
} |
58
|
|
|
|
59
|
4 |
|
return $this->doSimplification($node); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
4 |
|
private function doSimplification(TripleNode $node) { |
63
|
4 |
|
$subjectNodes = $this->resourceListNodeParser->parse($node->getSubject(), 'wikibase-item'); |
|
|
|
|
64
|
4 |
|
$propertyNodes = $this->resourceListNodeParser->parse($node->getPredicate(), 'wikibase-property'); |
|
|
|
|
65
|
4 |
|
$results = array(); |
66
|
|
|
|
67
|
4 |
|
foreach($subjectNodes as $subject) { |
68
|
4 |
|
foreach($propertyNodes as $predicate) { |
69
|
4 |
|
$results[] = $this->getNodesForObject($subject, $predicate); |
70
|
4 |
|
} |
71
|
4 |
|
} |
72
|
|
|
|
73
|
4 |
|
$node = new ResourceListNode($results); |
74
|
4 |
|
return $node; |
75
|
|
|
} |
76
|
|
|
|
77
|
4 |
|
protected function getNodesForObject(WikibaseResourceNode $subject, WikibaseResourceNode $predicate) { |
78
|
4 |
|
return $this->resourceListForEntityProperty->getForEntityProperty( |
79
|
4 |
|
$subject->getDataValue()->getEntityId(), |
|
|
|
|
80
|
4 |
|
$predicate->getDataValue()->getEntityId() |
|
|
|
|
81
|
4 |
|
); |
82
|
|
|
} |
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.