Skip to content

Commit

Permalink
NtriplesUtil::serializeLiteral() fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
zozlak committed Mar 29, 2021
1 parent 065a8c5 commit 45194ad
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions src/rdfHelpers/NtriplesUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@
*
* @author zozlak
*/
class NtriplesUtil
{
class NtriplesUtil {

/**
* Characters forbidden in n-triples literals according to
Expand Down Expand Up @@ -103,18 +102,15 @@ class NtriplesUtil
'\\' => '\\\\'
);

public static function escapeLiteral(string $str): string
{
public static function escapeLiteral(string $str): string {
return strtr($str, self::$literalEscapeMap);
}

public static function escapeIri(string $str): string
{
public static function escapeIri(string $str): string {
return strtr($str, self::$iriEscapeMap);
}

public static function serializeIri(NamedNode | BlankNode $res): string
{
public static function serializeIri(NamedNode | BlankNode $res): string {
if ($res instanceof DefaultGraph) {
return '';
}
Expand All @@ -126,18 +122,16 @@ public static function serializeIri(NamedNode | BlankNode $res): string
}
}

public static function serializeLiteral(Literal $literal): string
{
public static function serializeLiteral(Literal $literal): string {
$langtype = '@' . $literal->getLang();
if ($langtype === '@') {
$langtype = $literal->getDatatype();
$langtype = $langtype == RDF::XSD_STRING ?: '^^<' . self::escapeIri($literal->getDatatype()) . '>';
$langtype = $langtype == RDF::XSD_STRING ? '' : '^^<' . self::escapeIri($literal->getDatatype()) . '>';
}
return self::escapeLiteral((string) $literal->getValue()) . $langtype;
return '"' . self::escapeLiteral((string) $literal->getValue()) . '"' . $langtype;
}

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

0 comments on commit 45194ad

Please sign in to comment.