Completed
Push — master ( 7880aa...3f359c )
by Thomas
05:14
created

SpecificTripleNodeSimplifier::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1.008

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 5
ccs 4
cts 5
cp 0.8
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
crap 1.008
1
<?php
2
3
namespace PPP\Wikidata\TreeSimplifier;
4
5
use DataValues\TimeValue;
6
use DateInterval;
7
use DateTime;
8
use InvalidArgumentException;
9
use PPP\DataModel\AbstractNode;
10
use PPP\DataModel\IntersectionNode;
11
use PPP\DataModel\JsonLdResourceNode;
12
use PPP\DataModel\MissingNode;
13
use PPP\DataModel\ResourceListNode;
14
use PPP\DataModel\ResourceNode;
15
use PPP\DataModel\StringResourceNode;
16
use PPP\DataModel\TripleNode;
17
use PPP\DataModel\UnionNode;
18
use PPP\Module\TreeSimplifier\NodeSimplifier;
19
use PPP\Wikidata\ValueParsers\ResourceListNodeParser;
20
use PPP\Wikidata\WikibaseResourceNode;
21
use stdClass;
22
use Wikibase\DataModel\Entity\EntityIdValue;
23
use Wikibase\DataModel\Entity\ItemId;
24
use Wikibase\DataModel\Entity\PropertyId;
25
26
/**
27
 * Do some actions for specific use case:
28
 * - if a predicate is not useful like "name" or "identity" cast subjects to wikibase items
29
 * - if the predicte is son or daughter use "child" with an intersection with the relevant sex
30
 *
31
 * @licence GPLv2+
32
 * @author Thomas Pellissier Tanon
33
 *
34
 *
35
 * TODO: remove hardcoded values?
36
 */
37
class SpecificTripleNodeSimplifier implements NodeSimplifier {
38
39
	private static $MEANINGLESS_PREDICATES = array(
40
		'name',
41
		'identity',
42
		'definition'
43
	);
44
45
	const PROPERTY_CHILD = 'P40';
46
	const PROPERTY_SEX = 'P21';
47
	const PROPERTY_BIRTH_DATE = 'P569';
48
	const ITEM_MALE = 'Q6581097';
49
	const ITEM_FEMALE = 'Q6581072';
50
51
	/**
52
	 * @var ResourceListNodeParser
53
	 */
54
	private $resourceListNodeParser;
55
56
	/**
57
	 * @var ResourceListForEntityProperty
58
	 */
59
	private $resourceListForEntityProperty;
60
61
	/**
62
	 * @var DateTime
63
	 */
64
	private $now;
65
66
	/**
67
	 * @param ResourceListNodeParser $resourceListNodeParser
68
	 * @param ResourceListForEntityProperty $resourceListForEntityProperty
69
	 * @param DateTime $now
70
	 */
71 11
	public function __construct(ResourceListNodeParser $resourceListNodeParser, ResourceListForEntityProperty $resourceListForEntityProperty, DateTime $now) {
72 11
		$this->resourceListNodeParser = $resourceListNodeParser;
73 11
		$this->resourceListForEntityProperty = $resourceListForEntityProperty;
74 11
		$this->now = $now;
75
	}
76
77
	/**
78
	 * @see NodeSimplifier::isSimplifierFor
79
	 */
80 10
	public function isSimplifierFor(AbstractNode $node) {
81 10
		return $node instanceof TripleNode &&
82
			$node->getSubject() instanceof ResourceListNode &&
83
			$node->getPredicate() instanceof ResourceListNode &&
84 2
			$node->getObject() instanceof MissingNode;
85 10
	}
86
87
	/**
88
	 * @see NodeSimplifier::doSimplification
89
	 */
90 8
	public function simplify(AbstractNode $node) {
91
		if(!$this->isSimplifierFor($node)) {
92
			throw new InvalidArgumentException('SpecificTripleNodeSimplifier can only clean TripleNode objects');
93
		}
94
95
		return $this->doSimplification($node);
0 ignored issues
show
Compatibility introduced by
$node of type object<PPP\DataModel\AbstractNode> is not a sub-type of object<PPP\DataModel\TripleNode>. It seems like you assume a child class of the class PPP\DataModel\AbstractNode to be always present.

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.

Loading history...
96 8
	}
97
98 7
	public function doSimplification(TripleNode $node) {
99 7
		$additionalNodes = array();
100 7
		$otherPredicates = array();
101
102
		/** @var ResourceNode $predicate */
103 7
		foreach($node->getPredicate() as $predicate) {
0 ignored issues
show
Bug introduced by
The expression $node->getPredicate() of type object<PPP\DataModel\AbstractNode> is not traversable.
Loading history...
104
			if(in_array($predicate->getValue(), self::$MEANINGLESS_PREDICATES)) {
105
				$additionalNodes[] = $this->resourceListNodeParser->parse($node->getSubject(), 'wikibase-item');
0 ignored issues
show
Compatibility introduced by
$node->getSubject() of type object<PPP\DataModel\AbstractNode> is not a sub-type of object<PPP\DataModel\ResourceListNode>. It seems like you assume a child class of the class PPP\DataModel\AbstractNode to be always present.

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.

Loading history...
106
			} else if($predicate->equals(new StringResourceNode('son'))) {
107
				$additionalNodes[] = $this->buildSonNode($node);
108
			} else if($predicate->equals(new StringResourceNode('daughter'))) {
109
				$additionalNodes[] = $this->buildDaughterNode($node);
110
			} else if($predicate->equals(new StringResourceNode('age'))) {
111
				$additionalNodes[] = $this->buildAgeNode($node);
112
			} else {
113 2
				$otherPredicates[] = $predicate;
114 6
			}
115
		}
116
117 7
		if(!empty($otherPredicates)) {
118
			$additionalNodes[] = new TripleNode($node->getSubject(), new ResourceListNode($otherPredicates), $node->getObject());
119
		}
120
121
		if(count($additionalNodes) === 1) {
122 6
			return $additionalNodes[0];
123
		}
124
125
		return new UnionNode($additionalNodes);
126 7
	}
127
128 1
	private function buildSonNode(TripleNode $node) {
129
		return new IntersectionNode(array(
130
			new TripleNode(
131 1
				$node->getSubject(),
132
				new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId(self::PROPERTY_CHILD))))),
133 1
				$node->getObject()
134
			),
