X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=vendor%2Feasyrdf%2Feasyrdf%2Flib%2FEasyRdf%2FParser%2FJsonLdImplementation.php;fp=vendor%2Feasyrdf%2Feasyrdf%2Flib%2FEasyRdf%2FParser%2FJsonLdImplementation.php;h=d2e22c49d05cb8369fae9b56f1fe6b56b1745b3d;hp=0000000000000000000000000000000000000000;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad diff --git a/vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/JsonLdImplementation.php b/vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/JsonLdImplementation.php new file mode 100644 index 000000000..d2e22c49d --- /dev/null +++ b/vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/JsonLdImplementation.php @@ -0,0 +1,120 @@ + + * @license http://www.opensource.org/licenses/bsd-license.php + */ +class EasyRdf_Parser_JsonLd extends EasyRdf_Parser +{ + /** + * Parse a JSON-LD document into an EasyRdf_Graph + * + * Attention: Since JSON-LD supports datasets, a document may contain + * multiple graphs and not just one. This parser returns only the + * default graph. An alternative would be to merge all graphs. + * + * @param object EasyRdf_Graph $graph the graph to load the data into + * @param string $data the RDF document data + * @param string $format the format of the input data + * @param string $baseUri the base URI of the data being parsed + * @return integer The number of triples added to the graph + */ + public function parse($graph, $data, $format, $baseUri) + { + parent::checkParseParams($graph, $data, $format, $baseUri); + + if ($format != 'jsonld') { + throw new EasyRdf_Exception( + "EasyRdf_Parser_JsonLd does not support $format" + ); + } + + try { + $quads = \ML\JsonLD\JsonLD::toRdf($data, array('base' => $baseUri)); + } catch (\ML\JsonLD\Exception\JsonLdException $e) { + throw new EasyRdf_Parser_Exception($e->getMessage()); + } + + foreach ($quads as $quad) { + // Ignore named graphs + if (null !== $quad->getGraph()) { + continue; + } + + $subject = (string) $quad->getSubject(); + if ('_:' === substr($subject, 0, 2)) { + $subject = $this->remapBnode($subject); + } + + $predicate = (string) $quad->getProperty(); + + if ($quad->getObject() instanceof \ML\IRI\IRI) { + $object = array( + 'type' => 'uri', + 'value' => (string) $quad->getObject() + ); + + if ('_:' === substr($object['value'], 0, 2)) { + $object = array( + 'type' => 'bnode', + 'value' => $this->remapBnode($object['value']) + ); + } + } else { + $object = array( + 'type' => 'literal', + 'value' => $quad->getObject()->getValue() + ); + + if ($quad->getObject() instanceof \ML\JsonLD\LanguageTaggedString) { + $object['lang'] = $quad->getObject()->getLanguage(); + } else { + $object['datatype'] = $quad->getObject()->getType(); + } + } + + $this->addTriple($subject, $predicate, $object); + } + + return $this->tripleCount; + } +}