Skip to content

Commit

Permalink
Replaceable operators (#38)
Browse files Browse the repository at this point in the history
* Updated from NeonXP/MathExecutor

* Fixed function in () block issue

* Fixing typos in and clarifying documentation.

* Syncing from origin (#3)

* Documentation fixes (#34)

Fixing typos in and clarifying documentation.

* MathExecutor allow override default operators, functions and vars (#36)

* Allow for operators to be replaced based on regex expression
  • Loading branch information
phpfui authored and Alexander Kiryukhin committed Jan 10, 2019
1 parent 4c86b6f commit 1323063
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ A simple math expressions calculator
* Exceptions on divide by zero, or treat as zero
* Unary Minus (e.g. -3)
* Pi ($pi) and Euler's number ($e) support to 11 decimal places
* Easily extendable

## Install via Composer:
Stable branch
Expand Down Expand Up @@ -152,3 +153,14 @@ Expressions can contain double or single quoted strings that are evaluated the s
```php
echo $executor->execute("1 + '2.5' * '.5' + myFunction('category')");
```

## Extending MathExecutor
You can add operators, functions and variables with the public methods in MathExecutor, but if you need to do more serious modifications to base behaviours, the easiest way to extend MathExecutor is to redefine the following methods in your derived class:
* defaultOperators
* defaultFunctions
* defaultVars

This will allow you to remove functions and operators if needed, or implement different types more simply.

Also note that you can replace an existing default operator by adding a new operator with the same regular expression string. For example if you just need to redefine TokenPlus, you can just add a new operator with the same regex string, in this case '\+'.

3 changes: 1 addition & 2 deletions src/NXP/Classes/TokenFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ public function addOperator($operatorClass)
throw new UnknownOperatorException($operatorClass);
}

$this->operators[] = $operatorClass;
$this->operators = array_unique($this->operators);
$this->operators[$operatorClass::getRegex()] = $operatorClass;
}

/**
Expand Down

0 comments on commit 1323063

Please sign in to comment.