1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Queryr\Resources\Builders; |
4
|
|
|
|
5
|
|
|
use DataValues\DataValue; |
6
|
|
|
use DataValues\StringValue; |
7
|
|
|
use Queryr\Resources\SimpleStatement; |
8
|
|
|
use Wikibase\DataModel\Entity\EntityId; |
9
|
|
|
use Wikibase\DataModel\Entity\EntityIdValue; |
10
|
|
|
use Wikibase\DataModel\Entity\PropertyId; |
11
|
|
|
use Wikibase\DataModel\Snak\PropertyValueSnak; |
12
|
|
|
use Wikibase\DataModel\Statement\Statement; |
13
|
|
|
use Wikibase\DataModel\Statement\StatementList; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @licence GNU GPL v2+ |
17
|
|
|
* @author Jeroen De Dauw < [email protected] > |
18
|
|
|
*/ |
19
|
|
|
class SimpleStatementsBuilder { |
20
|
|
|
|
21
|
|
|
private $languageCode; |
22
|
|
|
private $labelLookup; |
23
|
|
|
|
24
|
3 |
|
public function __construct( $languageCode, ResourceLabelLookup $labelLookup ) { |
25
|
3 |
|
$this->languageCode = $languageCode; |
26
|
3 |
|
$this->labelLookup = $labelLookup; |
27
|
3 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param StatementList $statements |
31
|
|
|
* |
32
|
|
|
* @return array |
33
|
|
|
*/ |
34
|
3 |
|
public function buildFromStatements( StatementList $statements ) { |
35
|
3 |
|
$simpleStatements = []; |
36
|
|
|
|
37
|
3 |
|
foreach ( $statements->getPropertyIds() as $propertyId ) { |
38
|
3 |
|
$simpleStatement = new SimpleStatement(); |
39
|
|
|
|
40
|
3 |
|
$statementValues = $this->getStatementValuesWithPropertyId( $statements, $propertyId ); |
41
|
|
|
|
42
|
3 |
|
if ( !empty( $statementValues ) ) { |
43
|
3 |
|
$simpleStatement->values = $statementValues; |
44
|
3 |
|
$simpleStatement->valueType = $statementValues[0]->getType(); |
45
|
3 |
|
$simpleStatement->propertyName = $this->getEntityName( $propertyId ); |
46
|
3 |
|
$simpleStatement->propertyId = $propertyId; |
47
|
|
|
|
48
|
3 |
|
$simpleStatements[] = $simpleStatement; |
49
|
3 |
|
} |
50
|
3 |
|
} |
51
|
|
|
|
52
|
3 |
|
return $simpleStatements; |
53
|
|
|
} |
54
|
|
|
|
55
|
3 |
|
private function getEntityName( EntityId $id ) { |
56
|
3 |
|
$label = $this->labelLookup->getLabelByIdAndLanguage( $id, $this->languageCode ); |
57
|
|
|
|
58
|
3 |
|
return $label === null ? $id->getSerialization() : $label; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param StatementList $statements |
63
|
|
|
* @param PropertyId $propertyId |
64
|
|
|
* |
65
|
|
|
* @return DataValue[] |
66
|
|
|
*/ |
67
|
3 |
|
private function getStatementValuesWithPropertyId( StatementList $statements, PropertyId $propertyId ) { |
68
|
3 |
|
$statementValues = []; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var Statement $statement |
72
|
|
|
*/ |
73
|
3 |
|
foreach ( $statements->getByPropertyId( $propertyId )->getBestStatements() as $statement ) { |
74
|
3 |
|
$snak = $statement->getMainSnak(); |
75
|
|
|
|
76
|
3 |
|
if ( $snak instanceof PropertyValueSnak ) { |
77
|
3 |
|
$statementValues[] = $this->handle( $snak ); |
78
|
3 |
|
} |
79
|
3 |
|
} |
80
|
|
|
|
81
|
3 |
|
return $statementValues; |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
|
private function handle( PropertyValueSnak $snak ) { |
85
|
3 |
|
$value = $snak->getDataValue(); |
86
|
|
|
|
87
|
3 |
|
if ( $value instanceof EntityIdValue ) { |
88
|
2 |
|
return new StringValue( $this->getEntityName( $value->getEntityId() ) ); |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
return $value; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
} |