Copied!

Class MathExecutor

  • package NXP
Cloneable Instantiable
Methods
public __clone ()
public __construct ()
 

Base math operators

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

Add function to executor

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

public getCache () : array
 

Get cache array with tokens

  • return array []
public getFunctions () : array
 

Get all registered functions

  • return array containing callback and places indexed by
    function name
public getOperators () : array
 

Get all registered operators to executor

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

Get a specific var

  • throws UnknownVariableException if VarNotFoundHandler is not set
  • return int | float
public getVars () : array
 

Get all vars

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

Remove a specific operator

  • return Operator [] of operator class names
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 , $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
  • return NXP\MathExecutor
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 | int | string> $variables
  • param bool $clear Clear previous variables
  • throws Exception
  • return NXP\MathExecutor
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
  • return NXP\MathExecutor
public useBCMath ( int $scale = 2 ) : self
public varExists ( string $variable ) : bool
 

Test to see if a variable exists

Properties
protected array $cache
 
  • var array []
protected array $functions
 
  • var array
protected $onVarNotFound
 
  • var callable | null
protected $onVarValidation
 
  • var callable | null
protected array $operators
 
  • var Operator []
protected array $variables
 

Available variables

  • var array | 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
protected defaultVars () : array
 

Returns the default variables names as key/value pairs

  • return array
protected defaultVarValidation ( string $variable , $value ) : void
 

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

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