From af2436d7cc555216526052b901583c0e75dddb24 Mon Sep 17 00:00:00 2001 From: Bruce Wells Date: Mon, 27 Aug 2018 17:47:28 -0400 Subject: Exception Messages Basically the token that is causing the exception for better diagnostics. --- src/NXP/Classes/Calculator.php | 2 +- src/NXP/Classes/TokenFactory.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src/NXP/Classes') diff --git a/src/NXP/Classes/Calculator.php b/src/NXP/Classes/Calculator.php index 41e0d9f..b584d69 100644 --- a/src/NXP/Classes/Calculator.php +++ b/src/NXP/Classes/Calculator.php @@ -40,7 +40,7 @@ class Calculator if ($token instanceof TokenVariable) { $variable = $token->getValue(); if (!array_key_exists($variable, $variables)) { - throw new UnknownVariableException(); + throw new UnknownVariableException($variable); } $value = $variables[$variable]; array_push($stack, new TokenNumber($value)); diff --git a/src/NXP/Classes/TokenFactory.php b/src/NXP/Classes/TokenFactory.php index 19ba1cf..07b9ea8 100644 --- a/src/NXP/Classes/TokenFactory.php +++ b/src/NXP/Classes/TokenFactory.php @@ -61,7 +61,7 @@ class TokenFactory $class = new \ReflectionClass($operatorClass); if (!in_array('NXP\Classes\Token\InterfaceToken', $class->getInterfaceNames())) { - throw new UnknownOperatorException; + throw new UnknownOperatorException($operatorClass); } $this->operators[] = $operatorClass; @@ -140,10 +140,10 @@ class TokenFactory if (isset($this->functions[$token])) { return new TokenFunction($this->functions[$token]); } else { - throw new UnknownFunctionException(); + throw new UnknownFunctionException($token); } } - throw new UnknownTokenException(); + throw new UnknownTokenException($token); } } -- cgit v1.2.3 From 29d32a155f9956d1041a4420c19986565f695040 Mon Sep 17 00:00:00 2001 From: Bruce Wells Date: Fri, 31 Aug 2018 12:22:58 -0400 Subject: Add ability to get functions and operators that have been registered --- src/NXP/Classes/TokenFactory.php | 22 ++++++++++++++++++++++ src/NXP/MathExecutor.php | 21 +++++++++++++++++++++ 2 files changed, 43 insertions(+) (limited to 'src/NXP/Classes') diff --git a/src/NXP/Classes/TokenFactory.php b/src/NXP/Classes/TokenFactory.php index 19ba1cf..2b1e00e 100644 --- a/src/NXP/Classes/TokenFactory.php +++ b/src/NXP/Classes/TokenFactory.php @@ -51,6 +51,18 @@ class TokenFactory $this->functions[$name] = array($places, $function); } + + /** + * get functions + * + * @return array containing callback and places indexed by + * function name + */ + public function getFunctions() + { + return $this->functions; + } + /** * Add operator * @param string $operatorClass @@ -68,6 +80,16 @@ class TokenFactory $this->operators = array_unique($this->operators); } + /** + * Get registered operators + * + * @return array of operator class names + */ + public function getOperators() + { + return $this->operators; + } + /** * Add variable * @param string $name diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php index f348355..0c3d185 100644 --- a/src/NXP/MathExecutor.php +++ b/src/NXP/MathExecutor.php @@ -156,6 +156,16 @@ class MathExecutor return $this; } + /** + * Get all registered operators to executor + * + * @return array of operator class names + */ + public function getOperators() + { + return $this->tokenFactory->getOperators(); + } + /** * Add function to executor * @@ -171,6 +181,17 @@ class MathExecutor return $this; } + /** + * Get all registered functions + * + * @return array containing callback and places indexed by + * function name + */ + public function getFunctions() + { + return $this->tokenFactory->getFunctions(); + } + /** * Execute expression * -- cgit v1.2.3