You are here

public static function EasyRdf_Literal::create in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/easyrdf/easyrdf/lib/EasyRdf/Literal.php \EasyRdf_Literal::create()

Create a new literal object

PHP values of type bool, int or float, will automatically be converted to the corresponding datatype and PHP sub-class.

If a registered datatype is given, then the registered subclass of EasyRdf_Literal will instantiated.

Note that literals are not required to have a language or datatype. Literals cannot have both a language and a datatype.

Parameters

mixed $value The value of the literal or an associative array:

string $lang The natural language of the literal or null (e.g. 'en'):

string $datatype The datatype of the literal or null (e.g. 'xsd:integer'):

Return value

object EasyRdf_Literal (or subclass of EasyRdf_Literal)

4 calls to EasyRdf_Literal::create()
EasyRdf_Graph::addLiteral in vendor/easyrdf/easyrdf/lib/EasyRdf/Graph.php
Add a literal value as a property of a resource
EasyRdf_Graph::arrayToObject in vendor/easyrdf/easyrdf/lib/EasyRdf/Graph.php
Get an EasyRdf_Resource or EasyRdf_Literal object from an associative array. @ignore
EasyRdf_Graph::checkValueParam in vendor/easyrdf/easyrdf/lib/EasyRdf/Graph.php
Check that a value parameter is valid, and convert it to an associative array if needed @ignore
EasyRdf_Sparql_Result::newTerm in vendor/easyrdf/easyrdf/lib/EasyRdf/Sparql/Result.php
Create a new EasyRdf_Resource or EasyRdf_Literal depending on the type of data passed in.

File

vendor/easyrdf/easyrdf/lib/EasyRdf/Literal.php, line 79

Class

EasyRdf_Literal
Class that represents an RDF Literal

Code

public static function create($value, $lang = null, $datatype = null) {
  if (EasyRdf_Utils::isAssociativeArray($value)) {
    if (isset($value['xml:lang'])) {
      $lang = $value['xml:lang'];
    }
    elseif (isset($value['lang'])) {
      $lang = $value['lang'];
    }
    if (isset($value['datatype'])) {
      $datatype = $value['datatype'];
    }
    $value = isset($value['value']) ? $value['value'] : null;
  }
  if (is_null($datatype) or $datatype === '') {
    if (is_null($lang) or $lang === '') {

      // Automatic datatype selection
      $datatype = self::getDatatypeForValue($value);
    }
  }
  elseif (is_object($datatype)) {
    $datatype = strval($datatype);
  }
  else {

    // Expand shortened URIs (qnames)
    $datatype = EasyRdf_Namespace::expand($datatype);
  }

  // Work out what class to use for this datatype
  if (isset(self::$datatypeMap[$datatype])) {
    $class = self::$datatypeMap[$datatype];
  }
  else {
    $class = 'EasyRdf_Literal';
  }
  return new $class($value, $lang, $datatype);
}