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\ValueFormatter; |
9
|
|
|
use ValueFormatters\ValueFormatterBase; |
10
|
|
|
use Wikibase\DataModel\Entity\EntityIdValue; |
11
|
|
|
use Wikibase\DataModel\Entity\Item; |
12
|
|
|
use Wikibase\DataModel\Entity\ItemId; |
13
|
|
|
use Wikibase\DataModel\Entity\Property; |
14
|
|
|
use Wikibase\DataModel\Entity\PropertyId; |
15
|
|
|
use Wikibase\DataModel\Services\Lookup\ItemLookup; |
16
|
|
|
use Wikibase\DataModel\Services\Lookup\PropertyLookup; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @licence AGPLv3+ |
20
|
|
|
* @author Thomas Pellissier Tanon |
21
|
|
|
*/ |
22
|
|
|
class JsonLdEntityIdFormatter extends ValueFormatterBase implements JsonLdDataValueFormatter { |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ItemLookup |
26
|
|
|
*/ |
27
|
|
|
private $itemLookup; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var JsonLdItemFormatter |
31
|
|
|
*/ |
32
|
|
|
private $itemFormatter; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var PropertyLookup |
36
|
|
|
*/ |
37
|
|
|
private $propertyLookup; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var JsonLdPropertyFormatter |
41
|
|
|
*/ |
42
|
|
|
private $propertyFormatter; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param ItemLookup $itemLookup |
46
|
|
|
* @param ValueFormatter $itemFormatter |
47
|
|
|
* @param PropertyLookup $propertyLookup |
48
|
|
|
* @param ValueFormatter $propertyFormatter |
49
|
|
|
* @param FormatterOptions $options |
50
|
|
|
*/ |
51
|
|
|
public function __construct( |
52
|
|
|
ItemLookup $itemLookup, |
53
|
|
|
ValueFormatter $itemFormatter, |
54
|
|
|
PropertyLookup $propertyLookup, |
55
|
|
|
ValueFormatter $propertyFormatter, |
56
|
|
|
FormatterOptions $options |
57
|
|
|
) { |
58
|
|
|
$this->itemLookup = $itemLookup; |
59
|
|
|
$this->itemFormatter = $itemFormatter; |
|
|
|
|
60
|
|
|
$this->propertyLookup = $propertyLookup; |
61
|
|
|
$this->propertyFormatter = $propertyFormatter; |
|
|
|
|
62
|
|
|
|
63
|
|
|
parent::__construct($options); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @see ValueFormatter::format |
68
|
|
|
*/ |
69
|
4 |
|
public function format($value) { |
70
|
4 |
|
if(!($value instanceof EntityIdValue)) { |
71
|
|
|
throw new InvalidArgumentException('$value is not a EntityIdValue.'); |
72
|
|
|
} |
73
|
|
|
|
74
|
4 |
|
return $this->toJsonLd($value); |
75
|
|
|
} |
76
|
|
|
|
77
|
4 |
|
private function toJsonLd(EntityIdValue $value) { |
78
|
4 |
|
$entityId = $value->getEntityId(); |
79
|
|
|
|
80
|
4 |
|
if($entityId instanceof ItemId) { |
81
|
2 |
|
return $this->itemIdToJsonLd($entityId); |
82
|
2 |
|
} elseif($entityId instanceof PropertyId) { |
83
|
2 |
|
return $this->propertyIdToJsonLd($entityId); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
throw new InvalidArgumentException('Unsupported entity type: ' . $entityId->getEntityType()); |
87
|
|
|
} |
88
|
|
|
|
89
|
2 |
|
private function itemIdToJsonLd(ItemId $itemId) { |
90
|
2 |
|
$item = $this->itemLookup->getItemForId($itemId); |
91
|
2 |
|
if($item === null) { |
92
|
1 |
|
$item = new Item($itemId); |
93
|
1 |
|
} |
94
|
|
|
|
95
|
2 |
|
return $this->itemFormatter->format($item); |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
private function propertyIdToJsonLd(PropertyId $propertyId) { |
99
|
2 |
|
$property = $this->propertyLookup->getPropertyForId($propertyId); |
100
|
2 |
|
if($property === null) { |
101
|
1 |
|
$property = new Property($propertyId, null, ''); |
102
|
1 |
|
} |
103
|
|
|
|
104
|
2 |
|
return $this->propertyFormatter->format($property); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.