135
			new TripleNode(
136 1
				$node->getObject(),
137
				new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId(self::PROPERTY_SEX))))),
138
				new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new ItemId(self::ITEM_MALE)))))
139
			),
140
		));
141 1
	}
142
143 1
	private function buildDaughterNode(TripleNode $node) {
144
		return new IntersectionNode(array(
145
			new TripleNode(
146 1
				$node->getSubject(),
147
				new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId(self::PROPERTY_CHILD))))),
148 1
				$node->getObject()
149
			),
150
			new TripleNode(
151 1
				$node->getObject(),
152
				new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId(self::PROPERTY_SEX))))),
153
				new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new ItemId(self::ITEM_FEMALE)))))
154
			),
155
		));
156 1
	}
157
158 1
	private function buildAgeNode(TripleNode $node) {
159
		$subjectNodes = $this->resourceListNodeParser->parse($node->getSubject(), 'wikibase-item');
0 ignored issues
show
Compatibility introduced by
$node->getSubject() of type object<PPP\DataModel\AbstractNode> is not a sub-type of object<PPP\DataModel\ResourceListNode>. It seems like you assume a child class of the class PPP\DataModel\AbstractNode to be always present.

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.

Loading history...
160 1
		$ageValues = array();
161
162
		/** @var WikibaseResourceNode $subjectNode */
163
		foreach($subjectNodes as $subjectNode) {
164 1
			$birthDateObjects = $this->resourceListForEntityProperty->getForEntityProperty(
165
				$subjectNode->getDataValue()->getEntityId(),
166
				new PropertyId(self::PROPERTY_BIRTH_DATE)
167
			);
168
169
			/** @var WikibaseResourceNode $birthDateObject */
170
			foreach($birthDateObjects as $birthDateObject) {
171
				$ageValues[] = $this->formatAgeValue($this->computeAge($birthDateObject->getDataValue()));
172
			}
173
		}
174
175
		return new ResourceListNode($ageValues);
176 1
	}
177
178 1
	private function computeAge(TimeValue $birthDateValue) {
179
		$birthDate = new DateTime(preg_replace('/^\+0*/', '', $birthDateValue->getTime()));
180
		return $this->now->diff($birthDate);
181 1
	}
182
183
	//TODO: implement a DurationValue?
184 1
	private function formatAgeValue(DateInterval $age) {
185
		$formattedAge = $age->format('%y');
186
187
		$literal = new stdClass();
188 1
		$literal->{'@type'} = 'Duration';
189
		$literal->{'@value'} = $age->format('P%yY%mM%dD');
190
191
		$resource = new stdClass();
192 1
		$resource->{'@context'} = 'http://schema.org';
193 1
		$resource->{'@type'} = 'Duration';
194 1
		$resource->{'name'} = $formattedAge;
195 1
		$resource->{'http://www.w3.org/1999/02/22-rdf-syntax-ns#value'} = $literal;
196
197
		return new JsonLdResourceNode($formattedAge, $resource);
198 1
	}
199
}
200