ILIAS  release_8 Revision v8.24
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
69 public function __construct()
70 {
71 include_once 'Modules/TestQuestionPool/classes/questions/LogicalAnswerCompare/Factory/ilAssLacExpressionManufacturer.php';
72 include_once 'Modules/TestQuestionPool/classes/questions/LogicalAnswerCompare/Factory/ilAssLacOperationManufacturer.php';
73 include_once "Modules/TestQuestionPool/classes/questions/LogicalAnswerCompare/ilAssLacCompositeBuilder.php";
74 }
75
85 {
86 $this->condition = $condition;
87 $this->index = 0;
88 $this->checkBrackets();
89 $this->fetchExpressions();
90 $this->fetchOperators();
92 $nodes = $this->createNodeArray();
93 $compositeBuilder = new ilAssLacCompositeBuilder();
94 return $compositeBuilder->create($nodes);
95 }
96
104 protected function fetchExpressions(): void
105 {
107 $this->expressions = $manufacturer->match($this->condition);
108 }
109
117 protected function fetchOperators(): void
118 {
120 $this->operators = $manufacturer->match($this->condition);
121 }
122
130 protected function cannonicalizeCondition(): void
131 {
133 $this->condition = preg_replace($manufacturer->getPattern(), 'n', $this->condition);
135 $this->condition = preg_replace($manufacturer->getPattern(), 'o', $this->condition);
136 $this->condition = preg_replace("/no/", "n o", $this->condition);
137 $this->condition = preg_replace("/on/", "o n", $this->condition);
138
139 for ($i = 0; $i < strlen($this->condition); $i++) {
140 if ($this->condition[$i] == "!" && !$this->isNegationSurroundedByBrackets($i)) {
142 }
143 }
144 }
145
146 protected function checkBrackets(): void
147 {
148 $num_brackets_open = substr_count($this->condition, "(");
149 $num_brackets_close = substr_count($this->condition, ")");
150
151 if ($num_brackets_open > $num_brackets_close) {
152 throw new ilAssLacMissingBracket(")");
153 }
154 if ($num_brackets_open < $num_brackets_close) {
155 throw new ilAssLacMissingBracket("(");
156 }
157 }
158
181 protected function createNodeArray(): array
182 {
183 $expected = array("n", "(", "!");
184 $group = array();
185 $negation = false;
186
187 while ($this->index < strlen($this->condition)) {
188 $a = $this->condition[$this->index];
189 if (trim($this->condition[$this->index]) != "" && in_array($this->condition[$this->index], $expected)) {
190 if ($this->condition[$this->index] == ')') {
191 return $group;
192 } elseif ($this->condition[$this->index] == 'n') {
193 $group[] = array('type' => 'expression', 'value' => array_shift($this->expressions));
194 $expected = array("o", ")");
195 } elseif ($this->condition[$this->index] == 'o') {
196 $group[] = array('type' => 'operator', 'value' => array_shift($this->operators));
197 $expected = array("n", "(", "!");
198 } elseif ($this->condition[$this->index] == '(') {
199 $this->index++;
200 $elements = $this->createNodeArray();
201 $group[] = array('type' => "group", "negated" => $negation, 'nodes' => $elements);
202 $negation = false;
203 $expected = array("o",")");
204 } elseif ($this->condition[$this->index] == "!") {
205 $negation = true;
206 }
207 } elseif (trim($this->condition[$this->index]) != "") {
208 throw new ilAssLacConditionParserException($this->index - $this->spaces + 1);
209 } else {
210 $this->spaces++;
211 }
212
213 $this->index++;
214 }
215 return array('type' => 'group', "negated" => $negation, 'nodes' => $group);
216 }
217
221 public function getExpressions(): array
222 {
223 return $this->expressions;
224 }
225
229 protected function surroundNegationExpression($index): void
230 {
231 $start = strpos($this->condition, "n", $index + 1);
232 $end = false;
233
234 if ($start !== false) {
235 $end = strpos($this->condition, "n", $start + 1);
236 }
237
238 if ($start !== false && $end !== false) {
239 $this->condition = substr_replace($this->condition, "(n o n)", $start, $end - $start + 1);
240 }
241 }
242
249 {
250 $next_bracket = strpos($this->condition, "(", $index + 1);
251 $next_expression = strpos($this->condition, "n", $index + 1);
252
253 return $next_bracket !== false && $next_bracket < $next_expression;
254 }
255}
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...
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...
cannonicalizeCondition()
Cannonicalize the condition into a more general form.
fetchExpressions()
Matches all expressions in the current condition and assign these to the class attribute ConditionPar...
fetchOperators()
Matches all operators in the current condition and assign these to the class attribute ConditionParse...
__construct()
Construct requirements.
parse($condition)
Parses the delivered condition and creates a composite tree Structure.
static _getInstance()
Get an Instance of ExpressionManufacturer.
static _getInstance()
Get an Instance of OperationManufacturer.
$i
Definition: metadata.php:41
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples