本文整理汇总了PHP中Symfony\Component\ExpressionLanguage\ExpressionLanguage::parse方法的典型用法代码示例。如果您正苦于以下问题:PHP ExpressionLanguage::parse方法的具体用法?PHP ExpressionLanguage::parse怎么用?PHP ExpressionLanguage::parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\ExpressionLanguage\ExpressionLanguage
的用法示例。
在下文中一共展示了ExpressionLanguage::parse方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getExpression
/**
* @return \Symfony\Component\ExpressionLanguage\ParsedExpression
*/
protected function getExpression()
{
if (!$this->parsedExpression) {
$this->parsedExpression = $this->expressionLanguage->parse($this, $this->keys);
}
return $this->parsedExpression;
}
示例2: evaluate
/**
* @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;
}
示例3: getExpression
/**
* @return \Symfony\Component\ExpressionLanguage\Expression
*/
protected function getExpression()
{
if (!$this->expression) {
$this->expression = $this->expressionLanguage->parse($this->getName(), array('subject', 'context'));
}
return $this->expression;
}
示例4: getExpression
/**
* @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;
}
示例5: evaluate
/**
* @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);
}
示例6: testCachedParse
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);
}
示例7: testCachedParseWithDeprecatedParserCacheInterface
/**
* @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);
}
示例8: execute
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]));
}
开发者ID:GrizliK1988,项目名称:symfony-certification-prepare-project,代码行数:20,代码来源:ExpressionLanguageCommand.php
示例9: filter
/**
* 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;
});
}