|
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\Term\Fingerprint; |
|
13
|
|
|
use Wikibase\DataModel\Term\FingerprintProvider; |
|
14
|
|
|
use Wikibase\DataModel\Term\Term; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @licence GPLv2+ |
|
18
|
|
|
* @author Thomas Pellissier Tanon |
|
19
|
|
|
*/ |
|
20
|
|
|
class JsonLdEntityFormatter extends ValueFormatterBase { |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Base URI for Wikibase entities. For Wikidata it is "http://www.wikidata.org/entity/" |
|
24
|
|
|
*/ |
|
25
|
|
|
const OPT_ENTITY_BASE_URI = 'entity-baseuri'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param FormatterOptions $options |
|
29
|
|
|
*/ |
|
30
|
4 |
|
public function __construct(FormatterOptions $options) { |
|
31
|
4 |
|
parent::__construct($options); |
|
32
|
|
|
|
|
33
|
4 |
|
$this->requireOption(self::OPT_ENTITY_BASE_URI); |
|
34
|
4 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @see ValueFormatter::format |
|
38
|
|
|
*/ |
|
39
|
4 |
|
public function format($value) { |
|
40
|
4 |
|
if(!($value instanceof EntityDocument)) { |
|
41
|
|
|
throw new InvalidArgumentException('$value is not an EntityDocument.'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
4 |
|
return $this->toJsonLd($value); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
4 |
|
private function toJsonLd(EntityDocument $entity) { |
|
48
|
4 |
|
$resource = new stdClass(); |
|
49
|
4 |
|
$resource->{'@type'} = 'Thing'; |
|
50
|
4 |
|
$resource->name = $entity->getId()->getSerialization(); |
|
51
|
4 |
|
$resource->{'@id'} = $this->getOption(JsonLdEntityFormatter::OPT_ENTITY_BASE_URI) . $entity->getId()->getSerialization(); |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
4 |
|
if($entity instanceof FingerprintProvider) { |
|
54
|
4 |
|
$this->addFingerprintToResource($entity->getFingerprint(), $resource); |
|
55
|
4 |
|
} |
|
56
|
|
|
|
|
57
|
4 |
|
return $resource; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
4 |
|
private function addFingerprintToResource(Fingerprint $fingerprint, stdClass $resource) { |
|
61
|
4 |
|
$languageCode = $this->getOption(ValueFormatter::OPT_LANG); |
|
62
|
|
|
|
|
63
|
|
|
try { |
|
64
|
4 |
|
$resource->name = $this->newResourceFromTerm($fingerprint->getLabel($languageCode)); |
|
65
|
4 |
|
} catch(OutOfBoundsException $e) { |
|
66
|
|
|
//Just ignore it |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
try { |
|
70
|
4 |
|
$resource->description = $this->newResourceFromTerm($fingerprint->getDescription($languageCode)); |
|
71
|
4 |
|
} catch(OutOfBoundsException $e) { |
|
72
|
|
|
//Just ignore it |
|
73
|
|
|
} |
|
74
|
4 |
|
} |
|
75
|
|
|
|
|
76
|
3 |
|
private function newResourceFromTerm(Term $term) { |
|
77
|
3 |
|
$literal = new stdClass(); |
|
78
|
3 |
|
$literal->{'@language'} = $term->getLanguageCode(); |
|
79
|
3 |
|
$literal->{'@value'} = $term->getText(); |
|
80
|
3 |
|
return $literal; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
This check looks for accesses to local static members using the fully qualified name instead of
self::.While this is perfectly valid, the fully qualified name of
Certificate::TRIPLEDES_CBCcould just as well be replaced byself::TRIPLEDES_CBC. Referencing local members withself::assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.