jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/371446 )
Change subject: Add JSON-LD support.
..
Add JSON-LD support.
Bug: T44063
Change-Id: I77d6a771abfef5b00b1b151c48aa950be30896cf
---
M README.md
M composer.json
A src/JsonLdRdfWriter.php
M src/RdfWriterBase.php
M src/RdfWriterFactory.php
A tests/data/DumpHeader.jsonld
A tests/data/DumpHeader.nt
A tests/data/DumpHeader.rdf
A tests/data/DumpHeader.ttl
A tests/data/EricMiller.jsonld
A tests/data/LabeledBlankNode.jsonld
A tests/data/NumberedBlankNode.jsonld
A tests/data/Numbers.jsonld
A tests/data/Predicates.jsonld
A tests/data/Resources.jsonld
A tests/data/TextWithSpecialChars.jsonld
A tests/data/Texts.jsonld
A tests/data/Triples.jsonld
A tests/data/Values.jsonld
A tests/phpunit/JsonLdRdfWriterTest.php
M tests/phpunit/NTriplesRdfWriterTest.php
M tests/phpunit/RdfWriterFactoryTest.php
M tests/phpunit/RdfWriterTestBase.php
M tests/phpunit/TurtleRdfWriterTest.php
24 files changed, 784 insertions(+), 16 deletions(-)
Approvals:
Smalyshev: Looks good to me, approved
jenkins-bot: Verified
Thiemo Mättig (WMDE): Looks good to me, but someone else must approve
diff --git a/README.md b/README.md
index 95321b5..edd0618 100644
--- a/README.md
+++ b/README.md
@@ -1,14 +1,15 @@
# Purtle
**Purtle** is a fast, lightweight RDF generator. It provides a "fluent"
interface for
-generating RDF output in Turtle, XML/RDF or N-Triples. The fluent interface
allows the
+generating RDF output in Turtle, JSON-LD, XML/RDF or N-Triples. The fluent
interface allows the
resulting PHP code to be structured just like Turtle notation for RDF, hence
the name: "Purtle"
is a contraction of "PHP Turtle".
-The three concrete classes implementing the common `RdfWriter` interface are:
+The concrete classes implementing the common `RdfWriter` interface are:
* `TurtleRdfWriter` outputs Turtle
* `XmlRdfWriter` outputs XML/RDF
* `NTriplesRdfWriter` outputs N-Triples
+* `JsonLdRdfWriter` outputs JSON-LD
The PHP code would look something like this:
diff --git a/composer.json b/composer.json
index 5bf8b1b..d714be3 100644
--- a/composer.json
+++ b/composer.json
@@ -5,7 +5,8 @@
"keywords": [
"RDF",
"Serializer",
- "Turtle"
+ "Turtle",
+ "JSON-LD"
],
"homepage": "https://mediawiki.org/wiki/Purtle";,
"license": "GPL-2.0+",
@@ -18,6 +19,9 @@
},
{
"name": "Thiemo Mättig"
+ },
+ {
+ "name": "C. Scott Ananian"
}
],
"support": {
diff --git a/src/JsonLdRdfWriter.php b/src/JsonLdRdfWriter.php
new file mode 100644
index 000..2366a98
--- /dev/null
+++ b/src/JsonLdRdfWriter.php
@@ -0,0 +1,368 @@
+https://www.w3.org/TR/json-ld/#the-context
+* @var string[]
+*/
+ protected $context = [];
+
+ /**
+* The JSON-LD "@graph" array, which lists all the nodes
+* described by this JSON-LD object.
+* @see https://www.w3.org/TR/json-ld/#named-graphs
+* @var array[]|null
+*/
+ private $graph = [];
+
+ /**
+* A collection of predicates about a specific subject. The
+* subject is identified by the "@id" key in this array; the other
+* keys identify JSON-LD properties.
+* @see https://www.w3.org/TR/json-ld/#dfn-edge
+* @var array
+*/
+ private $predicates = [];
+
+ /**
+* A sequence of zero or more IRIs, nodes, or values, which are the
+* destination targets of the current predicates.
+* @see https://www.w3.org/TR/json-ld/#dfn-list
+* @var array
+*/
+ private $values = [];
+
+ /**
+* True iff we have written the opening of the "@graph" field.
+* @var bool
+*/
+ private $wroteGraph = false;
+
+ /**
+* JSON-LD objects describing a single node can omit the "@graph" field;
+* this variable remains false only so long as we can guarantee that
+* only a single node has been described.
+* @var bool
+*/
+ private $disableGraphOpt = false;
+
+ /**
+* The IRI for the RDF `type` property.
+*/
+ const RDF_TYPE_IRI = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type';
+
+ /**
+* @param string $role
+* @param BNodeLabeler|null $labeler
+*/
+ public function __construct( $role = parent::DOCUMENT_ROLE,
BNodeLabeler $labeler = null ) {
+ parent::__construct( $role, $labeler );
+
+ // The following named methods are protected, not private, so we
+ // can invoke them directly w/o function wrappers.
+ $this->transitionTable[self::STATE_START][self::STATE_DOCUMENT]
=
+