/** * @return \Symfony\Component\ExpressionLanguage\ParsedExpression */ protected function getExpression() { if (!$this->parsedExpression) { $this->parsedExpression = $this->expressionLanguage->parse($this, $this->keys); } return $this->parsedExpression; }
/** * @param string $expression * @param mixed $data * @return mixed */ public function evaluate($expression, $data) { if (!is_string($expression)) { return $expression; } $key = $expression; if (!array_key_exists($key, $this->cache)) { if (!preg_match(self::EXPRESSION_REGEX, $expression, $matches)) { $this->cache[$key] = false; } else { $expression = $matches['expression']; $context = $this->context; $context['object'] = $data; $this->cache[$key] = $this->expressionLanguage->parse($expression, array_keys($context)); } } if (false !== $this->cache[$key]) { if (!isset($context)) { $context = $this->context; $context['object'] = $data; } return $this->expressionLanguage->evaluate($this->cache[$key], $context); } return $expression; }
/** * @return \Symfony\Component\ExpressionLanguage\Expression */ protected function getExpression() { if (!$this->expression) { $this->expression = $this->expressionLanguage->parse($this->getName(), array('subject', 'context')); } return $this->expression; }
/** * @return \Symfony\Component\ExpressionLanguage\Expression */ protected function getExpression() { if (!$this->expression) { $keys = array_keys($this->values); $keys[] = 'subject'; $keys[] = 'context'; $this->expression = $this->expressionLanguage->parse($this->getName(), $keys); } return $this->expression; }
/** * @param string $expression * @param mixed $data * @return mixed */ public function evaluate($expression, $data) { if (!preg_match(self::EXPRESSION_REGEX, $expression, $matches)) { return $expression; } $expression = $matches['expression']; $context = array_merge($this->context, array('object' => $data)); $parsedExpression = $this->expressionLanguage->parse($expression, array_keys($context)); return $this->expressionLanguage->evaluate($parsedExpression, $context); }
public function testCachedParse() { $cacheMock = $this->getMock('Symfony\\Component\\ExpressionLanguage\\ParserCache\\ParserCacheInterface'); $savedParsedExpression = null; $expressionLanguage = new ExpressionLanguage($cacheMock); $cacheMock->expects($this->exactly(2))->method('fetch')->with('1 + 1//')->will($this->returnCallback(function () use(&$savedParsedExpression) { return $savedParsedExpression; })); $cacheMock->expects($this->exactly(1))->method('save')->with('1 + 1//', $this->isInstanceOf('Symfony\\Component\\ExpressionLanguage\\ParsedExpression'))->will($this->returnCallback(function ($key, $expression) use(&$savedParsedExpression) { $savedParsedExpression = $expression; })); $parsedExpression = $expressionLanguage->parse('1 + 1', array()); $this->assertSame($savedParsedExpression, $parsedExpression); $parsedExpression = $expressionLanguage->parse('1 + 1', array()); $this->assertSame($savedParsedExpression, $parsedExpression); }
/** * @group legacy */ public function testCachedParseWithDeprecatedParserCacheInterface() { $cacheMock = $this->getMock('Symfony\\Component\\ExpressionLanguage\\ParserCache\\ParserCacheInterface'); $cacheItemMock = $this->getMock('Psr\\Cache\\CacheItemInterface'); $savedParsedExpression = null; $expressionLanguage = new ExpressionLanguage($cacheMock); $cacheMock->expects($this->exactly(1))->method('fetch')->with('1%20%2B%201%2F%2F')->willReturn($savedParsedExpression); $cacheMock->expects($this->exactly(1))->method('save')->with('1%20%2B%201%2F%2F', $this->isInstanceOf(ParsedExpression::class))->will($this->returnCallback(function ($key, $expression) use(&$savedParsedExpression) { $savedParsedExpression = $expression; })); $parsedExpression = $expressionLanguage->parse('1 + 1', array()); $this->assertSame($savedParsedExpression, $parsedExpression); }
protected function execute(InputInterface $input, OutputInterface $output) { $expressionLanguage = new ExpressionLanguage(); $expressionLanguage->registerProvider(new StringExpressionLanguageProvider()); var_dump($expressionLanguage->evaluate('1+2')); $compiled = $expressionLanguage->compile('"TEST"~" "~"aaa"'); var_dump($compiled); $testClass = new TestClass(); $testClass->aa = 123; var_dump($expressionLanguage->evaluate('test.aa~" "~test.hi()', ['test' => $testClass])); $language = new ExpressionLanguage(null, [new StringExpressionLanguageProvider()]); var_dump($language->evaluate('lowercase("AAA")')); eval('var_dump(' . $language->compile('lowercase("AAA")') . ');'); $expr = new Expression('(1+2)*test.aa'); $parsedExpression = $language->parse($expr, ['test']); var_dump($parsedExpression); var_dump($language->evaluate($parsedExpression, ['test' => $testClass])); $serializedExpression = new SerializedParsedExpression('(1+2)*test.aa', serialize($parsedExpression->getNodes())); var_dump($language->evaluate($serializedExpression, ['test' => $testClass])); }
/** * Filters the items by expression * * @param array $array The input array * @param string $expression The expression * If the expression returns true, the current value from array is returned into * the result array. Array keys are preserved. * Use "item" alias in the expression for access to iterated array item. * * @return array */ public static function filter($array, $expression) { $array = self::cast($array); $language = new ExpressionLanguage(); $parsedNodes = $language->parse($expression, ['item'])->getNodes(); return array_filter($array, function ($item) use($parsedNodes) { $res = (bool) $parsedNodes->evaluate([], ['item' => $item]); return $res; }); }