/** * @param string $value * @param Node|string $datatype optional * @param string $lang optional * @return Literal */ public function createLiteral($value, $datatype = null, $lang = null) { if ($value === null) { throw new \Exception('Can\'t initialize literal with null as value.'); } elseif (!is_string($value)) { throw new \Exception("The literal value has to be of type string"); } $datatypeUri = null; $world = librdf_php_get_world(); if ($datatype !== null) { if (!$datatype instanceof Node) { // Ensure valid Node creation $datatype = $this->createNamedNode($datatype); } elseif (!$datatype->isNamed()) { throw new \Exception("Argument datatype has to be a named node."); } if ($lang !== null && $datatype->getUri() !== self::$rdfLangString) { throw new \Exception('Language tagged Literals must have <' . self::$rdfLangString . '> datatype.'); } /* * Make sure that the no language is set, since redland doesn't allow a language tag to be set if a * datatype is given. */ if ($lang === null) { // TODO catch invalid URIs $datatypeUri = librdf_new_uri($world, $datatype->getUri()); } } /* * This redland method does only support either $lang or $datatypeUri or both null */ $redlandNode = librdf_new_node_from_typed_literal($world, $value, $lang, $datatypeUri); if ($redlandNode === null) { throw new \Exception('Initialization of redland node failed.'); } return new Literal($redlandNode); }
/** * Create a new Literal node. * * Both the $language and $datatype parameters are optional. * * The value of the literal node can either be a string or an XML literal * in the form of a DOMNodeList object. If using XML, a datatype of * http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral is implied, so * the datatype parameter cannot be used with XML. A literal cannot have * both a language and a datatype. * * @param mixed $value The literal value, either a string, a DOMNodeList or a librdf_node resource * @param string $datatype An optional datatype URI for the literal value * @param string $language An option language for the literal value * @return void * @throws LibRDF_Error If unabel to create a new node * @access public */ public function __construct() { $valuestr = ""; $is_xml = 0; // possible parameter lists are either LibRDF_Node $resource or // string $value, $datatype=NULL, string $language=NULL $num_args = func_num_args(); if ($num_args < 1 or $num_args > 3) { throw new LibRDF_Error("Invalid number of arguments"); } $value = func_get_arg(0); if ($num_args >= 2) { $datatype = func_get_arg(1); if ($datatype) { $datatype = new LibRDF_URI($datatype); } } else { $datatype = NULL; } if ($num_args >= 3) { $language = func_get_arg(2); } else { $language = NULL; } if ($num_args == 1 and is_resource($value)) { if (!librdf_node_is_literal($value)) { throw new LibRDF_Error("Argument 1 not a valid librdf_node " . " literal node"); } else { $this->node = $value; } } else { // value is XML, convert to a string and set the datatype if ($value instanceof DOMNodeList) { // XML values imply a datatype of // http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral, so // specifying a different datatype is an error if ($datatype !== NULL and $datatype->__toString() !== "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral") { throw new LibRDF_Error("Cannot override datatype for XML literal"); } else { $datatype = NULL; } $valuestr = ""; foreach ($value as $item) { $valuestr .= $item->ownerDocument->saveXML($item); } $is_xml = 1; } else { $valuestr = (string) $value; $is_xml = 0; } if ($datatype !== NULL) { $datatype_uri = $datatype->getURI(); } else { $datatype_uri = NULL; } if ($is_xml or $datatype === NULL and $language === NULL) { $this->node = librdf_new_node_from_literal(librdf_php_get_world(), $valuestr, $language, $is_xml); } else { $this->node = librdf_new_node_from_typed_literal(librdf_php_get_world(), $valuestr, $language, $datatype_uri); } } if (!$this->node) { throw new LibRDF_Error("Unable to create new literal node"); } }
function append_objects($s0, $p0, $lst) { if (!is_null($s0)) { $s = strlen($s0) > 1 && $s0[0] == '_' && $s0[1] == ':' ? $this->_blankNode($s0) : $this->_uriNode(absolutize($this->_base, $s0)); } if (!is_null($p0)) { $p = $this->_uriNode(absolutize($this->_base, $p0)); } $r = 0; foreach ($lst as $elt) { if (isset($elt['type']) && isset($elt['value'])) { $type = $elt['type']; $value = $elt['value']; $datatype = isset($elt['datatype']) ? $elt['datatype'] : ''; if ($type == 'literal' && $datatype) { //$json = json_encode(array($s0=>array($p0=>array(array('type'=>'literal','value'=>$value))))); $datatype_uri = librdf_new_uri($this->_world, $datatype); $o = librdf_new_node_from_typed_literal($this->_world, $value, NULL, $datatype_uri); //librdf_free_uri($dt); } elseif ($type == 'literal') { $o = $this->_literalNode($value, null); } elseif ($elt['type'] == 'uri') { $o = $this->_uriNode(absolutize($this->_base, $value)); } elseif ($elt['type'] == 'bnode') { $o = $this->_blankNode($value); } $r += librdf_model_add($this->_model, $s, $p, $o) ? 0 : 1; //$o && librdf_free_node($o); } } //$p && librdf_free_node($p); //$s && librdf_free_node($s); return $r; }