1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PPP\DataModel; |
4
|
|
|
|
5
|
|
|
use stdClass; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* A JSON-LD resource node. |
9
|
|
|
* |
10
|
|
|
* @licence AGPLv3+ |
11
|
|
|
* @author Thomas Pellissier Tanon |
12
|
|
|
*/ |
13
|
|
|
class JsonLdResourceNode extends ResourceNode { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var stdClass |
17
|
|
|
*/ |
18
|
|
|
private $graph; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param string $value |
22
|
|
|
* @param stdClass $graph |
23
|
|
|
*/ |
24
|
4 |
|
public function __construct($value, stdClass $graph) { |
25
|
4 |
|
$this->setJsonLdDocumentLoader(); |
26
|
4 |
|
$this->graph = jsonld_compact($graph, 'http://schema.org'); |
27
|
|
|
|
28
|
4 |
|
parent::__construct($value); |
29
|
4 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return stdClass the compacted graph with as @context http://schema.org |
33
|
|
|
*/ |
34
|
1 |
|
public function getGraph() { |
35
|
1 |
|
return $this->graph; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
1 |
|
public function getValueType() { |
42
|
1 |
|
return 'resource-jsonld'; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @see AbstractNode::equals |
47
|
|
|
*/ |
48
|
9 |
|
public function equals($target) { |
49
|
9 |
|
return $target instanceof self && $this->resourceEquals($this->graph, $target->graph); |
50
|
|
|
} |
51
|
|
|
|
52
|
3 |
|
private function nodeEquals($a, $b) { |
53
|
3 |
|
if(is_array($a) && is_array($b)) { |
54
|
|
|
return $this->arrayEquals($a, $b); |
55
|
3 |
|
} elseif($a instanceof stdClass && $b instanceof stdClass) { |
56
|
1 |
|
return $this->resourceEquals($a, $b); |
57
|
|
|
} else { |
58
|
3 |
|
return $a == $b; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function arrayEquals(array $a, array $b) { |
63
|
|
|
foreach($a as $aValue) { |
64
|
|
|
if(!array_reduce($b, function($carry, $bValue) use ($aValue) { |
65
|
|
|
return $carry || $this->nodeEquals($aValue, $bValue); |
66
|
|
|
}, false)) { |
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return true; |
72
|
|
|
} |
73
|
|
|
|
74
|
8 |
|
private function resourceEquals(stdClass $a, stdClass $b) { |
75
|
8 |
|
if(count(array_intersect($this->getUris($a), $this->getUris($b))) > 0) { |
76
|
5 |
|
return true; |
77
|
|
|
} |
78
|
|
|
|
79
|
4 |
|
$aArray = get_object_vars($a); |
80
|
4 |
|
$bArray = get_object_vars($b); |
81
|
|
|
|
82
|
4 |
|
if(count($aArray) !== count($bArray)) { |
83
|
1 |
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
3 |
|
foreach($aArray as $aProperty => $aValue) { |
87
|
3 |
|
if(!array_key_exists($aProperty, $bArray) || !$this->nodeEquals($aValue, $bArray[$aProperty])) { |
88
|
1 |
|
return false; |
89
|
|
|
} |
90
|
3 |
|
} |
91
|
|
|
|
92
|
2 |
|
return true; |
93
|
|
|
} |
94
|
|
|
|
95
|
8 |
|
private function getUris(stdClass $resource) { |
96
|
8 |
|
$uris = array(); |
97
|
|
|
|
98
|
8 |
|
if(property_exists($resource, '@id')) { |
99
|
5 |
|
$uris[] = $resource->{'@id'}; |
100
|
5 |
|
} |
101
|
|
|
|
102
|
8 |
|
if(property_exists($resource, 'sameAs')) { |
103
|
3 |
|
$sameAs = $resource->sameAs; |
104
|
3 |
|
if(!is_array($sameAs)) { |
105
|
1 |
|
$sameAs = array($sameAs); |
106
|
1 |
|
} |
107
|
|
|
|
108
|
3 |
|
foreach($sameAs as $uri) { |
109
|
3 |
|
if(is_string($uri)) { |
110
|
3 |
|
$uris[] = $uri; |
111
|
3 |
|
} |
112
|
3 |
|
} |
113
|
3 |
|
} |
114
|
|
|
|
115
|
8 |
|
return $uris; |
116
|
|
|
} |
117
|
|
|
|
118
|
4 |
|
private function setJsonLdDocumentLoader() { |
119
|
4 |
|
global $jsonld_default_load_document; |
120
|
|
|
|
121
|
4 |
|
$jsonld_default_load_document = '\PPP\DataModel\JsonLdResourceNode::customContextLoader'; |
122
|
4 |
|
} |
123
|
|
|
|
124
|
4 |
|
public static function customContextLoader($url) { |
125
|
4 |
|
if($url !== 'http://schema.org') { |
126
|
|
|
return jsonld_default_document_loader($url); |
127
|
|
|
} else { |
128
|
|
|
return (object) array( |
129
|
4 |
|
'contextUrl' => null, |
130
|
4 |
|
'document' => file_get_contents(__DIR__ . '/../data/schemaorg-context.jsonld'), |
131
|
|
|
'documentUrl' => 'http://schema.org' |
132
|
4 |
|
); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|