Documentation

LiteralInterface extends TermInterface, TermCompareInterface

Description of RdfTerm

Tags
author

zozlak

Table of Contents

Constants

CAST_DATATYPE  = 2
CAST_LEXICAL_FORM  = 1

Methods

__toString()  : string
Should return:
equals()  : bool
getDatatype()  : string
Returns literal's datatype.
getLang()  : string|null
Returns literal's language tag.
getValue()  : mixed
Returns literal's value.
withDatatype()  : LiteralInterface
Returns a new literal being a copy with this one with datatype set to a given value.
withLang()  : LiteralInterface
Returns a new literal being a copy of this one with a lang tag set to a given value.
withValue()  : LiteralInterface
Returns a new literal being a copy of this one with a given value.

Constants

Methods

__toString()

Should return:

public __toString() : string
  • an URI for named nodes and blank nodes
  • a value in lexical form for literals

For all other kind of terms the return value depends on the implementation

Return values
string

getDatatype()

Returns literal's datatype.

public getDatatype() : string

The method must return the actual datatype even if it's implicit. It means http://www.w3.org/1999/02/22-rdf-syntax-ns#langString must be returned for literals with a lang tag and http://www.w3.org/2001/XMLSchema#string must be returned for literals without lang tag and without datatype specified explicitely.

Return values
string

getLang()

Returns literal's language tag.

public getLang() : string|null

If a literal lacks a language tag, null should be returned. It means this method can't return an empty string.

Return values
string|null

getValue()

Returns literal's value.

public getValue([int $cast = self::CAST_LEXICAL_FORM ]) : mixed

Separate cast options are needed as the RDF specification defines a few kinds of literal values. See https://www.w3.org/TR/rdf11-concepts/#section-Graph-Literal for details.

Parameters
$cast : int = self::CAST_LEXICAL_FORM

Determines the kind of value being returned:

  • \rdfInterface\CAST_LEXICAL_FORM - a string with literal's lexical form. All implementations must handle this kind of cast.
  • \rdfInterface\CAST_DATATYPE - value mapped to the datatype's domain. Implementations may handle this kind of cast. It's up to the implementation which datatypes are supported and how the mapping is being done. Implementations may provide additional types of cast.

withDatatype()

Returns a new literal being a copy with this one with datatype set to a given value.

public withDatatype(string $datatype) : LiteralInterface

Be aware it's impossibe to set the datatype to rdf::langString that way as it would require setting a non-empty lang tag. The withLang() method should be used in such a case as it changes the datatype implicitly.

As this method by definition doesn't allow to set a datatype allowing a literal to have a lang tag, it must set returned literal's lang tag to null.

This method must throw a \BadMethodCallException when called with a wrong datatype (http://www.w3.org/1999/02/22-rdf-syntax-ns#langString or an empty one).

Parameters
$datatype : string
Tags
see
withLang()
throws
BadMethodCallException
Return values
LiteralInterface

withLang()

Returns a new literal being a copy of this one with a lang tag set to a given value.

public withLang(string|null $lang) : LiteralInterface

Be aware setting a lang tag on a literal without one as well as dropping a lang tag from a literal having it implicitly changes literal's datatype. Setting a lang on a literal without one enforces setting its datatype to rdf:langString while dropping a lang from a literal having it changes literal's datatype to xsd:string (see https://www.w3.org/TR/rdf11-concepts/#section-Graph-Literal for details).

Parameters
$lang : string|null
Return values
LiteralInterface

withValue()

Returns a new literal being a copy of this one with a given value.

public withValue(int|float|string|bool|Stringable $value) : LiteralInterface

If the $value type is int/float/bool, the lang tag of the returned literal is set to null and the datatype is set to a corresponding XSD type (see the constructor documentation). If the $value is string/Stringable, the lang tag and datatype are preserved.

Parameters
$value : int|float|string|bool|Stringable
Return values
LiteralInterface

        
On this page

Search results