function PoHeader::tokenizeFormula

Same name in other branches
  1. 9 core/lib/Drupal/Component/Gettext/PoHeader.php \Drupal\Component\Gettext\PoHeader::tokenizeFormula()
  2. 8.9.x core/lib/Drupal/Component/Gettext/PoHeader.php \Drupal\Component\Gettext\PoHeader::tokenizeFormula()
  3. 10 core/lib/Drupal/Component/Gettext/PoHeader.php \Drupal\Component\Gettext\PoHeader::tokenizeFormula()

Tokenize the formula.

Parameters

string $formula: A string containing the arithmetic formula.

Return value

array List of arithmetic tokens identified in the formula.

1 call to PoHeader::tokenizeFormula()
PoHeader::parseArithmetic in core/lib/Drupal/Component/Gettext/PoHeader.php
Parses and sanitizes an arithmetic formula into a plural element stack.

File

core/lib/Drupal/Component/Gettext/PoHeader.php, line 376

Class

PoHeader
Gettext PO header handler.

Namespace

Drupal\Component\Gettext

Code

private function tokenizeFormula($formula) {
    $formula = str_replace(" ", "", $formula);
    $tokens = [];
    for ($i = 0; $i < strlen($formula); $i++) {
        if (is_numeric($formula[$i])) {
            $num = $formula[$i];
            $j = $i + 1;
            while ($j < strlen($formula) && is_numeric($formula[$j])) {
                $num .= $formula[$j];
                $j++;
            }
            $i = $j - 1;
            $tokens[] = $num;
        }
        elseif ($pos = strpos(" =<>!&|", $formula[$i])) {
            $next = $formula[$i + 1];
            switch ($pos) {
                case 1:
                case 2:
                case 3:
                case 4:
                    if ($next == '=') {
                        $tokens[] = $formula[$i] . '=';
                        $i++;
                    }
                    else {
                        $tokens[] = $formula[$i];
                    }
                    break;
                case 5:
                    if ($next == '&') {
                        $tokens[] = '&&';
                        $i++;
                    }
                    else {
                        $tokens[] = $formula[$i];
                    }
                    break;
                case 6:
                    if ($next == '|') {
                        $tokens[] = '||';
                        $i++;
                    }
                    else {
                        $tokens[] = $formula[$i];
                    }
                    break;
            }
        }
        else {
            $tokens[] = $formula[$i];
        }
    }
    return $tokens;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.