aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMatthijs Meulenbrug <mrm@users.noreply.github.com>2022-04-25 17:50:51 +0300
committerGitHub <noreply@github.com>2022-04-25 17:50:51 +0300
commitef82911187771e56c8987af5bf03d331cc533fde (patch)
treec1f0454c302b23bbe17f497c8ff75966686d1d59 /src
parenta0ff7a79af8c3bc5e9c5decee24cbefb5d80e93b (diff)
Add a custom variable validator + protected props (#98)
Diffstat (limited to 'src')
-rw-r--r--src/NXP/MathExecutor.php56
1 files changed, 46 insertions, 10 deletions
diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php
index a113275..ab550fa 100644
--- a/src/NXP/MathExecutor.php
+++ b/src/NXP/MathExecutor.php
@@ -31,27 +31,32 @@ class MathExecutor
*
* @var array<string, float|string>
*/
- private $variables = [];
+ protected $variables = [];
/**
* @var callable|null
*/
- private $onVarNotFound = null;
+ protected $onVarNotFound = null;
+
+ /**
+ * @var callable|null
+ */
+ protected $onVarValidation = null;
/**
* @var Operator[]
*/
- private $operators = [];
+ protected $operators = [];
/**
* @var array<string, CustomFunction>
*/
- private $functions = [];
+ protected $functions = [];
/**
* @var array<string, Token[]>
*/
- private $cache = [];
+ protected $cache = [];
/**
* Base math operators
@@ -74,6 +79,8 @@ class MathExecutor
foreach ($this->defaultFunctions() as $name => $callable) {
$this->addFunction($name, $callable);
}
+
+ $this->onVarValidation = [$this, 'defaultVarValidation'];
$this->variables = $this->defaultVars();
}
@@ -507,17 +514,17 @@ class MathExecutor
}
/**
- * Add variable to executor
+ * Add variable to executor. To set a custom validator use setVarValidationHandler.
*
* @param string $variable
- * @param int|float $value
+ * @param $value
* @return MathExecutor
+ * @throws MathExecutorException if the value is invalid based on the default or custom validator
*/
public function setVar(string $variable, $value): self
{
- if (!is_scalar($value) && $value !== null) {
- $type = gettype($value);
- throw new MathExecutorException("Variable ({$variable}) type ({$type}) is not scalar");
+ if ($this->onVarValidation) {
+ call_user_func($this->onVarValidation, $variable, $value);
}
$this->variables[$variable] = $value;
@@ -525,6 +532,20 @@ class MathExecutor
}
/**
+ * Default variable validation, ensures that the value is a scalar.
+ * @param string $variable
+ * @param $value
+ * @throws MathExecutorException if the value is not a scalar
+ */
+ protected function defaultVarValidation(string $variable, $value): void
+ {
+ if (!is_scalar($value) && $value !== null) {
+ $type = gettype($value);
+ throw new MathExecutorException("Variable ({$variable}) type ({$type}) is not scalar");
+ }
+ }
+
+ /**
* Test to see if a variable exists
*
* @param string $variable
@@ -568,6 +589,21 @@ class MathExecutor
}
/**
+ * 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 MathExecutor
+ */
+ public function setVarValidationHandler(?callable $handler): self
+ {
+ $this->onVarValidation = $handler;
+ return $this;
+ }
+
+ /**
* Remove variable from executor
*
* @param string $variable