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
|
|
|
public function __construct(FormatterOptions $options) { |
31
|
|
|
parent::__construct($options); |
32
|
|
|
|
33
|
|
|
$this->requireOption(self::OPT_ENTITY_BASE_URI); |
34
|
|
|
} |
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
|
|
|
return $this->toJsonLd($value); |
45
|
|
|
} |
46
|
|
|
|
47
|
4 |
|
private function toJsonLd(EntityDocument $entity) { |
48
|
|
|
$resource = new stdClass(); |
49
|
4 |
|
$resource->{'@type'} = 'Thing'; |
50
|
|
|
$resource->name = $entity->getId()->getSerialization(); |
51
|
|
|
$resource->{'@id'} = $this->getOption(JsonLdEntityFormatter::OPT_ENTITY_BASE_URI) . $entity->getId()->getSerialization(); |
|
|
|
|
52
|
|
|
|
53
|
4 |
|
if($entity instanceof FingerprintProvider) { |
54
|
|
|
$this->addFingerprintToResource($entity->getFingerprint(), $resource); |
55
|
|
|
} |
56
|
|
|
|
57
|
4 |
|
return $resource; |
58
|
4 |
|
} |
59
|
|
|
|
60
|
4 |
|
private function addFingerprintToResource(Fingerprint $fingerprint, stdClass $resource) { |
61
|
|
|
$languageCode = $this->getOption(ValueFormatter::OPT_LANG); |
62
|
|
|
|
63
|
|
|
try { |
64
|
1 |
|
$resource->name = $this->newResourceFromTerm($fingerprint->getLabel($languageCode)); |
65
|
|
|
} catch(OutOfBoundsException $e) { |
66
|
|
|
//Just ignore it |
67
|
4 |
|
} |
68
|
|
|
|
69
|
|
|
try { |
70
|
3 |
|
$resource->description = $this->newResourceFromTerm($fingerprint->getDescription($languageCode)); |
71
|
|
|
} catch(OutOfBoundsException $e) { |
72
|
|
|
//Just ignore it |
73
|
4 |
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
2 |
|
private function newResourceFromTerm(Term $term) { |
77
|
|
|
$literal = new stdClass(); |
78
|
|
|
$literal->{'@language'} = $term->getLanguageCode(); |
79
|
|
|
$literal->{'@value'} = $term->getText(); |
80
|
2 |
|
return $literal; |
81
|
2 |
|
} |
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_CBC
could 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.