-
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.
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.
- Loading branch information
Showing
5 changed files
with
261 additions
and
35 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 |
---|---|---|
@@ -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; | ||
} | ||
} |
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 |
---|---|---|
@@ -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; | ||
} | ||
} |
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 |
---|---|---|
@@ -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); | ||
} |
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