1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PPP\Wikidata\ValueFormatters\JsonLd\Entity; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use OutOfBoundsException; |
7
|
|
|
use stdClass; |
8
|
|
|
use ValueFormatters\FormatterOptions; |
9
|
|
|
use ValueFormatters\ValueFormatter; |
10
|
|
|
use ValueFormatters\ValueFormatterBase; |
11
|
|
|
use Wikibase\DataModel\Entity\EntityDocument; |
12
|
|
|
use Wikibase\DataModel\Statement\StatementList; |
13
|
|
|
use Wikibase\DataModel\StatementListProvider; |
14
|
|
|
use Wikibase\DataModel\Term\Fingerprint; |
15
|
|
|
use Wikibase\DataModel\Term\FingerprintProvider; |
16
|
|
|
use Wikibase\DataModel\Term\Term; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @licence GPLv2+ |
20
|
|
|
* @author Thomas Pellissier Tanon |
21
|
|
|
*/ |
22
|
|
|
class ExtendedJsonLdEntityFormatter extends ValueFormatterBase { |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ValueFormatter |
26
|
|
|
*/ |
27
|
|
|
private $entityFormatter; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ValueFormatter |
31
|
|
|
*/ |
32
|
|
|
private $snakFormatter; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param ValueFormatter $entityFormatter |
36
|
|
|
* @param ValueFormatter $snakFormatter |
37
|
|
|
* @param FormatterOptions $options |
38
|
|
|
*/ |
39
|
1 |
|
public function __construct(ValueFormatter $entityFormatter, ValueFormatter $snakFormatter, FormatterOptions $options) { |
40
|
|
|
|
41
|
1 |
|
$this->entityFormatter = $entityFormatter; |
42
|
1 |
|
$this->snakFormatter = $snakFormatter; |
43
|
|
|
|
44
|
1 |
|
parent::__construct($options); |
45
|
1 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @see ValueFormatter::format |
49
|
|
|
*/ |
50
|
1 |
|
public function format($value) { |
51
|
1 |
|
if(!($value instanceof EntityDocument)) { |
52
|
|
|
throw new InvalidArgumentException('$value is not an EntityDocument.'); |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
return $this->toJsonLd($value); |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
private function toJsonLd(EntityDocument $entity) { |
59
|
1 |
|
$resource = $this->entityFormatter->format($entity); |
60
|
|
|
|
61
|
1 |
|
if($entity instanceof FingerprintProvider) { |
62
|
1 |
|
$this->addFingerprintAliasesToResource($entity->getFingerprint(), $resource); |
63
|
1 |
|
} |
64
|
|
|
|
65
|
1 |
|
if($entity instanceof StatementListProvider) { |
66
|
1 |
|
$this->addStatementListToResource($entity->getStatements(), $resource); |
67
|
1 |
|
} |
68
|
|
|
|
69
|
1 |
|
return $resource; |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
private function addFingerprintAliasesToResource(Fingerprint $fingerprint, stdClass $resource) { |
73
|
1 |
|
$languageCode = $this->getOption(ValueFormatter::OPT_LANG); |
74
|
|
|
|
75
|
|
|
try { |
76
|
1 |
|
$aliasGroup = $fingerprint->getAliasGroup($languageCode); |
77
|
1 |
|
$resource->alternateName = array(); |
78
|
1 |
|
foreach($aliasGroup->getAliases() as $alias) { |
79
|
1 |
|
$resource->alternateName[] = $this->newResourceFromTerm(new Term($aliasGroup->getLanguageCode(), $alias)); |
80
|
1 |
|
} |
81
|
1 |
|
} catch(OutOfBoundsException $e) { |
82
|
|
|
//Just ignore it |
83
|
|
|
} |
84
|
1 |
|
} |
85
|
|
|
|
86
|
1 |
|
private function addStatementListToResource(StatementList $statementList, stdClass $resource) { |
87
|
1 |
|
foreach($statementList->getBestStatements()->getMainSnaks() as $snak) { |
88
|
1 |
|
$formatted = $this->snakFormatter->format($snak); |
89
|
|
|
|
90
|
1 |
|
foreach($formatted as $property => $value) { |
91
|
1 |
|
if(isset($resource->{$property}) && !is_array($resource->{$property})) { |
92
|
|
|
continue; //We do not allow to edit special properties |
93
|
|
|
} |
94
|
1 |
|
$resource->{$property}[] = $value; |
95
|
1 |
|
} |
96
|
1 |
|
} |
97
|
1 |
|
} |
98
|
|
|
|
99
|
1 |
|
private function newResourceFromTerm(Term $term) { |
100
|
1 |
|
$literal = new stdClass(); |
101
|
1 |
|
$literal->{'@language'} = $term->getLanguageCode(); |
102
|
1 |
|
$literal->{'@value'} = $term->getText(); |
103
|
1 |
|
return $literal; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|