value = $value; $this->lang = $lang ? $lang : null; $this->datatype = $datatype ? $datatype : null; if ($this->datatype) { if (is_object($this->datatype)) { // Convert objects to strings $this->datatype = strval($this->datatype); } else { // Expand shortened URIs (CURIEs) $this->datatype = EasyRdf_Namespace::expand($this->datatype); } // Literals can not have both a language and a datatype $this->lang = null; } else { // Set the datatype based on the subclass $class = get_class($this); if (isset(self::$classMap[$class])) { $this->datatype = self::$classMap[$class]; $this->lang = null; } } // Cast value to string settype($this->value, 'string'); } /** Returns the value of the literal. * * @return string Value of this literal. */ public function getValue() { return $this->value; } /** Returns the full datatype URI of the literal. * * @return string Datatype URI of this literal. */ public function getDatatypeUri() { return $this->datatype; } /** Returns the shortened datatype URI of the literal. * * @return string Datatype of this literal (e.g. xsd:integer). */ public function getDatatype() { if ($this->datatype) { return EasyRdf_Namespace::shorten($this->datatype); } else { return null; } } /** Returns the language of the literal. * * @return string Language of this literal. */ public function getLang() { return $this->lang; } /** Returns the properties of the literal as an associative array * * For example: * array('type' => 'literal', 'value' => 'string value') * * @return array The properties of the literal */ public function toRdfPhp() { $array = array( 'type' => 'literal', 'value' => $this->value ); if ($this->datatype) { $array['datatype'] = $this->datatype; } if ($this->lang) { $array['lang'] = $this->lang; } return $array; } /** Magic method to return the value of a literal as a string * * @return string The value of the literal */ public function __toString() { return isset($this->value) ? $this->value : ''; } /** Return pretty-print view of the literal * * @param string $format Either 'html' or 'text' * @param string $color The colour of the text * @return string */ public function dumpValue($format = 'html', $color = 'black') { return EasyRdf_Utils::dumpLiteralValue($this, $format, $color); } } /* Register default set of datatype classes */ EasyRdf_Literal::setDatatypeMapping('xsd:boolean', 'EasyRdf_Literal_Boolean'); EasyRdf_Literal::setDatatypeMapping('xsd:date', 'EasyRdf_Literal_Date'); EasyRdf_Literal::setDatatypeMapping('xsd:dateTime', 'EasyRdf_Literal_DateTime'); EasyRdf_Literal::setDatatypeMapping('xsd:decimal', 'EasyRdf_Literal_Decimal'); EasyRdf_Literal::setDatatypeMapping('xsd:hexBinary', 'EasyRdf_Literal_HexBinary'); EasyRdf_Literal::setDatatypeMapping('rdf:HTML', 'EasyRdf_Literal_HTML'); EasyRdf_Literal::setDatatypeMapping('xsd:integer', 'EasyRdf_Literal_Integer'); EasyRdf_Literal::setDatatypeMapping('rdf:XMLLiteral', 'EasyRdf_Literal_XML');