You are here

protected function EasyRdf_Parser_Turtle::parseNumber in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/Turtle.php \EasyRdf_Parser_Turtle::parseNumber()

Parses a numeric value, either of type integer, decimal or double @ignore

1 call to EasyRdf_Parser_Turtle::parseNumber()
EasyRdf_Parser_Turtle::parseValue in vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/Turtle.php
Parses an RDF value. This method parses uriref, qname, node ID, quoted literal, integer, double and boolean. @ignore

File

vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/Turtle.php, line 762

Class

EasyRdf_Parser_Turtle
Class to parse Turtle with no external dependancies.

Code

protected function parseNumber() {
  $value = '';
  $datatype = EasyRdf_Namespace::get('xsd') . 'integer';
  $c = $this
    ->read();

  // read optional sign character
  if ($c == '+' || $c == '-') {
    $value .= $c;
    $c = $this
      ->read();
  }
  while (ctype_digit($c)) {
    $value .= $c;
    $c = $this
      ->read();
  }
  if ($c == '.' || $c == 'e' || $c == 'E') {

    // read optional fractional digits
    if ($c == '.') {
      if (self::isWhitespace($this
        ->peek())) {

        // We're parsing an integer that did not have a space before the
        // period to end the statement
      }
      else {
        $value .= $c;
        $c = $this
          ->read();
        while (ctype_digit($c)) {
          $value .= $c;
          $c = $this
            ->read();
        }
        if (mb_strlen($value, "UTF-8") == 1) {

          // We've only parsed a '.'
          throw new EasyRdf_Parser_Exception("Turtle Parse Error: object for statement missing", $this->line, $this->column);
        }

        // We're parsing a decimal or a double
        $datatype = EasyRdf_Namespace::get('xsd') . 'decimal';
      }
    }
    else {
      if (mb_strlen($value, "UTF-8") == 0) {

        // We've only parsed an 'e' or 'E'
        throw new EasyRdf_Parser_Exception("Turtle Parse Error: object for statement missing", $this->line, $this->column);
      }
    }

    // read optional exponent
    if ($c == 'e' || $c == 'E') {
      $datatype = EasyRdf_Namespace::get('xsd') . 'double';
      $value .= $c;
      $c = $this
        ->read();
      if ($c == '+' || $c == '-') {
        $value .= $c;
        $c = $this
          ->read();
      }
      if (!ctype_digit($c)) {
        throw new EasyRdf_Parser_Exception("Turtle Parse Error: exponent value missing", $this->line, $this->column);
      }
      $value .= $c;
      $c = $this
        ->read();
      while (ctype_digit($c)) {
        $value .= $c;
        $c = $this
          ->read();
      }
    }
  }

  // Unread last character, it isn't part of the number
  $this
    ->unread($c);

  // Return result as a typed literal
  return array(
    'type' => 'literal',
    'value' => $value,
    'datatype' => $datatype,
  );
}