/** * 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 _literalNode($value, $type = null) { if (!isset($this->_literalNodes[$type])) { $this->_literalNodes[$type] = array(); } if (!isset($this->_literalNodes[$type][$value])) { $r = $this->_literalNodes[$type][$value] = librdf_new_node_from_literal($this->_world, $value, NULL, 0); } else { $r = $this->_literalNodes[$type][$value]; } return $r; }