aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBruce Wells <bruce.wells@simparel.com>2018-10-30 23:16:01 +0300
committerBruce Wells <bruce.wells@simparel.com>2018-10-31 16:35:40 +0300
commitf0d4562b9ef3bbc64cb8353f0597e639420ede6b (patch)
tree86f215d0c7187843e863e7a980ae81943e7d4a5f /src
parent334dd26e3c78a36002aed34aa26aa526b11c5dfb (diff)
Division By Zero Exception support
Updated the documentation. Unit tests for strings. DivisionByZeroException support.
Diffstat (limited to 'src')
-rw-r--r--src/NXP/Classes/Token/AbstractOperator.php30
-rw-r--r--src/NXP/Classes/Token/TokenDivision.php4
-rw-r--r--src/NXP/Classes/TokenFactory.php33
-rw-r--r--src/NXP/MathExecutor.php23
4 files changed, 89 insertions, 1 deletions
diff --git a/src/NXP/Classes/Token/AbstractOperator.php b/src/NXP/Classes/Token/AbstractOperator.php
index 6cdfe99..87634d5 100644
--- a/src/NXP/Classes/Token/AbstractOperator.php
+++ b/src/NXP/Classes/Token/AbstractOperator.php
@@ -17,4 +17,34 @@ abstract class AbstractOperator implements InterfaceToken, InterfaceOperator
{
const RIGHT_ASSOC = 'RIGHT';
const LEFT_ASSOC = 'LEFT';
+
+ /**
+ * Divide by zero reporting
+ *
+ * @var bool
+ */
+ private $divideByZeroReporting = false;
+
+ /**
+ * Set division by zero exception reporting
+ *
+ * @param bool $exception default true
+ *
+ * @return $this
+ */
+ public function setDivisionByZeroException($exception = true)
+ {
+ $this->divideByZeroReporting = $exception;
+ return $this;
+ }
+
+ /**
+ * Get division by zero exception status
+ *
+ * @return bool
+ */
+ public function getDivisionByZeroException()
+ {
+ return $this->divideByZeroReporting;
+ }
}
diff --git a/src/NXP/Classes/Token/TokenDivision.php b/src/NXP/Classes/Token/TokenDivision.php
index 5bbc35e..9166d34 100644
--- a/src/NXP/Classes/Token/TokenDivision.php
+++ b/src/NXP/Classes/Token/TokenDivision.php
@@ -59,6 +59,10 @@ class TokenDivision extends AbstractOperator
throw new IncorrectExpressionException("Division requires two operators");
}
+ if ($this->getDivisionByZeroException() && $op2->getValue() == 0) {
+ throw new DivisionByZeroException();
+ }
+
$result = $op2->getValue() != 0 ? $op1->getValue() / $op2->getValue() : 0;
return new TokenNumber($result);
diff --git a/src/NXP/Classes/TokenFactory.php b/src/NXP/Classes/TokenFactory.php
index d70dd55..6956f31 100644
--- a/src/NXP/Classes/TokenFactory.php
+++ b/src/NXP/Classes/TokenFactory.php
@@ -35,6 +35,13 @@ class TokenFactory
protected $operators = [];
/**
+ * Divide by zero reporting
+ *
+ * @var bool
+ */
+ protected $divideByZeroReporting = false;
+
+ /**
* Available functions
*
* @var array
@@ -91,6 +98,29 @@ class TokenFactory
}
/**
+ * Set division by zero exception reporting
+ *
+ * @param bool $exception default true
+ *
+ * @return TokenFactory
+ */
+ public function setDivisionByZeroException($exception = true)
+ {
+ $this->divideByZeroReporting = $exception;
+ return $this;
+ }
+
+ /**
+ * Get division by zero exception status
+ *
+ * @return bool
+ */
+ public function getDivisionByZeroException()
+ {
+ return $this->divideByZeroReporting;
+ }
+
+ /**
* @return string
*/
public function getTokenParserRegex()
@@ -143,7 +173,8 @@ class TokenFactory
foreach ($this->operators as $operator) {
$regex = sprintf('/%s/i', $operator::getRegex());
if (preg_match($regex, $token)) {
- return new $operator;
+ $op = new $operator;
+ return $op->setDivisionByZeroException($this->getDivisionByZeroException());
}
}
diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php
index d62d07a..fbf6ab1 100644
--- a/src/NXP/MathExecutor.php
+++ b/src/NXP/MathExecutor.php
@@ -219,6 +219,29 @@ class MathExecutor
}
/**
+ * Set division by zero exception reporting
+ *
+ * @param bool $exception default true
+ *
+ * @return MathExecutor
+ */
+ public function setDivisionByZeroException($exception = true)
+ {
+ $this->tokenFactory->setDivisionByZeroException($exception);
+ return $this;
+ }
+
+ /**
+ * Get division by zero exception status
+ *
+ * @return bool
+ */
+ public function getDivisionByZeroException()
+ {
+ return $this->tokenFactory->getDivisionByZeroException();
+ }
+
+ /**
* Execute expression
*
* @param $expression