Copied!

Class MathExecutor

  • package NXP
CloneableInstantiable
Methods
public __clone()
public __construct()
 

Base math operators

public addFunction(string $name, ?callable $function = NULL) : self
 

Add function to executor

  • param string $name Name of function
  • param callable|null $function Function
  • throws \ReflectionException
  • throws \Exception\IncorrectNumberOfFunctionParametersException
public addOperator(NXP\Classes\Operator $operator) : self
 

Add operator to executor

public clearCache() : self
 

Clear token's cache

public execute(string $expression, bool $cache = true)
 

Execute expression

  • throws \Exception\IncorrectExpressionException
  • throws \Exception\UnknownOperatorException
  • throws \UnknownVariableException
  • throws \Exception\IncorrectBracketsException
  • return int|float|string|null
public getCache() : array
 

Get cache array with tokens

  • return array<string,\Token[]>
public getFunctions() : array
 

Get all registered functions

  • return array<string,\CustomFunction> containing callback and places indexed by function name function name
public getOperators() : array
 

Get all registered operators to executor

  • return \Operator[] of operator class names
public getVar(string $variable) : ?mixed
 

Get a specific var

  • throws \UnknownVariableException if VarNotFoundHandler is not set
public getVars() : array
 

Get all vars

  • return array<string,float|string>
public removeOperator(string $operator) : self
 

Remove a specific operator

public removeVar(string $variable) : self
 

Remove variable from executor

public removeVars() : self
 

Remove all variables and the variable not found handler

public setDivisionByZeroIsZero() : self
 

Set division by zero returns zero instead of throwing DivisionByZeroException

public setVar(string $variable, ?mixed $value) : self
 

Add variable to executor. To set a custom validator use setVarValidationHandler.

  • throws \MathExecutorException if the value is invalid based on the default or custom validator
public setVarNotFoundHandler(callable $handler) : self
 

Define a method that will be invoked when a variable is not found.

The first parameter will be the variable name, and the returned value will be used as the variable value.

public setVars(array $variables, bool $clear = true) : self
 

Add variables to executor

  • param array<string,float|int|string> $variables
  • param bool $clear Clear previous variables
  • throws \Exception
public setVarValidationHandler(?callable $handler) : self
 

Define a validation method that will be invoked when a variable is set using setVar.

The first parameter will be the variable name, and the second will be the variable value. Set to null to disable validation.

  • param ?callable $handler throws a MathExecutorException in case of an invalid variable
public useBCMath(int $scale = 2) : self
public varExists(string $variable) : bool
 

Test to see if a variable exists

Properties
protected array $cache = []
 
  • var array<string,\Token[]>
protected array $functions = []
 
  • var array<string,\CustomFunction>
protected $onVarNotFound = NULL
 
  • var callable|null
protected $onVarValidation = NULL
 
  • var callable|null
protected array $operators = []
 
  • var \Operator[]
protected array $variables = []
 

Available variables

  • var array<string,float|string>
Methods
protected addDefaults() : self
 

Set default operands and functions

  • throws \ReflectionException
protected defaultFunctions() : array
 

Gets the default functions as an array. Key is function name and value is the function as a closure.

  • return callable[]
protected defaultOperators() : array
 

Get the default operators

  • return array<string,array{: callable, : int, : bool}>
protected defaultVars() : array
 

Returns the default variables names as key/value pairs

  • return array<string,float>
protected defaultVarValidation(string $variable, ?mixed $value) : void
 

Default variable validation, ensures that the value is a scalar or array.

  • throws \MathExecutorException if the value is not a scalar
© 2024 Bruce Wells
Search Namespaces \ Classes
Configuration