support for calculations

This commit is contained in:
Sascha Nitsch 2024-09-09 17:28:42 +02:00
parent c47142ea5c
commit f445dcb0be
2 changed files with 25 additions and 1 deletions

View file

@ -12,5 +12,7 @@
"email": "grumpydeveloper@contentnation.net"
}
],
"require": {}
"require": {
"arokettu/arithmetic-parser": "*"
}
}

View file

@ -7,6 +7,28 @@
**/
namespace ScriptedBrowser\Commands;
/**
* calculate given formula
*
* @param \ScriptedBrowser\Main $main main instance
* @param \ScriptedBrowser\Control $control control interface
* @param array<string,mixed> $options options
* @return bool always true
*/
function calc($main, $control, $options)
{
require_once 'vendor/autoload.php';
$input = $main->vars($options['parameter']);
$result = $options['result'];
try {
$calc = \Arokettu\ArithmeticParser\Calculator::evaluate($input);
$main->setVar($result, $calc);
return true;
} catch (\Exception) {
return false;
}
}
/**
* inject and execute given javascript
*