Skip to content

Commit 5236f23

Browse files
Lukas RosenstockLukas Rosenstock
authored andcommitted
Arc2JsonLdConverter
1 parent 90c5bd6 commit 5236f23

File tree

4 files changed

+274
-0
lines changed

4 files changed

+274
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor

Arc2JsonLdConverter.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace CloudObjects\Utilities\RDF;
4+
5+
use ML\IRI\IRI;
6+
use ML\JsonLD\JsonLD, ML\JsonLD\Quad, ML\JsonLD\TypedValue, ML\JsonLD\RdfConstants;
7+
8+
class Arc2JsonLdConverter {
9+
10+
/**
11+
* Converts an array of ARC2 triples into an array of RDF quads
12+
* in JsonLD library format.
13+
* @param array $triples ARC2 triples
14+
*/
15+
public static function triplesToQuads(array $triples) {
16+
$quads = array();
17+
foreach ($triples as $t) {
18+
$quads[] = new Quad(new IRI($t['s']), new IRI($t['p']),
19+
($t['o_type'] == 'uri') ? new IRI($t['o']) : new TypedValue($t['o'],
20+
(isset($t['o_datatype']) && $t['o_datatype']!='') ? $t['o_datatype'] : RdfConstants::XSD_STRING));
21+
}
22+
23+
return $quads;
24+
}
25+
26+
/**
27+
* Converts an ARC2 index into an array of RDF quads in JsonLD
28+
* library format.
29+
* @param array $index ARC2 index
30+
*/
31+
public static function indexToQuads(array $index) {
32+
$quads = array();
33+
foreach ($index as $subject => $predicates) {
34+
foreach ($predicates as $predicate => $objects) {
35+
foreach ($objects as $object) {
36+
$quads[] = new Quad(new IRI($subject),
37+
new IRI($predicate),
38+
($object['type']!='literal')
39+
? new IRI($object['value']) : new TypedValue($object['value'],
40+
(isset($object['datatype']) && $object['datatype']!='')
41+
? $object['datatype'] : RdfConstants::XSD_STRING));
42+
}
43+
}
44+
}
45+
return $quads;
46+
}
47+
48+
/**
49+
* Converts an array of RDF quads in JsonLD library format into
50+
* an array of ARC2 triples.
51+
* @param array $quads JsonLD quads
52+
*/
53+
public static function quadsToTriples(array $quads) {
54+
$arcTriples = array();
55+
foreach ($quads as $q) {
56+
$arcTriples[] = array(
57+
's' => (string)$q->getSubject(),
58+
'p' => (string)$q->getProperty(),
59+
'o' => (is_a($q->getObject(), 'ML\JsonLD\TypedValue')) ? $q->getObject()->getValue() : (string)$q->getObject(),
60+
'o_type' => (is_a($q->getObject(), 'ML\JsonLD\TypedValue')) ? 'literal' : 'uri'
61+
);
62+
}
63+
return $arcTriples;
64+
}
65+
66+
/**
67+
* Converts an array of RDF quads in JsonLD library format into
68+
* an ARC2 index.
69+
* @param array $quads JsonLD quads
70+
*/
71+
public static function quadsToIndex(array $quads) {
72+
return \ARC2::getSimpleIndex(self::quadsToTriples($quads), 0);
73+
}
74+
75+
/**
76+
* Converts any JsonLD into an array of ARC2 triples.
77+
* @param string|array|object $jsonLd JsonLD content
78+
*/
79+
public static function jsonLdToTriples($jsonLd) {
80+
if (is_array($jsonLd)) $jsonLd = (object)$jsonLd;
81+
return self::quadsToTriples(JsonLD::toRdf($jsonLd));
82+
}
83+
84+
/**
85+
* Converts any JsonLD into an ARC2 index.
86+
* @param string|array|object $jsonLd JsonLD content
87+
*/
88+
public static function jsonLdToIndex($jsonLd) {
89+
if (is_array($jsonLd)) $jsonLd = (object)$jsonLd;
90+
return self::quadsToIndex(JsonLD::toRdf($jsonLd));
91+
}
92+
93+
}

composer.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "cloudobjects/rdfutilities",
3+
"description": "ARC2JsonLD Helper Class",
4+
"keywords": ["cloudobjects", "rdf", "json-ld", "arc2"],
5+
"homepage": "https://github.com/CloudObjects/RDFUtilities",
6+
"license": "MPL",
7+
"authors": [
8+
{
9+
"name": "Lukas Rosenstock"
10+
}
11+
],
12+
"require": {
13+
"semsol/arc2" : "v2.1.0",
14+
"ml/json-ld" : ">=1.0.3"
15+
},
16+
"autoload": {
17+
"psr-4": {
18+
"CloudObjects\\Utilities\\RDF\\" : ""
19+
}
20+
}
21+
}

composer.lock

Lines changed: 159 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)