/** * @test * @dataProvider getSomeEvaluationTestValues * @param string $comparison * @param boolean $expected */ public function testSomeEvaluations($comparison, $expected, $variables = array()) { $parser = new BooleanParser(); $this->assertEquals($expected, BooleanNode::convertToBoolean($parser->evaluate($comparison, $variables), $this->renderingContext), 'Expression: ' . $comparison); $compiledEvaluation = $parser->compile($comparison); $functionName = 'expression_' . md5($comparison . rand(0, 100000)); eval('function ' . $functionName . '($context) {return ' . $compiledEvaluation . ';}'); $this->assertEquals($expected, BooleanNode::convertToBoolean($functionName($variables), $this->renderingContext), 'compiled Expression: ' . $compiledEvaluation); }
/** * @param RenderingContextInterface $renderingContext * @param string $expression * @param array $matches * @return mixed */ public static function evaluateExpression(RenderingContextInterface $renderingContext, $expression, array $matches) { $parts = preg_split('/([\\?:])/s', $expression); $parts = array_map([__CLASS__, 'trimPart'], $parts); list($check, $then, $else) = $parts; $context = static::gatherContext($renderingContext, $expression); $parser = new BooleanParser(); $checkResult = $parser->evaluate($check, $context); if ($checkResult) { return static::getTemplateVariableOrValueItself($renderingContext->getTemplateParser()->unquoteString($then), $renderingContext); } else { return static::getTemplateVariableOrValueItself($renderingContext->getTemplateParser()->unquoteString($else), $renderingContext); } }
/** * Takes a stack of nodes evaluates it with the end result * being a single boolean value. Creates new BooleanNodes * recursively to process braced expressions as single units. * * @param RenderingContextInterface $renderingContext * @param array $expressionParts * @return boolean the boolean value */ public static function evaluateStack(RenderingContextInterface $renderingContext, array $expressionParts) { $expression = static::reconcatenateExpression($expressionParts); $context = static::gatherContext($renderingContext, $expressionParts); $parser = new BooleanParser(); return static::convertToBoolean($parser->evaluate($expression, $context), $renderingContext); }