/** * @param RenderingContextInterface $renderingContext * @param string $expression * @param array $matches * @return integer|float */ public static function evaluateExpression(RenderingContextInterface $renderingContext, $expression, array $matches) { $expression = trim($expression, '{}'); list($variable, $type) = explode(' as ', $expression); $variable = parent::getTemplateVariableOrValueItself($variable, $renderingContext); if (!in_array($type, self::$validTypes)) { $type = parent::getTemplateVariableOrValueItself($type, $renderingContext); } if (!in_array($type, self::$validTypes)) { throw new Exception(sprintf('Invalid target conversion type "%s" specified in casting expression', $type)); } return self::convert($variable, $type); }
/** * @param RenderingContextInterface $renderingContext * @param string $expression * @param array $matches * @return integer|float */ public static function evaluateExpression(RenderingContextInterface $renderingContext, $expression, array $matches) { // Split the expression on all recognized operators $matches = array(); preg_match_all('/([+\\-*\\^\\/\\%]|[a-z0-9\\.]+)/s', $expression, $matches); $matches[0] = array_map('trim', $matches[0]); // Like the BooleanNode, we dumb down the processing logic to not apply // any special precedence on the priority of operators. We simply process // them in order. $variables = $renderingContext->getVariableProvider()->getAll(); $result = array_shift($matches[0]); $result = parent::getTemplateVariableOrValueItself($result, $renderingContext); $operator = NULL; $operators = array('*', '^', '-', '+', '/', '%'); foreach ($matches[0] as $part) { if (in_array($part, $operators)) { $operator = $part; } else { $part = parent::getTemplateVariableOrValueItself($part, $renderingContext); $result = self::evaluateOperation($result, $operator, $part); } } return $result; }