diff -r f8200c5482ec -r c731ab9b934d server/src/app/Helpers/RdfHelper.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/server/src/app/Helpers/RdfHelper.php Mon Oct 31 14:24:23 2016 +0100 @@ -0,0 +1,130 @@ +prefixes; + } + + /** + * Keep track of the prefixes used while serialising + * @ignore + */ + public function addPrefix($qname) + { + list ($prefix) = explode(':', $qname); + $this->prefixes[$prefix] = true; + return $prefix; + } + + + /** + * Given a IRI string, escape and enclose in angle brackets. + * + * @param string $resourceIri + * + * @return string + */ + public function escapeIri($resourceIri) { + $escapedIri = str_replace('>', '\\>', $resourceIri); + return "<".htmlentities($escapedIri).">"; + } + + /** + * Insprired by EasyRdf\Serialiser\Turtle::serialiseLiteral + * Given a string, enclose in quotes and escape any quotes in the string. + * Strings containing tabs, linefeeds or carriage returns will be + * enclosed in three double quotes ("""). + * + * @param string $value + * + * @return string + */ + public function serialiseLiteral($literal) + { + $value = strval($literal); + $quoted = Turtle::quotedString($value); + + if ($datatype = $literal->getDatatypeUri()) { + if ($datatype == 'http://www.w3.org/2001/XMLSchema#integer') { + return sprintf('%d', $value); + } elseif ($datatype == 'http://www.w3.org/2001/XMLSchema#decimal') { + return sprintf('%s', $value); + } elseif ($datatype == 'http://www.w3.org/2001/XMLSchema#double') { + return sprintf('%e', $value); + } elseif ($datatype == 'http://www.w3.org/2001/XMLSchema#boolean') { + return sprintf('%s', $value); + } else { + $escaped = $this->serialiseResource($datatype, false); + return sprintf('%s^^%s', $quoted, $escaped); + } + } elseif ($lang = $literal->getLang()) { + return sprintf('%s@%s',$quoted, $lang); + } else { + return "$quoted"; + } + } + + /** + * Given a an EasyRdf\Resource or URI, convert it into a string, suitable to + * be written to a Turtle document. URIs will be shortened into CURIES + * where possible. + * + * @param Resource|string $resource The resource to convert to a Turtle string + * @param boolean $createNamespace If true, a new namespace may be created + * + * @return string + */ + public function serialiseResource($resource, $createNamespace = false) + { + if (is_object($resource)) { + if ($resource->isBNode()) { + $uri = preg_replace("/^_\:/", "", $resource->getUri(), 1); + return "_:".htmlentities($uri).""; + } + + $resource = $resource->getUri(); + } + + $short = RdfNamespace::shorten($resource, $createNamespace); + + if ($short) { + $prefix = $this->addPrefix($short); + $shortVal = substr($short, strlen($prefix)+1); + $namespaceVal = RdfNamespace::namespaces()[$prefix]; + + return "$prefix:".htmlentities($shortVal).""; + } + + return $this->escapeIri($resource); + } + + + public function serialiseValue($val) { + + if($val instanceof Resource) { + return $this->serialiseResource($val, false); + } elseif($val instanceof Literal) { + return $this->serialiseLiteral($val); + } else { + throw new \InvalidArgumentException( + "serialiseObject() requires \$object to be ". + 'of type EasyRdf\Resource or EasyRdf\Literal' + ); + } + + } + +}