Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. NumericLiteral.php

function NumericLiteral::setRaw

Sets node's raw value

Parameters

mixed $raw Raw value:

Return value

$this

Throws

\Exception

Overrides Literal::setRaw

File

vendor/mck89/peast/lib/Peast/Syntax/Node/NumericLiteral.php, line 107

Class

NumericLiteral
A node that represents a numeric literal.

Namespace

Peast\Syntax\Node

Code

public function setRaw($raw) {
    $value = $raw;
    $format = self::DECIMAL;
    if (is_string($value) && $value !== "") {
        
        //Hexadecimal, binary or octal
        $startZero = $value[0] === "0";
        $form = $startZero && isset($value[1]) ? strtolower($value[1]) : null;
        
        //Numeric separator cannot appear at the beginning or at the end of the number
        if (preg_match("/^_|_\$/", $value)) {
            throw new \Exception("Invalid numeric value");
        }
        elseif (isset($this->forms[$form])) {
            $formDef = $this->forms[$form];
            if (!preg_match($formDef["check"], $value)) {
                throw new \Exception("Invalid " . $formDef["format"]);
            }
            $value = str_replace("_", "", $value);
            $value = $formDef["conv"]($value);
            $format = $formDef["format"];
        }
        elseif ($startZero && preg_match("/^0[0-7_]+\$/", $value)) {
            
            //Legacy octal form
            $value = str_replace("_", "", $value);
            $value = octdec($value);
            $format = self::OCTAL;
        }
        elseif (preg_match("/^([\\d_]*\\.?[\\d_]*)(?:e[+\\-]?[\\d_]+)?\$/i", $value, $match) && $match[1] !== "" && $match[1] !== "." && !preg_match("/_e|e[+-]?_|_\$/", $value)) {
            $value = str_replace("_", "", $value);
        }
        else {
            throw new \Exception("Invalid numeric value");
        }
    }
    elseif (!is_int($value) && !is_float($value)) {
        throw new \Exception("Invalid numeric value");
    }
    $value = (double) $value;
    $intValue = (int) $value;
    if ($value == $intValue) {
        $value = $intValue;
    }
    $this->format = $format;
    $this->value = $value;
    $this->raw = $raw;
    return $this;
}
RSS feed
Powered by Drupal