From 720c629894eaa351ac52a3aa22af3cf7ef7527f2 Mon Sep 17 00:00:00 2001 From: Markus Lanthaler Date: Tue, 2 Oct 2012 18:24:56 +0200 Subject: [PATCH] Add Quad, the QuadSerializer interface and the NQuads serializer This allows it to easily serialize data from a JSON-LD document to other formats. This addresses #11. --- NQuads.php | 68 ++++++++++++++++ Processor.php | 14 ++-- Quad.php | 154 +++++++++++++++++++++++++++++++++++ QuadSerializer.php | 29 +++++++ Test/JsonLDTestSuiteTest.php | 31 +------ 5 files changed, 261 insertions(+), 35 deletions(-) create mode 100644 NQuads.php create mode 100644 Quad.php create mode 100644 QuadSerializer.php diff --git a/NQuads.php b/NQuads.php new file mode 100644 index 0000000..8f2d6da --- /dev/null +++ b/NQuads.php @@ -0,0 +1,68 @@ + + * + * 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 + */ +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; + } +} diff --git a/Processor.php b/Processor.php index e63da63..2dd8308 100644 --- a/Processor.php +++ b/Processor.php @@ -1752,7 +1752,7 @@ 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; } @@ -1760,13 +1760,13 @@ public function toQuads(&$element, &$result, $activesubj = null, $activeprty = n { 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) @@ -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; } @@ -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); @@ -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; } diff --git a/Quad.php b/Quad.php new file mode 100644 index 0000000..5b70f66 --- /dev/null +++ b/Quad.php @@ -0,0 +1,154 @@ + + * + * 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 + */ +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; + } +} diff --git a/QuadSerializer.php b/QuadSerializer.php new file mode 100644 index 0000000..c08c1f2 --- /dev/null +++ b/QuadSerializer.php @@ -0,0 +1,29 @@ + + * + * 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 + */ +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); +} diff --git a/Test/JsonLDTestSuiteTest.php b/Test/JsonLDTestSuiteTest.php index 2b9519a..ac820e8 100644 --- a/Test/JsonLDTestSuiteTest.php +++ b/Test/JsonLDTestSuiteTest.php @@ -10,6 +10,7 @@ namespace ML\JsonLD\Test; use ML\JsonLD\JsonLD; +use ML\JsonLD\NQuads; use ML\JsonLD\Test\TestManifestIterator; @@ -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'}, @@ -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); }