eefb07ca8807280d8543107a2a92e90a429b3faf
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / ExpressionLanguage / Resolver.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\ExpressionLanguage;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
19
20 /**
21 * Class Resolver
22 * @internal
23 */
24 class Resolver
25 {
26 /**
27 * @var ProviderInterface
28 */
29 protected $provider;
30
31 /**
32 * @var \Symfony\Component\ExpressionLanguage\ExpressionLanguage
33 */
34 protected $expressionLanguage;
35
36 /**
37 * @var array
38 */
39 public $expressionLanguageVariables = [];
40
41 /**
42 * @param ProviderInterface $provider
43 */
44 public function __construct(ProviderInterface $provider)
45 {
46 $this->provider = $provider;
47 $this->expressionLanguage = new ExpressionLanguage(null, $provider->getExpressionLanguageProviders());
48 $this->expressionLanguageVariables = $provider->getExpressionLanguageVariables();
49 }
50
51 /**
52 * Evaluate an expression.
53 *
54 * @param string $condition The expression to parse
55 * @return bool
56 */
57 public function evaluate(string $condition): bool
58 {
59 return (bool)$this->expressionLanguage->evaluate($condition, $this->expressionLanguageVariables);
60 }
61
62 /**
63 * Compiles an expression source code.
64 *
65 * @param string $condition The expression to compile
66 * @return string
67 */
68 public function compile(string $condition): string
69 {
70 return (string)$this->expressionLanguage->compile($condition, array_keys($this->expressionLanguageVariables));
71 }
72 }