Semi-stable version with old variable names
This commit is contained in:
@@ -33,10 +33,20 @@ class NotenRechner {
|
||||
|
||||
private function santinzeString(string $string) : string
|
||||
{
|
||||
$replacePattern = '/[^a-zA-Z0-9(){}+*\/.\-\[\]]/';
|
||||
$replacePatterns = [
|
||||
'/->R\w+/', // 1. Removes ->R followed by digits
|
||||
'/[^a-zA-Z0-9(){}+*\/.[\]\-]/' // 2. Fixed syntax: Allowed characters list
|
||||
];
|
||||
return preg_replace($replacePatterns,'', $string);
|
||||
}
|
||||
|
||||
private function sanitizeStringWithRun(string $string) : string
|
||||
{
|
||||
$replacePattern = '/[^a-zA-Z0-9(){}+*\/.\-\[\]>]/';
|
||||
return preg_replace($replacePattern, "", $string);
|
||||
}
|
||||
|
||||
|
||||
public function getBenoetigteIds(string $schemaRaw)
|
||||
{
|
||||
$schema = $this->santinzeString($schemaRaw);
|
||||
@@ -78,29 +88,67 @@ class NotenRechner {
|
||||
|
||||
}
|
||||
|
||||
|
||||
private function insertValues(string $schemaRaw, array $valuesArray = [])
|
||||
public function getBenoetigteIdsComplexWithRun(string $schemaRaw)
|
||||
{
|
||||
$schema = $this->santinzeString($schemaRaw);
|
||||
$schema = $this->sanitizeStringWithRun($schemaRaw);
|
||||
$indexedMatches = [];
|
||||
|
||||
$idsNeeded = $this->getBenoetigteIds($schemaRaw);
|
||||
$missingIds = array_diff($idsNeeded, array_keys($valuesArray));
|
||||
|
||||
if (!empty($missingIds)) {
|
||||
return ['success' => false, 'value' => 'Fehlende IDs'];
|
||||
}
|
||||
|
||||
try {
|
||||
$returnStr = preg_replace_callback('/\{(\d+)\}/', function($m) use ($valuesArray) {
|
||||
return $valuesArray[$m[1]];
|
||||
}, $schema);
|
||||
return ['success' => true, 'value' => $returnStr];
|
||||
} catch (\Exception $e) {
|
||||
return ['success' => false, 'value' => 'Problem beim Einsetzen der Werte'];
|
||||
// Pattern breakdown:
|
||||
// \{(\d+) -> Match { and capture the first number (Note ID)
|
||||
// (?:\[(\w*)\])? -> OPTIONALLY match [ and capture alphanumeric content inside (Apparatus ID)
|
||||
// (?:\[(\d+)\])? -> OPTIONALLY match [ and capture number inside (Run Number)
|
||||
// \} -> Match the closing }
|
||||
if (preg_match_all('/\{(\d+)(?:\[(\w*)\])?(?:\[(\d+)\])?\}/', $schema, $matches, PREG_SET_ORDER)) {
|
||||
|
||||
foreach ($matches as $match) {
|
||||
$noteId = $match[1];
|
||||
|
||||
$rawGeraet = $match[2] ?? 'S';
|
||||
$geraetId = ($rawGeraet === 'S' || $rawGeraet === '') ? 'A' : $rawGeraet;
|
||||
|
||||
$runNumber = isset($match[3]) ? (int)$match[3] : 1;
|
||||
|
||||
$indexedMatches[] = [
|
||||
'noteId' => $noteId,
|
||||
'geraetId' => $geraetId,
|
||||
'run' => $runNumber
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$arrayRes = $this->getStoreGeraetRun($schemaRaw);
|
||||
|
||||
$indexedMatches['targetRun'] = $arrayRes['run_num'] ?? 'A';
|
||||
$indexedMatches['targetGeraet'] = $arrayRes['geraet_id'] ?? 'A';
|
||||
|
||||
return $indexedMatches;
|
||||
|
||||
}
|
||||
|
||||
private function insertValuesComplex(string $schemaRaw, array $valuesArray, int $currentId, int $runNumber)
|
||||
public function getStoreGeraetRun(string $schemaRaw): array
|
||||
{
|
||||
$schema = $this->sanitizeStringWithRun($schemaRaw);
|
||||
|
||||
$pattern = '/->R(?<run_num>\d+|S)(?:G(?<geraet_id>\d+|S))?/';
|
||||
|
||||
if (preg_match($pattern, $schema, $matchesTagetRun)) {
|
||||
|
||||
$run = $matchesTagetRun['run_num'] ?? 'S';
|
||||
$geraet = $matchesTagetRun['geraet_id'] ?? 'S';
|
||||
|
||||
return [
|
||||
'run_num' => $run === 'S' ? 'A' : (int)$run,
|
||||
'geraet_id' => $geraet === 'S' ? 'A' : (int)$geraet,
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'run_num' => 'A',
|
||||
'geraet_id' => 'A'
|
||||
];
|
||||
}
|
||||
|
||||
private function insertValuesComplex(string $schemaRaw, array $valuesArray, int $currentId, int $runNumber, bool $avr = false)
|
||||
{
|
||||
$schema = $this->santinzeString($schemaRaw);
|
||||
$idsNeededArray = $this->getBenoetigteIdsComplex($schemaRaw);
|
||||
@@ -110,22 +158,26 @@ class NotenRechner {
|
||||
$noteId = $sina['noteId'];
|
||||
$geraetIdSearch = ($sina['geraetId'] === 'A') ? $currentId : $sina['geraetId'];
|
||||
|
||||
if (!isset($valuesArray[$geraetIdSearch][$noteId][$runNumber])) {
|
||||
if (!isset($valuesArray[$geraetIdSearch][$noteId][$runNumber]["value"])) {
|
||||
return ['success' => false, 'value' => "Fehlende Daten für Gerät $geraetIdSearch, Note $noteId, Lauf $runNumber"];
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
$returnStr = preg_replace_callback('/\{(\d+)(?:\[(\w*)\])?(?:\[(\d+)\])?\}/', function($m) use ($valuesArray, $currentId, $runNumber) {
|
||||
$returnStr = preg_replace_callback('/\{(\d+)(?:\[(\w*)\])?(?:\[(\d+)\])?\}/', function($m) use ($valuesArray, $currentId, $runNumber, $avr) {
|
||||
$noteId = $m[1];
|
||||
|
||||
// Get value inside brackets, default to 'S' if none exists
|
||||
$rawGeraet = $m[2] ?? 'S';
|
||||
$geraetId = ($rawGeraet === 'S' || $rawGeraet === '') ? $currentId : $rawGeraet;
|
||||
|
||||
// Return value from [Device][Note][Run]
|
||||
return $valuesArray[$geraetId][$noteId][$runNumber] ?? 0;
|
||||
|
||||
if ($avr && $valuesArray[$geraetId][$noteId][$runNumber]["type"] === 'default_value') {
|
||||
return 'default_value_skip';
|
||||
} else {
|
||||
// Return value from [Device][Note][Run]
|
||||
return $valuesArray[$geraetId][$noteId][$runNumber]["value"] ?? 0;
|
||||
}
|
||||
|
||||
}, $schema);
|
||||
|
||||
return ['success' => true, 'value' => $returnStr];
|
||||
@@ -144,27 +196,15 @@ class NotenRechner {
|
||||
$result = $this->rechner->calculate($rechnung);
|
||||
return ['success' => true, 'value' => floatval($result)];
|
||||
} catch (\ChrisKonnertz\StringCalc\Exceptions\StringCalcException $e) {
|
||||
return ['success' => false, 'value' => 'Problem bei der Berechnung: Ungültige Syntax'];
|
||||
return ['success' => false, 'value' => 'Problem bei der Berechnung: Ungültige Syntax '. $rechnung];
|
||||
} catch (\Exception $e) {
|
||||
return ['success' => false, 'value' => 'Problem bei der Berechnung'];
|
||||
}
|
||||
}
|
||||
|
||||
public function berechneString(string $schemaRaw, array $valuesArray = []) {
|
||||
public function berechneStringComplex(string $schemaRaw, array $valuesArray = [], int $geraetId = -1, int $run_number = 0) {
|
||||
if ($schemaRaw === '') { return ['success' => false, 'value' => 'Leeres Schema']; }
|
||||
|
||||
$rechnungArray = $this->insertValues($schemaRaw, $valuesArray);
|
||||
|
||||
if (!isset($rechnungArray['success']) || !isset($rechnungArray['value']) || !$rechnungArray['success']) {
|
||||
return ['success' => false, 'value' => $rechnungArray['value'] ?? 'Fehler beim Einsetzen der Werte oder Werte fehlen'];
|
||||
}
|
||||
|
||||
return $this->calculate($rechnungArray['value']);
|
||||
}
|
||||
|
||||
public function berechneStringComplex(string $schemaRaw, array $valuesArray = [], int $geraetId = 0, int $run_number = 0) {
|
||||
if ($schemaRaw === '') { return ['success' => false, 'value' => 'Leeres Schema']; }
|
||||
if ($geraetId === 0) { return ['success' => false, 'value' => 'Keine Geraet Id angegeben.']; }
|
||||
if ($geraetId < 0) { return ['success' => false, 'value' => 'Keine Geraet Id angegeben.']; }
|
||||
if ($run_number === 0) { return ['success' => false, 'value' => 'Keine Run_Number angegeben.']; }
|
||||
|
||||
$rechnungArray = $this->insertValuesComplex($schemaRaw, $valuesArray, $geraetId, $run_number);
|
||||
@@ -176,9 +216,9 @@ class NotenRechner {
|
||||
return $this->calculate($rechnungArray['value']);
|
||||
}
|
||||
|
||||
public function berechneStringComplexRun(string $schemaRaw, array $valuesArray = [], int $geraetId = 0, int $programm = 0, array $arrayRuns = []) {
|
||||
public function berechneStringComplexRun(string $schemaRaw, array $valuesArray = [], int $geraetId = -1, int $programm = 0, array $arrayRuns = []) {
|
||||
if ($schemaRaw === '') { return ['success' => false, 'value' => 'Leeres Schema']; }
|
||||
if ($geraetId === 0) { return ['success' => false, 'value' => 'Keine Geraet Id angegeben.']; }
|
||||
if ($geraetId < 0) { return ['success' => false, 'value' => 'Keine Geraet Id angegeben.']; }
|
||||
if ($programm === 0) { return ['success' => false, 'value' => 'Kein Programm angegeben.']; }
|
||||
if ($arrayRuns === []) { return ['success' => false, 'value' => 'Leeres Array Runs Noten.']; }
|
||||
|
||||
@@ -213,9 +253,7 @@ class NotenRechner {
|
||||
|
||||
foreach ($ids as $sid) {
|
||||
$geraetId = ($sid['geraetId'] === 'A') ? $currentId : $sid['geraetId'];
|
||||
$runCountN = $arrayRuns[$sid['noteId']][$geraetId][$programm] ?? $arrayRuns["default"] ?? 1;
|
||||
|
||||
//var_dump($arrayRuns[$sid['noteId']][$geraetId][$programm]);
|
||||
$runCountN = $arrayRuns[$sid['noteId']][$geraetId][$programm] ?? $arrayRuns[$sid['noteId']][$geraetId]["all"] ?? $arrayRuns["default"] ?? 1;
|
||||
|
||||
if ($runCount !== $runCountN && $runCount !== 0) { return; }
|
||||
$runCount = $runCountN;
|
||||
@@ -225,21 +263,26 @@ class NotenRechner {
|
||||
switch ($name) {
|
||||
case "AVGR":
|
||||
case "SUMR":
|
||||
$nulls = 0;
|
||||
|
||||
for ($r = 1; $r <= $runCount; $r++) {
|
||||
|
||||
$res = $this->insertValuesComplex($content, $valuesArray, $currentId, $r);
|
||||
|
||||
// FIXED: Only bail if success is false
|
||||
$res = $this->insertValuesComplex($content, $valuesArray, $currentId, $r, true);
|
||||
|
||||
if (!$res['success']) {
|
||||
return "0"; // Or handle error as needed
|
||||
return "0";
|
||||
} elseif ($res['value'] === 'default_value_skip') {
|
||||
$nulls++;
|
||||
} else {
|
||||
$parts[] = "(" . $res['value'] . ")";
|
||||
}
|
||||
$parts[] = "(" . $res['value'] . ")";
|
||||
}
|
||||
|
||||
$innerMath = implode(" + ", $parts);
|
||||
$string = ($name === "AVGR") ? "(($innerMath) / $runCount)" : "($innerMath)";
|
||||
$div_count = $runCount - $nulls;
|
||||
if ($div_count === 0 || count($parts) < 1) return "0";
|
||||
$string = ($name === "AVGR") ? "(($innerMath) / $div_count)" : "($innerMath)";
|
||||
|
||||
//var_dump($string);
|
||||
break;
|
||||
|
||||
case "MAXR":
|
||||
|
||||
Reference in New Issue
Block a user