First version, for githup; UNSTABLE, DO NOT USE!
This commit is contained in:
175
scripts/string-calculator/string-calculator-functions.php
Normal file
175
scripts/string-calculator/string-calculator-functions.php
Normal file
@@ -0,0 +1,175 @@
|
||||
<?php
|
||||
|
||||
if (!isset($baseDir)) {
|
||||
$baseDir = $_SERVER['DOCUMENT_ROOT'];
|
||||
}
|
||||
|
||||
//ini_set('display_errors', 1);
|
||||
//ini_set('display_startup_errors', 1);
|
||||
|
||||
require $baseDir . '/../composer/vendor/autoload.php';
|
||||
|
||||
use ChrisKonnertz\StringCalc\StringCalc;
|
||||
|
||||
class NotenRechner {
|
||||
|
||||
public function __construct(
|
||||
private StringCalc $rechner = new StringCalc()
|
||||
) {}
|
||||
|
||||
/*
|
||||
Es wird ein Array mit allen Notentypen pro Gerät, Jahr und Person erwartet, so dass noten_bezeichnung_id als Key verwendet werden kann:
|
||||
|
||||
$valuesArray = [
|
||||
noten_bezeichnung_id => value,
|
||||
…
|
||||
];
|
||||
*/
|
||||
|
||||
private function santinzeString(string $string) : string
|
||||
{
|
||||
$replacePattern = '/[^a-zA-Z0-9(){}+*\/.\-\[\]]/';
|
||||
return preg_replace($replacePattern, "", $string);
|
||||
}
|
||||
|
||||
public function getBenoetigteIds(string $schemaRaw)
|
||||
{
|
||||
$schema = $this->santinzeString($schemaRaw);
|
||||
|
||||
preg_match_all('/\{(\d+)\}/', $schema, $matches);
|
||||
return $matches[1];
|
||||
|
||||
}
|
||||
|
||||
public function getBenoetigteIdsComplex(string $schemaRaw)
|
||||
{
|
||||
$schema = $this->santinzeString($schemaRaw);
|
||||
$indexedMatches = [];
|
||||
|
||||
// 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
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $indexedMatches;
|
||||
|
||||
}
|
||||
|
||||
|
||||
private function insertValues(string $schemaRaw, array $valuesArray = [])
|
||||
{
|
||||
$schema = $this->santinzeString($schemaRaw);
|
||||
|
||||
$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'];
|
||||
}
|
||||
}
|
||||
|
||||
private function insertValuesComplex(string $schemaRaw, array $valuesArray, int $currentId)
|
||||
{
|
||||
$schema = $this->santinzeString($schemaRaw);
|
||||
$idsNeededArray = $this->getBenoetigteIdsComplex($schemaRaw);
|
||||
|
||||
// 1. Validation Loop
|
||||
foreach ($idsNeededArray as $sina) {
|
||||
$noteId = $sina['noteId'];
|
||||
$geraetIdSearch = ($sina['geraetId'] === 'A') ? $currentId : $sina['geraetId'];
|
||||
$runNumber = $sina['run'] ?? 1;
|
||||
|
||||
if (!isset($valuesArray[$geraetIdSearch][$noteId][$runNumber])) {
|
||||
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) {
|
||||
$noteId = $m[1];
|
||||
|
||||
// Get value inside brackets, default to 'S' if none exists
|
||||
$rawGeraet = $m[2] ?? 'S';
|
||||
$geraetId = ($rawGeraet === 'S' || $rawGeraet === '') ? $currentId : $rawGeraet;
|
||||
|
||||
$runNumber = isset($m[3]) ? (int)$m[3] : 1;
|
||||
|
||||
// Return value from [Device][Note][Run]
|
||||
return $valuesArray[$geraetId][$noteId][$runNumber] ?? 0;
|
||||
|
||||
}, $schema);
|
||||
|
||||
return ['success' => true, 'value' => $returnStr];
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return ['success' => false, 'value' => 'Problem beim Einsetzen der Werte'];
|
||||
}
|
||||
}
|
||||
|
||||
private function calculate(string $rechnungRaw) {
|
||||
global $rechner;
|
||||
|
||||
$rechnung = $this->santinzeString($rechnungRaw);
|
||||
|
||||
try {
|
||||
$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'];
|
||||
} catch (\Exception $e) {
|
||||
return ['success' => false, 'value' => 'Problem bei der Berechnung'];
|
||||
}
|
||||
}
|
||||
|
||||
public function berechneString(string $schemaRaw, array $valuesArray = []) {
|
||||
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) {
|
||||
if ($schemaRaw === '') { return ['success' => false, 'value' => 'Leeres Schema']; }
|
||||
if ($geraetId === 0) { return ['success' => false, 'value' => 'Keine Geraet Id angegeben.']; }
|
||||
|
||||
$rechnungArray = $this->insertValuesComplex($schemaRaw, $valuesArray, $geraetId);
|
||||
|
||||
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']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user