Class ilDclExpressionParser.
More...
◆ __construct()
◆ calculate()
ilDclExpressionParser::calculate |
( |
|
$operator, |
|
|
|
$left, |
|
|
|
$right |
|
) |
| |
|
protected |
◆ calculateFunction()
ilDclExpressionParser::calculateFunction |
( |
|
$function, |
|
|
array |
$args = array() |
|
) |
| |
|
protected |
Calculate a function with its arguments.
- Parameters
-
| $function | Function name to calculate |
array | $args | Arguments of function |
- Returns
- float|int|number
- Exceptions
-
Definition at line 403 of file class.ilDclExpressionParser.php.
Referenced by calculateFunctions().
407 $count = count($args);
409 return ($count) ? array_sum($args) / $count : 0;
411 return array_sum($args);
417 throw new ilException(
"Unrecognized function '$function'");
◆ calculateFunctions()
ilDclExpressionParser::calculateFunctions |
( |
|
$token | ) |
|
|
protected |
Execute any math functions inside a token.
- Parameters
-
- Returns
- string
Definition at line 195 of file class.ilDclExpressionParser.php.
References $result, PHPMailer\PHPMailer\$token, calculateFunction(), getFunctionArgs(), and substituteFieldValues().
Referenced by parse().
197 if (isset(self::$cache_math_function_tokens[$this->field->getId()][
$token])) {
198 $result = self::$cache_math_function_tokens[$this->field->getId()][
$token];
204 foreach (self::getFunctions() as $function) {
205 $pattern .=
"($function)\\(([^)]*)\\)|";
207 if (!preg_match_all(rtrim($pattern,
'|') .
'#',
$token,
$result)) {
209 self::$cache_math_function_tokens[$this->field->getId()][
$token] =
false;
215 foreach (
$result[0] as $k => $to_replace) {
217 $function = $function_args[
'function'];
substituteFieldValues(array $tokens)
Given an array of tokens, replace each token that is a placeholder (e.g.
getFunctionArgs($index, array $data)
Helper method to return the function and its arguments from a preg_replace_all $result array...
calculateFunction($function, array $args=array())
Calculate a function with its arguments.
◆ formatScientific()
ilDclExpressionParser::formatScientific |
( |
|
$value | ) |
|
|
protected |
- Parameters
-
- Returns
- string
Definition at line 129 of file class.ilDclExpressionParser.php.
Referenced by parse().
131 if (abs($value) >= self::SCIENTIFIC_NOTATION_UPPER) {
132 return sprintf(
"%e", $value);
134 if (abs($value) <= self::SCIENTIFIC_NOTATION_LOWER && $value != 0) {
135 return sprintf(
"%e", $value);
137 if (is_float($value)) {
◆ getFunctionArgs()
ilDclExpressionParser::getFunctionArgs |
( |
|
$index, |
|
|
array |
$data |
|
) |
| |
|
protected |
Helper method to return the function and its arguments from a preg_replace_all $result array.
- Parameters
-
- Returns
- array
Definition at line 234 of file class.ilDclExpressionParser.php.
References $i, and $index.
Referenced by calculateFunctions().
243 $function = $_data[
$index];
244 $args = explode(
';',
$data[
$i + 1][$index]);
245 $return[
'function'] = $function;
246 $return[
'args'] = $args;
◆ getFunctions()
static ilDclExpressionParser::getFunctions |
( |
| ) |
|
|
static |
◆ getOperators()
static ilDclExpressionParser::getOperators |
( |
| ) |
|
|
static |
◆ isMathToken()
ilDclExpressionParser::isMathToken |
( |
|
$token | ) |
|
|
protected |
◆ parse()
ilDclExpressionParser::parse |
( |
| ) |
|
Parse expression and return result.
This method loops the tokens and checks if Token is of type string or math. Concatenates results to produce resulting string of parsed expression.
- Exceptions
-
- Returns
- string
Definition at line 86 of file class.ilDclExpressionParser.php.
References PHPMailer\PHPMailer\$token, calculateFunctions(), formatScientific(), ilDclTokenizer\getMathTokens(), ilDclTokenizer\getTokens(), isMathToken(), parseMath(), and substituteFieldValues().
88 if (isset(self::$cache_tokens[$this->field->getId()])) {
89 $tokens = self::$cache_tokens[$this->field->getId()];
92 self::$cache_tokens[$this->field->getId()] = $tokens;
96 foreach ($tokens as
$token) {
110 if (strpos($token,
'"') === 0) {
111 $parsed .= strip_tags(trim($token,
'"'));
112 } elseif (strpos($token,
'[[') === 0) {
113 $parsed .= trim(strip_tags($this->substituteFieldValue($token)));
115 throw new ilException(
"Unrecognized string token: '$token'");
substituteFieldValues(array $tokens)
Given an array of tokens, replace each token that is a placeholder (e.g.
parseMath(array $tokens)
Parse a math expression.
isMathToken($token)
Check if a given token is a math expression.
calculateFunctions($token)
Execute any math functions inside a token.
static getTokens($expression)
Split expression by & (ignore escaped &-symbols with backslash)
static getMathTokens($math_expression)
Generate tokens for a math expression.
◆ parseMath()
ilDclExpressionParser::parseMath |
( |
array |
$tokens | ) |
|
|
protected |
Parse a math expression.
- Parameters
-
- Returns
- null
- Exceptions
-
Definition at line 322 of file class.ilDclExpressionParser.php.
References $operators, $result, PHPMailer\PHPMailer\$token, and calculate().
Referenced by parse().
329 foreach ($tokens as
$token) {
330 if (empty($token)
or is_null($token)) {
333 if (is_numeric($token)
or $token ===
'(') {
334 $stack->push($token);
335 if ($token ===
'(') {
338 } elseif (in_array($token, array_keys(
$operators))) {
340 if ($new_precedence > $precedence || $in_bracket) {
342 $stack->push($token);
343 $precedences->push($new_precedence);
344 $precedence = $new_precedence;
347 while ($new_precedence <= $precedence && $stack->count() > 1) {
348 $right = (float) $stack->pop();
349 $operator = $stack->pop();
350 $left = (float) $stack->pop();
353 $precedence = $precedences->pop();
355 $stack->push($token);
356 $precedence = $new_precedence;
357 $precedences->push($new_precedence);
359 } elseif ($token ===
')') {
362 $elem = $stack->pop();
363 while ($elem !==
'(' && !$stack->isEmpty()) {
365 $elem = $stack->pop();
368 $stack->push($this->
parseMath(array_reverse($_tokens)));
371 throw new ilException(
"Unrecognized token '$token'");
376 if ($stack->count() == 1) {
379 return (ctype_digit((
string)
$result)) ?
$result : number_format($result, self::N_DECIMALS,
'.',
"'");
381 while ($stack->count() >= 2) {
382 $right = $stack->pop();
383 $operator = $stack->pop();
384 $left = $stack->count() ? $stack->pop() : 0;
385 $stack->push($this->
calculate($operator, $left, $right));
calculate($operator, $left, $right)
parseMath(array $tokens)
Parse a math expression.
◆ substituteFieldValues()
ilDclExpressionParser::substituteFieldValues |
( |
array |
$tokens | ) |
|
|
protected |
◆ $cache_fields
ilDclExpressionParser::$cache_fields = array() |
|
staticprotected |
◆ $cache_math_function_tokens
ilDclExpressionParser::$cache_math_function_tokens = array() |
|
staticprotected |
◆ $cache_math_tokens
ilDclExpressionParser::$cache_math_tokens = array() |
|
staticprotected |
◆ $cache_tokens
ilDclExpressionParser::$cache_tokens = array() |
|
staticprotected |
◆ $expression
ilDclExpressionParser::$expression |
|
protected |
◆ $field
ilDclExpressionParser::$field |
|
protected |
◆ $functions
ilDclExpressionParser::$functions |
|
staticprotected |
◆ $operators
ilDclExpressionParser::$operators |
|
staticprotected |
Initial value:= array(
'+' => array('precedence' => 1),
'-' => array('precedence' => 1),
'*' => array('precedence' => 2),
'/' => array('precedence' => 2),
'^' => array('precedence' => 3),
)
Definition at line 30 of file class.ilDclExpressionParser.php.
Referenced by isMathToken(), and parseMath().
◆ $record
ilDclExpressionParser::$record |
|
protected |
◆ N_DECIMALS
const ilDclExpressionParser::N_DECIMALS = 1 |
◆ SCIENTIFIC_NOTATION_LOWER
const ilDclExpressionParser::SCIENTIFIC_NOTATION_LOWER = 0.000000001 |
◆ SCIENTIFIC_NOTATION_UPPER
const ilDclExpressionParser::SCIENTIFIC_NOTATION_UPPER = 1000000000000 |
The documentation for this class was generated from the following file: