Skip to content

Commit

Permalink
Add Quad, the QuadSerializer interface and the NQuads serializer
Browse files Browse the repository at this point in the history
This allows it to easily serialize data from a JSON-LD document to other formats.

This addresses #11.
  • Loading branch information
lanthaler committed Oct 2, 2012
1 parent 33639e7 commit 720c629
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 35 deletions.
68 changes: 68 additions & 0 deletions NQuads.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
* (c) Markus Lanthaler <mail@markus-lanthaler.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ML\JsonLD;

use ML\JsonLD\Exception\ParseException;
use ML\IRI\IRI;


/**
* NQuads serializes quads to the NQuads format
*
* @author Markus Lanthaler <mail@markus-lanthaler.com>
*/
class NQuads implements QuadSerializer
{
/**
* {@inheritDoc}
*/
public function serialize(array $quads)
{
$result = '';
foreach ($quads as $quad)
{
$result .= ('_' === $quad->getSubject()->getScheme())
? $quad->getSubject()
: '<' . $quad->getSubject() . '>';
$result .= ' ';

$result .= ('_' === $quad->getProperty()->getScheme())
? $quad->getProperty()
: '<' . $quad->getProperty() . '>';
$result .= ' ';

if ($quad->getObject() instanceof IRI)
{
$result .= ('_' === $quad->getObject()->getScheme())
? $quad->getObject()
: '<' . $quad->getObject() . '>';
}
else
{
$result .= '"' . $quad->getObject()->getValue() . '"';
$result .= ($quad->getObject() instanceof TypedValue)
? '^^<' . $quad->getObject()->getType() . '>'
: '@' . $quad->getObject()->getLanguage();
}
$result .= ' ';

if ($quad->getGraph())
{
$result .= ('_' === $quad->getGraph()->getScheme())
? $quad->getGraph() :
'<' . $quad->getGraph() . '>';
$result .= ' ';
}
$result .= ".\n";
}

return $result;
}
}
14 changes: 7 additions & 7 deletions Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1752,21 +1752,21 @@ public function toQuads(&$element, &$result, $activesubj = null, $activeprty = n
if (property_exists($element, '@value'))
{
$value = Value::fromJsonLd($element);
$result[] = array($activesubj, $activeprty, $value, $graph);
$result[] = new Quad($activesubj, $activeprty, $value, $graph);

return;
}
elseif (property_exists($element, '@list'))
{
if (0 === ($len = count($element->{'@list'})))
{
$result[] = array($activesubj, $activeprty, new IRI(RdfConstants::RDF_NIL), $graph);
$result[] = new Quad($activesubj, $activeprty, new IRI(RdfConstants::RDF_NIL), $graph);

return;
}

$first_bn = new IRI($this->getBlankNodeId());
$result[] = array($activesubj, $activeprty, $first_bn, $graph);
$result[] = new Quad($activesubj, $activeprty, $first_bn, $graph);

$i = 0;
while ($i < $len)
Expand All @@ -1778,10 +1778,10 @@ public function toQuads(&$element, &$result, $activesubj = null, $activeprty = n
? new IRI($this->getBlankNodeId())
: new IRI(RdfConstants::RDF_NIL);

$result[] = array($first_bn, new IRI(RdfConstants::RDF_REST), $rest_bn, $graph);
$result[] = new Quad($first_bn, new IRI(RdfConstants::RDF_REST), $rest_bn, $graph);
$first_bn = $rest_bn;
}

return;
}

Expand All @@ -1806,7 +1806,7 @@ public function toQuads(&$element, &$result, $activesubj = null, $activeprty = n

if ($prevsubj)
{
$result[] = array($prevsubj, $activeprty, $activesubj, $graph);;
$result[] = new Quad($prevsubj, $activeprty, $activesubj, $graph);;
}

$properties = get_object_vars($element);
Expand All @@ -1818,7 +1818,7 @@ public function toQuads(&$element, &$result, $activesubj = null, $activeprty = n
{
foreach ($value as $val)
{
$result[] = array($activesubj, new IRI(RdfConstants::RDF_TYPE), new IRI($val), $graph);
$result[] = new Quad($activesubj, new IRI(RdfConstants::RDF_TYPE), new IRI($val), $graph);
}
continue;
}
Expand Down
154 changes: 154 additions & 0 deletions Quad.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php

/*
* (c) Markus Lanthaler <mail@markus-lanthaler.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ML\JsonLD;

use ML\IRI\IRI;

/**
* A quad
*
* @author Markus Lanthaler <mail@markus-lanthaler.com>
*/
class Quad
{
/**
* The subject
*
* @var IRI
*/
private $subject;

/**
* The property or predicate
*
* @var IRI
*/
private $property;

/**
* The object
*
* @var Value|IRI
*/
private $object;

/**
* The graph
*
* @var IRI
*/
private $graph;


/**
* Constructor
*
* @param IRI $subject The subject.
* @param IRI $property The property.
* @param Value|IRI $object The object.
* @param IRI $graph The graph.
*
* @throws InvalidArgumentException If the object parameter has a wrong type
*/
public function __construct(IRI $subject, IRI $property, $object, IRI $graph = null)
{
$this->subject = $subject;
$this->property = $property;
$this->setObject($object); // use setter which checks the type
$this->graph = $graph;
}

/**
* Set the subject
*
* @param IRI $subject The subject
*/
public function setSubject(IRI $subject)
{
$this->subject = $subject;
}

/**
* Get the subject
*
* @return IRI The subject
*/
public function getSubject()
{
return $this->subject;
}

/**
* Set the property
*
* @param IRI $property The property
*/
public function setProperty(IRI $property)
{
$this->property = $property;
}

/**
* Get the property
*
* @return IRI The property
*/
public function getProperty()
{
return $this->property;
}

/**
* Set the object
*
* @param IRI|Value $object The object
*
* @throws InvalidArgumentException If object is of wrong type.
*/
public function setObject($object)
{
if (!($object instanceof IRI) && !($object instanceof Value))
{
throw new \InvalidArgumentException('Object must be an IRI or Value object');
}

$this->object = $object;
}

/**
* Get the object
*
* @return IRI|Value The object
*/
public function getObject()
{
return $this->object;
}

/**
* Set the graph
*
* @param IRI $graph The graph
*/
public function setGraph(IRI $graph = null)
{
$this->graph = $graph;
}

/**
* Get the graph
*
* @return IRI The graph
*/
public function getGraph()
{
return $this->graph;
}
}
29 changes: 29 additions & 0 deletions QuadSerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* (c) Markus Lanthaler <mail@markus-lanthaler.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ML\JsonLD;

use ML\JsonLD\Exception\ParseException;

/**
* Quad serializer interface
*
* @author Markus Lanthaler <mail@markus-lanthaler.com>
*/
interface QuadSerializer
{
/**
* Serializes quads to a string.
*
* @param array $quads Array of {@link Quad Quads} to be serialized
*
* @return string The serialized quads.
*/
public function serialize(array $quads);
}
31 changes: 3 additions & 28 deletions Test/JsonLDTestSuiteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace ML\JsonLD\Test;

use ML\JsonLD\JsonLD;
use ML\JsonLD\NQuads;
use ML\JsonLD\Test\TestManifestIterator;


Expand Down Expand Up @@ -105,7 +106,6 @@ public function testFraming($name, $test, $options)
$this->markTestSkipped('This implementation uses deep value matching and aggressive "re-embedding". See ISSUE-110 and ISSUE-119');
}


$expected = json_decode(file_get_contents($this->basedir . $test->{'expect'}));
$result = JsonLD::frame($this->basedir . $test->{'input'},
$this->basedir . $test->{'frame'},
Expand Down Expand Up @@ -140,33 +140,8 @@ public function testToQuads($name, $test, $options)
null,
$options);

// TODO Extract this NQuads serializer to a separate class
$result = '';
foreach ($quads as $quad)
{
$result .= ('_' === $quad[0]->getScheme()) ? $quad[0] : '<' . $quad[0] . '>';
$result .= ' ';
$result .= ('_' === $quad[1]->getScheme()) ? $quad[1] : '<' . $quad[1] . '>';
$result .= ' ';
if ($quad[2] instanceof \ML\IRI\IRI)
{
$result .= ('_' === $quad[2]->getScheme()) ? $quad[2] : '<' . $quad[2] . '>';
}
else
{
$result .= '"' . $quad[2]->getValue() . '"';
$result .= ($quad[2] instanceof \ML\JsonLD\TypedValue)
? '^^<' . $quad[2]->getType() . '>'
: '@' . $quad[2]->getLanguage();
}
$result .= ' ';
if ($quad[3])
{
$result .= ('_' === $quad[3]->getScheme()) ? $quad[3] : '<' . $quad[3] . '>';
$result .= ' ';
}
$result .= ".\n";
}
$serializer = new NQuads();
$result = $serializer->serialize($quads);

$this->assertEquals($expected, $result);
}
Expand Down

0 comments on commit 720c629

Please sign in to comment.