diff options
author | Bruce Wells <phpfui@users.noreply.github.com> | 2018-10-31 16:51:19 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-31 16:51:19 +0300 |
commit | 07d02e1fb0aeb9fca7174a065f61a5fa2643b12f (patch) | |
tree | 86f215d0c7187843e863e7a980ae81943e7d4a5f /src/NXP/Classes/Token | |
parent | 43f0ff3f28d198fbb4e36346fc5f36fa91cf3e18 (diff) | |
parent | f0d4562b9ef3bbc64cb8353f0597e639420ede6b (diff) |
Merge branch 'master' into master
Diffstat (limited to 'src/NXP/Classes/Token')
-rw-r--r-- | src/NXP/Classes/Token/AbstractOperator.php | 30 | ||||
-rw-r--r-- | src/NXP/Classes/Token/TokenDivision.php | 4 |
2 files changed, 34 insertions, 0 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); |