You are here

class EasyRdf_Literal_Decimal in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/easyrdf/easyrdf/lib/EasyRdf/Literal/Decimal.php \EasyRdf_Literal_Decimal

Class that represents an RDF Literal of datatype xsd:decimal

@package EasyRdf @link http://www.w3.org/TR/xmlschema-2/#decimal @copyright Copyright (c) 2009-2013 Nicholas J Humfrey @license http://www.opensource.org/licenses/bsd-license.php

Hierarchy

Expanded class hierarchy of EasyRdf_Literal_Decimal

1 string reference to 'EasyRdf_Literal_Decimal'
Literal.php in vendor/easyrdf/easyrdf/lib/EasyRdf/Literal.php

File

vendor/easyrdf/easyrdf/lib/EasyRdf/Literal/Decimal.php, line 46

View source
class EasyRdf_Literal_Decimal extends EasyRdf_Literal {

  /**
   * written according to http://www.w3.org/TR/xmlschema-2/#decimal
   */
  const DECIMAL_REGEX = '^([+\\-]?)(((\\d+)?\\.(\\d+))|((\\d+)\\.?))$';

  /** Constructor for creating a new decimal literal
   *
   * @param  double|int|string $value    The value of the literal
   * @param  string            $lang     Should be null (literals with a datatype can't have a language)
   * @param  string            $datatype Optional datatype (default 'xsd:decimal')
   *
   * @throws UnexpectedValueException
   * @return EasyRdf_Literal_Decimal
   */
  public function __construct($value, $lang = null, $datatype = null) {
    if (is_string($value)) {
      self::validate($value);
    }
    elseif (is_double($value) or is_int($value)) {
      $locale_data = localeconv();
      $value = str_replace($locale_data['decimal_point'], '.', strval($value));
    }
    else {
      throw new UnexpectedValueException('EasyRdf_Literal_Decimal expects int/float/string as value');
    }
    $value = self::canonicalise($value);
    parent::__construct($value, null, $datatype);
  }

  /** Return the value of the literal cast to a PHP string
   *
   * @return string
   */
  public function getValue() {
    return strval($this->value);
  }

  /**
   * @param string $value
   *
   * @throws UnexpectedValueException
   */
  public static function validate($value) {
    if (!mb_ereg_match(self::DECIMAL_REGEX, $value)) {
      throw new UnexpectedValueException("'{$value}' doesn't look like a valid decimal");
    }
  }

  /**
   * Converts valid xsd:decimal literal to Canonical representation
   * see http://www.w3.org/TR/xmlschema-2/#decimal
   *
   * @param string $value Valid xsd:decimal literal
   *
   * @return string
   */
  public static function canonicalise($value) {
    $pieces = array();
    mb_ereg(self::DECIMAL_REGEX, $value, $pieces);
    $sign = $pieces[1] === '-' ? '-' : '';

    // '+' is not allowed
    $integer = ltrim($pieces[4] !== false ? $pieces[4] : $pieces[7], '0');
    $fractional = rtrim($pieces[5], '0');
    if (empty($integer)) {
      $integer = '0';
    }
    if (empty($fractional)) {
      $fractional = '0';
    }
    return "{$sign}{$integer}.{$fractional}";
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EasyRdf_Literal::$classMap private static property @ignore A mapping from class name to datatype URI
EasyRdf_Literal::$datatype protected property @ignore The datatype URI of the literal
EasyRdf_Literal::$datatypeMap private static property @ignore a mapping from datatype uri to class name
EasyRdf_Literal::$lang protected property @ignore The language of the literal (e.g. 'en')
EasyRdf_Literal::$value protected property @ignore The string value for this literal
EasyRdf_Literal::create public static function Create a new literal object
EasyRdf_Literal::deleteDatatypeMapping public static function Remove the mapping between an RDF datatype and a PHP class name
EasyRdf_Literal::dumpValue public function Return pretty-print view of the literal
EasyRdf_Literal::getDatatype public function Returns the shortened datatype URI of the literal.
EasyRdf_Literal::getDatatypeForValue public static function Get datatype URI for a PHP value.
EasyRdf_Literal::getDatatypeUri public function Returns the full datatype URI of the literal.
EasyRdf_Literal::getLang public function Returns the language of the literal.
EasyRdf_Literal::setDatatypeMapping public static function Register an RDF datatype with a PHP class name
EasyRdf_Literal::toRdfPhp public function Returns the properties of the literal as an associative array
EasyRdf_Literal::__toString public function Magic method to return the value of a literal as a string
EasyRdf_Literal_Decimal::canonicalise public static function Converts valid xsd:decimal literal to Canonical representation see http://www.w3.org/TR/xmlschema-2/#decimal
EasyRdf_Literal_Decimal::DECIMAL_REGEX constant written according to http://www.w3.org/TR/xmlschema-2/#decimal
EasyRdf_Literal_Decimal::getValue public function Return the value of the literal cast to a PHP string Overrides EasyRdf_Literal::getValue
EasyRdf_Literal_Decimal::validate public static function
EasyRdf_Literal_Decimal::__construct public function Constructor for creating a new decimal literal Overrides EasyRdf_Literal::__construct