-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce JsonLdSerializable interface and make everything serializable
This addresses #15.
- Loading branch information
Showing
6 changed files
with
232 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
|
||
namespace ML\JsonLD; | ||
|
||
use stdClass as Object; | ||
use ML\IRI\IRI; | ||
|
||
/** | ||
|
@@ -18,7 +19,7 @@ | |
* | ||
* @author Markus Lanthaler <[email protected]> | ||
*/ | ||
class Document implements DocumentInterface | ||
class Document implements DocumentInterface, JsonLdSerializable | ||
{ | ||
/** | ||
* @var IRI The document's IRI | ||
|
@@ -171,4 +172,29 @@ public function removeGraph($graph = null) | |
unset($this->namedGraphs[$name]); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function toJsonLd($useNativeTypes = true) | ||
{ | ||
$defGraph = $this->defaultGraph->toJsonLd($useNativeTypes); | ||
|
||
if (0 === count($this->namedGraphs)) { | ||
return $defGraph; | ||
} | ||
|
||
foreach ($this->namedGraphs as $graphName => $graph) { | ||
$namedGraph = new Object(); | ||
$namedGraph->{'@id'} = $graphName; | ||
$namedGraph->{'@graph'} = $graph->toJsonLd($useNativeTypes); | ||
|
||
$defGraph[] = $namedGraph; | ||
} | ||
|
||
$document = new Object(); | ||
$document->{'@graph'} = $defGraph; | ||
|
||
return array($document); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
* | ||
* @author Markus Lanthaler <[email protected]> | ||
*/ | ||
class Graph implements GraphInterface | ||
class Graph implements GraphInterface, JsonLdSerializable | ||
{ | ||
/** | ||
* @var DocumentInterface The document this graph belongs to. | ||
|
@@ -219,6 +219,23 @@ public function merge(GraphInterface $graph) | |
return $this; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function toJsonLd($useNativeTypes = true) | ||
{ | ||
// Bring nodes into a deterministic order | ||
$nodes = $this->nodes; | ||
ksort($nodes); | ||
$nodes = array_values($nodes); | ||
|
||
$serializeNode = function ($node) use ($useNativeTypes) { | ||
return $node->toJsonLd($useNativeTypes); | ||
}; | ||
|
||
return array_map($serializeNode, $nodes); | ||
} | ||
|
||
/** | ||
* Create a new blank node identifier unique to the document. | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,12 +9,14 @@ | |
|
||
namespace ML\JsonLD; | ||
|
||
use stdClass as Object; | ||
|
||
/** | ||
* A Node represents a node in a JSON-LD graph. | ||
* | ||
* @author Markus Lanthaler <[email protected]> | ||
*/ | ||
class Node implements NodeInterface | ||
class Node implements NodeInterface, JsonLdSerializable | ||
{ | ||
/** The @type constant. */ | ||
const TYPE = '@type'; | ||
|
@@ -356,6 +358,55 @@ public function equals(NodeInterface $other) | |
return $this === $other; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function toJsonLd($useNativeTypes = true) | ||
{ | ||
$node = new \stdClass(); | ||
|
||
// Only label blank nodes if other nodes point to it | ||
if ((false === $this->isBlankNode()) || (count($this->getReverseProperties()) > 0)) { | ||
$node->{'@id'} = $this->getId(); | ||
} | ||
|
||
$properties = $this->getProperties(); | ||
|
||
foreach ($properties as $prop => $values) { | ||
if (false === is_array($values)) { | ||
$values = array($values); | ||
} | ||
|
||
if (self::TYPE === $prop) { | ||
$node->{'@type'} = array(); | ||
foreach ($values as $val) { | ||
$node->{'@type'}[] = $val->getId(); | ||
} | ||
|
||
continue; | ||
} | ||
|
||
$node->{$prop} = array(); | ||
|
||
foreach ($values as $value) { | ||
if ($value instanceof NodeInterface) { | ||
$ref = new \stdClass(); | ||
$ref->{'@id'} = $value->getId(); | ||
$node->{$prop}[] = $ref; | ||
} elseif (is_object($value)) { // language-tagged string or typed value | ||
$node->{$prop}[] = $value->toJsonLd($useNativeTypes); | ||
} else { | ||
$val = new Object(); | ||
$val->{'@value'} = $value; | ||
$node->{$prop}[] = $val; | ||
} | ||
} | ||
|
||
} | ||
|
||
return $node; | ||
} | ||
|
||
/** | ||
* Add a reverse property. | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ | |
* | ||
* @author Markus Lanthaler <[email protected]> | ||
*/ | ||
abstract class Value | ||
abstract class Value implements JsonLdSerializable | ||
{ | ||
/** | ||
* The value in the form of a string | ||
|
@@ -99,18 +99,6 @@ public static function fromJsonLd(Object $element) | |
return new TypedValue($value, (null === $type) ? RdfConstants::XSD_STRING : $type); | ||
} | ||
|
||
/** | ||
* Convert this instance to a JSON-LD element in expanded form | ||
* | ||
* @param boolean $useNativeTypes If set to true, native types are used | ||
* for xsd:integer, xsd:double, and | ||
* xsd:boolean, otherwise typed strings | ||
* will be used instead. | ||
* | ||
* @return string|integer|float|boolean|Object The JSON-LD element. | ||
*/ | ||
abstract public function toJsonLd($useNativeTypes = true); | ||
|
||
/** | ||
* Compares this instance to the specified value. | ||
* | ||
|