ILIAS  trunk Revision v11.0_alpha-1731-gff9cd7e2bd3
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
ilAssLacConditionParser.php
Go to the documentation of this file.
1 <?php
2 
28 {
34  protected $condition;
35 
42  protected $expressions;
43 
50  protected $operators;
51 
57  protected $index;
58 
64  protected $spaces;
65 
75  {
76  $this->condition = $condition;
77  $this->index = 0;
78  $this->checkBrackets();
79  $this->fetchExpressions();
80  $this->fetchOperators();
81  $this->cannonicalizeCondition();
82  $nodes = $this->createNodeArray();
83  $compositeBuilder = new ilAssLacCompositeBuilder();
84  return $compositeBuilder->create($nodes);
85  }
86 
94  protected function fetchExpressions(): void
95  {
97  $this->expressions = $manufacturer->match($this->condition);
98  }
99 
107  protected function fetchOperators(): void
108  {
110  $this->operators = $manufacturer->match($this->condition);
111  }
112 
120  protected function cannonicalizeCondition(): void
121  {
123  $this->condition = preg_replace($manufacturer->getPattern(), 'n', $this->condition);
125  $this->condition = preg_replace($manufacturer->getPattern(), 'o', $this->condition);
126  $this->condition = preg_replace("/no/", "n o", $this->condition);
127  $this->condition = preg_replace("/on/", "o n", $this->condition);
128 
129  for ($i = 0; $i < strlen($this->condition); $i++) {
130  if ($this->condition[$i] == "!" && !$this->isNegationSurroundedByBrackets($i)) {
131  $this->surroundNegationExpression($i);
132  }
133  }
134  }
135 
136  protected function checkBrackets(): void
137  {
138  $num_brackets_open = substr_count($this->condition, "(");
139  $num_brackets_close = substr_count($this->condition, ")");
140 
141  if ($num_brackets_open > $num_brackets_close) {
142  throw new ilAssLacMissingBracket(")");
143  }
144  if ($num_brackets_open < $num_brackets_close) {
145  throw new ilAssLacMissingBracket("(");
146  }
147  }
148 
171  protected function createNodeArray(): array
172  {
173  $expected = ["n", "(", "!"];
174  $group = [];
175  $negation = false;
176 
177  while ($this->index < strlen($this->condition)) {
178  $a = $this->condition[$this->index];
179  if (trim($this->condition[$this->index]) != "" && in_array($this->condition[$this->index], $expected)) {
180  if ($this->condition[$this->index] == ')') {
181  return $group;
182  } elseif ($this->condition[$this->index] == 'n') {
183  $group[] = ['type' => 'expression', 'value' => array_shift($this->expressions)];
184  $expected = ["o", ")"];
185  } elseif ($this->condition[$this->index] == 'o') {
186  $group[] = ['type' => 'operator', 'value' => array_shift($this->operators)];
187  $expected = ["n", "(", "!"];
188  } elseif ($this->condition[$this->index] == '(') {
189  $this->index++;
190  $elements = $this->createNodeArray();
191  $group[] = ['type' => "group", "negated" => $negation, 'nodes' => $elements];
192  $negation = false;
193  $expected = ["o",")"];
194  } elseif ($this->condition[$this->index] == "!") {
195  $negation = true;
196  }
197  } elseif (trim($this->condition[$this->index]) != "") {
198  throw new ilAssLacConditionParserException($this->index - $this->spaces + 1);
199  } else {
200  $this->spaces++;
201  }
202 
203  $this->index++;
204  }
205  return ['type' => 'group', "negated" => $negation, 'nodes' => $group];
206  }
207 
211  public function getExpressions(): array
212  {
213  return $this->expressions;
214  }
215 
219  protected function surroundNegationExpression($index): void
220  {
221  $start = strpos($this->condition, "n", $index + 1);
222  $end = false;
223 
224  if ($start !== false) {
225  $end = strpos($this->condition, "n", $start + 1);
226  }
227 
228  if ($start !== false && $end !== false) {
229  $this->condition = substr_replace($this->condition, "(n o n)", $start, $end - $start + 1);
230  }
231  }
232 
239  {
240  $next_bracket = strpos($this->condition, "(", $index + 1);
241  $next_expression = strpos($this->condition, "n", $index + 1);
242 
243  return $next_bracket !== false && $next_bracket < $next_expression;
244  }
245 }
cannonicalizeCondition()
Cannonicalize the condition into a more general form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
createNodeArray()
Creates an array representing all Nodes in a condition based on the fetched expressions and operators...
static _getInstance()
Get an Instance of ExpressionManufacturer.
fetchOperators()
Matches all operators in the current condition and assign these to the class attribute ConditionParse...
fetchExpressions()
Matches all expressions in the current condition and assign these to the class attribute ConditionPar...
static _getInstance()
Get an Instance of OperationManufacturer.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
parse($condition)
Parses the delivered condition and creates a composite tree Structure.