|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PPP\Wikidata\ValueFormatters\JsonLd\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use PPP\Wikidata\ValueFormatters\JsonLd\JsonLdDataValueFormatter; |
|
7
|
|
|
use ValueFormatters\FormatterOptions; |
|
8
|
|
|
use ValueFormatters\ValueFormatterBase; |
|
9
|
|
|
use Wikibase\DataModel\Entity\PropertyId; |
|
10
|
|
|
use Wikibase\DataModel\Services\Lookup\PropertyLookup; |
|
11
|
|
|
use Wikibase\DataModel\Snak\PropertyValueSnak; |
|
12
|
|
|
use Wikibase\DataModel\Snak\Snak; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @licence AGPLv3+ |
|
16
|
|
|
* @author Thomas Pellissier Tanon |
|
17
|
|
|
*/ |
|
18
|
|
|
class JsonLdSnakFormatter extends ValueFormatterBase { |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Array of allowed vocabularies to map snaks to. |
|
22
|
|
|
* Example: array('http://schema.org/') |
|
23
|
|
|
*/ |
|
24
|
|
|
const OPT_ALLOWED_VOCABULARIES = 'allowed-vocabularies'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var PropertyLookup |
|
28
|
|
|
*/ |
|
29
|
|
|
private $propertyLookup; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var EntityOntology |
|
33
|
|
|
*/ |
|
34
|
|
|
private $entityOntology; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var JsonLdDataValueFormatter |
|
38
|
|
|
*/ |
|
39
|
|
|
private $dataValueFormatter; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param PropertyLookup $propertyLookup |
|
43
|
|
|
* @param EntityOntology $entityOntology |
|
44
|
|
|
* @param JsonLdDataValueFormatter $dataValueFormatter |
|
45
|
|
|
* @param FormatterOptions $options |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct( |
|
48
|
|
|
PropertyLookup $propertyLookup, |
|
49
|
|
|
EntityOntology $entityOntology, |
|
50
|
|
|
JsonLdDataValueFormatter $dataValueFormatter, |
|
51
|
|
|
FormatterOptions $options |
|
52
|
|
|
) { |
|
53
|
|
|
$this->propertyLookup = $propertyLookup; |
|
54
|
|
|
$this->entityOntology = $entityOntology; |
|
55
|
|
|
$this->dataValueFormatter = $dataValueFormatter; |
|
56
|
|
|
|
|
57
|
|
|
parent::__construct($options); |
|
58
|
|
|
|
|
59
|
|
|
$this->requireOption(self::OPT_ALLOWED_VOCABULARIES); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @see ValueFormatter::format |
|
64
|
|
|
*/ |
|
65
|
3 |
|
public function format($value) { |
|
66
|
3 |
|
if(!($value instanceof Snak)) { |
|
67
|
|
|
throw new InvalidArgumentException('$value is not a Snak.'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
3 |
|
return $this->toJsonLdProperties($value); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
3 |
|
private function toJsonLdProperties(Snak $snak) { |
|
74
|
3 |
|
if(!($snak instanceof PropertyValueSnak)) { |
|
75
|
1 |
|
return array(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
2 |
|
return array_fill_keys( |
|
79
|
2 |
|
$this->normalizePropertyIris($this->getPropertyIris($snak->getPropertyId())), |
|
80
|
2 |
|
$this->dataValueFormatter->format($snak->getDataValue()) |
|
81
|
2 |
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
2 |
|
private function getPropertyIris(PropertyId $propertyId) { |
|
85
|
2 |
|
$property = $this->propertyLookup->getPropertyForId($propertyId); |
|
86
|
2 |
|
if($property === null) { |
|
87
|
1 |
|
return array(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
return $this->filterIris($this->entityOntology->getEquivalentPropertiesIris($property)); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
private function filterIris(array $iris) { |
|
94
|
1 |
|
$regex = $this->buildIriFilterRegex(); |
|
95
|
1 |
|
return array_filter( |
|
96
|
1 |
|
$iris, |
|
97
|
|
|
function($iri) use ($regex) { |
|
98
|
1 |
|
return preg_match($regex, $iri); |
|
99
|
|
|
} |
|
100
|
1 |
|
); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
1 |
|
private function buildIriFilterRegex() { |
|
104
|
1 |
|
$escaped = array_map( |
|
105
|
|
|
function($prefix) { |
|
106
|
1 |
|
return '(' . preg_quote($prefix, '/') . ')'; |
|
107
|
1 |
|
}, |
|
108
|
1 |
|
$this->getOption(self::OPT_ALLOWED_VOCABULARIES) |
|
109
|
1 |
|
); |
|
110
|
|
|
|
|
111
|
1 |
|
return '/^' . implode('|', $escaped) . '/'; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
2 |
|
private function normalizePropertyIris($iris) { |
|
115
|
2 |
|
return array_map( |
|
116
|
2 |
|
function($iri) { |
|
117
|
1 |
|
return str_replace('http://schema.org/', '', $iri); |
|
118
|
2 |
|
}, |
|
119
|
|
|
$iris |
|
120
|
2 |
|
); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|