aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Wells <brucekwells@gmail.com>2019-01-10 21:35:39 +0300
committerAlexander Kiryukhin <alexander@kiryukhin.su>2019-01-10 21:35:39 +0300
commit13230631322675a6e07c4fde0f9c78626b336469 (patch)
tree0011b15adaf299ecffb84d5806065c823dadb37b
parent4c86b6fab82c50af4bd6449ec5fe39242d3ace74 (diff)
Replaceable operators (#38)v0.7
* 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
-rw-r--r--README.md12
-rw-r--r--src/NXP/Classes/TokenFactory.php3
2 files changed, 13 insertions, 2 deletions
diff --git a/README.md b/README.md
index 39490a4..6579a23 100644
--- a/README.md
+++ b/README.md
@@ -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
@@ -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 '\+'.
+
diff --git a/src/NXP/Classes/TokenFactory.php b/src/NXP/Classes/TokenFactory.php
index 4ed55af..778cb59 100644
--- a/src/NXP/Classes/TokenFactory.php
+++ b/src/NXP/Classes/TokenFactory.php
@@ -88,8 +88,7 @@ class TokenFactory
throw new UnknownOperatorException($operatorClass);
}
- $this->operators[] = $operatorClass;
- $this->operators = array_unique($this->operators);
+ $this->operators[$operatorClass::getRegex()] = $operatorClass;
}
/**