|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PPP\Wikidata\TreeSimplifier; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use PPP\DataModel\AbstractNode; |
|
7
|
|
|
use PPP\DataModel\ResourceListNode; |
|
8
|
|
|
use PPP\DataModel\SentenceNode; |
|
9
|
|
|
use PPP\DataModel\StringResourceNode; |
|
10
|
|
|
use PPP\Module\TreeSimplifier\NodeSimplifier; |
|
11
|
|
|
use PPP\Wikidata\ValueParsers\ResourceListNodeParser; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Tries to cast the input sentence to a Wikibase item |
|
15
|
|
|
* |
|
16
|
|
|
* @licence AGPLv3+ |
|
17
|
|
|
* @author Thomas Pellissier Tanon |
|
18
|
|
|
*/ |
|
19
|
|
|
class SentenceNodeSimplifier implements NodeSimplifier { |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var ResourceListNodeParser |
|
23
|
|
|
*/ |
|
24
|
|
|
private $resourceListNodeParser; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param ResourceListNodeParser $resourceListNodeParser |
|
28
|
|
|
*/ |
|
29
|
4 |
|
public function __construct(ResourceListNodeParser $resourceListNodeParser) { |
|
30
|
4 |
|
$this->resourceListNodeParser = $resourceListNodeParser; |
|
31
|
4 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @see NodeSimplifier::isSimplifierFor |
|
35
|
|
|
*/ |
|
36
|
3 |
|
public function isSimplifierFor(AbstractNode $node) { |
|
37
|
3 |
|
return $node instanceof SentenceNode; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @see NodeSimplifier::doSimplification |
|
42
|
|
|
*/ |
|
43
|
1 |
|
public function simplify(AbstractNode $node) { |
|
44
|
1 |
|
if(!$this->isSimplifierFor($node)) { |
|
45
|
1 |
|
throw new InvalidArgumentException('SentenceNodeSimplifier can only simplify SentenceNode objects'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $this->doSimplification($node); |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function doSimplification(SentenceNode $node) { |
|
52
|
|
|
return $this->resourceListNodeParser->parse( |
|
53
|
|
|
new ResourceListNode(array(new StringResourceNode($node->getValue()))), |
|
54
|
|
|
'wikibase-item' |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
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.