|
| 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 | +} |
0 commit comments