MathExecutor allow override default operators, functions and vars (#36)
This commit is contained in:
parent
e8ede3d6a5
commit
808593bacf
2 changed files with 78 additions and 32 deletions
|
@ -50,12 +50,17 @@ class TokenFactory
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add function
|
* Add function
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param callable $function
|
* @param callable $function
|
||||||
* @param int $places
|
* @param int $places
|
||||||
|
* @throws \ReflectionException
|
||||||
*/
|
*/
|
||||||
public function addFunction($name, callable $function, $places = 1)
|
public function addFunction($name, callable $function, $places = null)
|
||||||
{
|
{
|
||||||
|
if ($places === null) {
|
||||||
|
$reflector = new \ReflectionFunction($function);
|
||||||
|
$places = $reflector->getNumberOfParameters();
|
||||||
|
}
|
||||||
$this->functions[$name] = [$places, $function];
|
$this->functions[$name] = [$places, $function];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -53,35 +53,6 @@ class MathExecutor
|
||||||
$this->addDefaults();
|
$this->addDefaults();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Set default operands and functions
|
|
||||||
*/
|
|
||||||
protected function addDefaults()
|
|
||||||
{
|
|
||||||
$this->tokenFactory = new TokenFactory();
|
|
||||||
|
|
||||||
$this->tokenFactory->addOperator('NXP\Classes\Token\TokenPlus');
|
|
||||||
$this->tokenFactory->addOperator('NXP\Classes\Token\TokenMinus');
|
|
||||||
$this->tokenFactory->addOperator('NXP\Classes\Token\TokenMultiply');
|
|
||||||
$this->tokenFactory->addOperator('NXP\Classes\Token\TokenDivision');
|
|
||||||
$this->tokenFactory->addOperator('NXP\Classes\Token\TokenDegree');
|
|
||||||
|
|
||||||
$this->tokenFactory->addFunction('sin', 'sin');
|
|
||||||
$this->tokenFactory->addFunction('cos', 'cos');
|
|
||||||
$this->tokenFactory->addFunction('tn', 'tan');
|
|
||||||
$this->tokenFactory->addFunction('asin', 'asin');
|
|
||||||
$this->tokenFactory->addFunction('acos', 'acos');
|
|
||||||
$this->tokenFactory->addFunction('atn', 'atan');
|
|
||||||
$this->tokenFactory->addFunction('min', 'min', 2);
|
|
||||||
$this->tokenFactory->addFunction('max', 'max', 2);
|
|
||||||
$this->tokenFactory->addFunction('avg', function($arg1, $arg2) { return ($arg1 + $arg2) / 2; }, 2);
|
|
||||||
|
|
||||||
$this->setVars([
|
|
||||||
'pi' => 3.14159265359,
|
|
||||||
'e' => 2.71828182846
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all vars
|
* Get all vars
|
||||||
*
|
*
|
||||||
|
@ -262,4 +233,74 @@ class MathExecutor
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set default operands and functions
|
||||||
|
*/
|
||||||
|
protected function addDefaults()
|
||||||
|
{
|
||||||
|
$this->tokenFactory = new TokenFactory();
|
||||||
|
|
||||||
|
foreach ($this->defaultOperators() as $operatorClass) {
|
||||||
|
$this->tokenFactory->addOperator($operatorClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($this->defaultFunctions() as $name => $callable) {
|
||||||
|
$this->tokenFactory->addFunction($name, $callable);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->setVars($this->defaultVars());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function defaultOperators()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'NXP\Classes\Token\TokenPlus',
|
||||||
|
'NXP\Classes\Token\TokenMinus',
|
||||||
|
'NXP\Classes\Token\TokenMultiply',
|
||||||
|
'NXP\Classes\Token\TokenDivision',
|
||||||
|
'NXP\Classes\Token\TokenDegree',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function defaultFunctions()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'sin' => function ($arg) {
|
||||||
|
return sin($arg);
|
||||||
|
},
|
||||||
|
'cos' => function ($arg) {
|
||||||
|
return cos($arg);
|
||||||
|
},
|
||||||
|
'tn' => function ($arg) {
|
||||||
|
return tan($arg);
|
||||||
|
},
|
||||||
|
'asin' => function ($arg) {
|
||||||
|
return asin($arg);
|
||||||
|
},
|
||||||
|
'acos' => function ($arg) {
|
||||||
|
return acos($arg);
|
||||||
|
},
|
||||||
|
'atn' => function ($arg) {
|
||||||
|
return atan($arg);
|
||||||
|
},
|
||||||
|
'min' => function ($arg1, $arg2) {
|
||||||
|
return min($arg1, $arg2);
|
||||||
|
},
|
||||||
|
'max' => function ($arg1, $arg2) {
|
||||||
|
return max($arg1, $arg2);
|
||||||
|
},
|
||||||
|
'avg' => function ($arg1, $arg2) {
|
||||||
|
return ($arg1 + $arg2) / 2;
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function defaultVars()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'pi' => 3.14159265359,
|
||||||
|
'e' => 2.71828182846
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue