ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ExpressionParser.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
29
31{
32 private static array $cached_tokens = [];
37
38 public function __construct(
39 private string $expression,
40 private FieldSubstitution $substitution,
41 ) {
42 $this->tokenizer = new Tokenizer();
43
44 $this->math_result_resolver = new MathResultResolver(
45 $substitution,
46 $this->tokenizer
47 );
48 $this->substition_result_resolver = new SubstitutionResultResolver($substitution);
49
50 $this->result_formatter = new ResultFormatter();
51 }
52
53 public function parse(): string
54 {
55 if (!isset(self::$cached_tokens[$this->substitution->getFieldId()])) {
56 self::$cached_tokens[$this->substitution->getFieldId()] = $this->tokenizer->tokenize($this->expression);
57 }
58 $tokens = self::$cached_tokens[$this->substitution->getFieldId()];
59 $parsed = '';
60
61 foreach ($tokens as $token) {
62 if ($token->getValue() === '') {
63 continue;
64 }
65 if ($token instanceof MathToken) {
66 $result = $this->math_result_resolver->resolve($token);
67 } else {
68 $result = $this->substition_result_resolver->resolve($token);
69 }
70
71 $parsed .= $this->result_formatter->format($result);
72 }
73
74 return $parsed;
75 }
76}
__construct(private string $expression, private FieldSubstitution $substitution,)
$token
Definition: xapitoken.php:70