Documentation

Literal
in package
implements Literal, SingletonTerm

Description of Literal

Tags
author

zozlak

Interfaces, Classes, Traits and Enums

Literal
Description of RdfTerm
SingletonTerm
Interface used to mark DataFactory-hashable terms

Table of Contents

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

Properties

$value

private int $value

| float | string | bool | Stringable

Methods

__construct()

Creates a new literal.

public __construct(int|float|string|bool|Stringable $value[, string|null $lang = null ][, string|null $datatype = null ]) : mixed

If the $datatype is null or empty, it must be set according to the $value and $lang parameter values:

  • if $lang is not empty, the $datatype must be set to http://www.w3.org/1999/02/22-rdf-syntax-ns#langString
  • if $lang is empty or null, the $datatype depends on the PHP type of the $value parameter:
    • bool - http://www.w3.org/2001/XMLSchema#boolean
    • int - http://www.w3.org/2001/XMLSchema#integer
    • float - http://www.w3.org/2001/XMLSchema#decimal
    • string - http://www.w3.org/2001/XMLSchema#string
    • Stringable object - http://www.w3.org/2001/XMLSchema#string or any other more precise XSD datatype if the implementation is able to derive it from the object class

The created literal must have valid combination of datatype and lang tag (meaning the datatype is rdf:langString if and only if the literal has a lang tag). It's up to the implementation how to assure it. Both throwing an exception and one of $lang/$datatype taking precedense over the other are considered valid solutions. Of course the implementation behavior should be documented.

See https://www.w3.org/TR/rdf11-concepts/#section-Graph-Literal for a reference.

Parameters
$value : int|float|string|bool|Stringable

Literal's value. Only values which can be casted to a string are allowed so it's clear how to obtain literal's value lexical form (see the RDF specification mentioned above). The lexical forms of scalar values must be created (returned by the getValue() method) in a way that back-casting them to the scalar type gives an equal value (probably the only tricky case here is boolean false).

$lang : string|null = null

Literal's lang tag. If null or empty string, the literal is assumed not to have a lang tag (as an empty lang tag is not allowed in RDF).

$datatype : string|null = null

Literal's datatype. If it's null, the datatype must be assigned according to the $lang and $value parameter values. The detailed procedure is described above.

Return values
mixed

__toString()

public __toString() : string
Return values
string

equals()

public equals(Term $term) : bool
Parameters
$term : Term
Return values
bool

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.
Return values
mixed

withDatatype()

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

public withDatatype(string $datatype) : Literal

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
Return values
Literal

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) : Literal

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
Literal

withValue()

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

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

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
Literal

Search results