Skip to content

Commit

Permalink
NtriplesUtil::serialize() deals also with quads
Browse files Browse the repository at this point in the history
  • Loading branch information
zozlak committed May 7, 2021
1 parent aa7073b commit fa43c23
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/rdfHelpers/NtriplesUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use rdfInterface\NamedNode;
use rdfInterface\BlankNode;
use rdfInterface\Literal;
use rdfInterface\Quad;
use rdfInterface\DefaultGraph;

/**
Expand Down Expand Up @@ -131,9 +132,24 @@ public static function serializeLiteral(Literal $literal): string {
return '"' . self::escapeLiteral((string) $literal->getValue()) . '"' . $langtype;
}

public static function serialize(NamedNode | BlankNode | Literal $term): string {
public static function serializeQuad(Quad $quad): string {
$sbj = self::serialize($quad->getSubject());
$pred = self::serializeIri($quad->getPredicate());
$obj = self::serialize($quad->getObject());
$graph = $quad->getGraph();
if ($graph !== null && !($graph instanceof DefaultGraph)) {
$graph = NtriplesUtil::serializeIri($graph);
} else {
$graph = '';
}
return "<< $sbj $pred $obj $graph >>";
}

public static function serialize(NamedNode | BlankNode | Literal | Quad $term): string {
if ($term instanceof Literal) {
return self::serializeLiteral($term);
} elseif ($term instanceof Quad) {
return self::serializeQuad($term);
} else {
return self::serializeIri($term);
}
Expand Down

0 comments on commit fa43c23

Please sign in to comment.