aboutsummaryrefslogtreecommitdiff
path: root/src/NXP
diff options
context:
space:
mode:
authorBruce Wells <brucekwells@gmail.com>2022-03-21 19:52:25 +0300
committerGitHub <noreply@github.com>2022-03-21 19:52:25 +0300
commita0ff7a79af8c3bc5e9c5decee24cbefb5d80e93b (patch)
tree9432edb4d427293952ed99f48968b8ad32f6633a /src/NXP
parent6ebe4849ff7448d5903914d2f8980af5c9fd8d34 (diff)
Adding varExists method and support for undefined var handler in getVar (#96)V2.1.112.0.4
* Added varExists method * getVar now respects VarNotFoundHandler setting * Use local version of PHP-CS-Fixer Instead of hard coded version from github actions * Fixing actions * Fixing actions * Dropping testing for 7.3, as it is no longer supported
Diffstat (limited to 'src/NXP')
-rw-r--r--src/NXP/Classes/CustomFunction.php3
-rw-r--r--src/NXP/Classes/Operator.php3
-rw-r--r--src/NXP/Classes/Token.php1
-rw-r--r--src/NXP/Classes/Tokenizer.php20
-rw-r--r--src/NXP/MathExecutor.php45
5 files changed, 41 insertions, 31 deletions
diff --git a/src/NXP/Classes/CustomFunction.php b/src/NXP/Classes/CustomFunction.php
index 6e4fdc5..225495f 100644
--- a/src/NXP/Classes/CustomFunction.php
+++ b/src/NXP/Classes/CustomFunction.php
@@ -1,6 +1,5 @@
<?php
-
namespace NXP\Classes;
use NXP\Exception\IncorrectNumberOfFunctionParametersException;
@@ -49,7 +48,7 @@ class CustomFunction
*
* @throws IncorrectNumberOfFunctionParametersException
*/
- public function execute(array &$stack) : Token
+ public function execute(array &$stack): Token
{
if (count($stack) < $this->places) {
throw new IncorrectNumberOfFunctionParametersException($this->name);
diff --git a/src/NXP/Classes/Operator.php b/src/NXP/Classes/Operator.php
index d9a70d7..1b1acc4 100644
--- a/src/NXP/Classes/Operator.php
+++ b/src/NXP/Classes/Operator.php
@@ -1,6 +1,5 @@
<?php
-
namespace NXP\Classes;
use NXP\Exception\IncorrectExpressionException;
@@ -55,7 +54,7 @@ class Operator
*
* @throws IncorrectExpressionException
*/
- public function execute(array &$stack) : Token
+ public function execute(array &$stack): Token
{
if (count($stack) < $this->places) {
throw new IncorrectExpressionException();
diff --git a/src/NXP/Classes/Token.php b/src/NXP/Classes/Token.php
index e12315e..924771e 100644
--- a/src/NXP/Classes/Token.php
+++ b/src/NXP/Classes/Token.php
@@ -1,6 +1,5 @@
<?php
-
namespace NXP\Classes;
class Token
diff --git a/src/NXP/Classes/Tokenizer.php b/src/NXP/Classes/Tokenizer.php
index 77014b8..087a78d 100644
--- a/src/NXP/Classes/Tokenizer.php
+++ b/src/NXP/Classes/Tokenizer.php
@@ -66,7 +66,7 @@ class Tokenizer
$this->operators = $operators;
}
- public function tokenize() : self
+ public function tokenize(): self
{
foreach (str_split($this->input, 1) as $ch) {
switch (true) {
@@ -184,17 +184,17 @@ class Tokenizer
return $this;
}
- private function isNumber(string $ch) : bool
+ private function isNumber(string $ch): bool
{
return $ch >= '0' && $ch <= '9';
}
- private function isAlpha(string $ch) : bool
+ private function isAlpha(string $ch): bool
{
return $ch >= 'a' && $ch <= 'z' || $ch >= 'A' && $ch <= 'Z' || $ch == '_';
}
- private function emptyNumberBufferAsLiteral() : void
+ private function emptyNumberBufferAsLiteral(): void
{
if (strlen($this->numberBuffer)) {
$this->tokens[] = new Token(Token::Literal, $this->numberBuffer);
@@ -202,22 +202,22 @@ class Tokenizer
}
}
- private function isDot(string $ch) : bool
+ private function isDot(string $ch): bool
{
return $ch == '.';
}
- private function isLP(string $ch) : bool
+ private function isLP(string $ch): bool
{
return $ch == '(';
}
- private function isRP(string $ch) : bool
+ private function isRP(string $ch): bool
{
return $ch == ')';
}
- private function emptyStrBufferAsVariable() : void
+ private function emptyStrBufferAsVariable(): void
{
if ($this->stringBuffer != '') {
$this->tokens[] = new Token(Token::Variable, $this->stringBuffer);
@@ -225,7 +225,7 @@ class Tokenizer
}
}
- private function isComma(string $ch) : bool
+ private function isComma(string $ch): bool
{
return $ch == ',';
}
@@ -235,7 +235,7 @@ class Tokenizer
* @throws IncorrectBracketsException
* @throws UnknownOperatorException
*/
- public function buildReversePolishNotation() : array
+ public function buildReversePolishNotation(): array
{
$tokens = [];
/** @var SplStack<Token> $stack */
diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php
index 5584d1a..a113275 100644
--- a/src/NXP/MathExecutor.php
+++ b/src/NXP/MathExecutor.php
@@ -65,7 +65,7 @@ class MathExecutor
* Set default operands and functions
* @throws ReflectionException
*/
- protected function addDefaults() : void
+ protected function addDefaults(): void
{
foreach ($this->defaultOperators() as $name => $operator) {
[$callable, $priority, $isRightAssoc] = $operator;
@@ -82,7 +82,7 @@ class MathExecutor
*
* @return array<string, array{callable, int, bool}>
*/
- protected function defaultOperators() : array
+ protected function defaultOperators(): array
{
return [
'+' => [
@@ -210,7 +210,7 @@ class MathExecutor
* @param Operator $operator
* @return MathExecutor
*/
- public function addOperator(Operator $operator) : self
+ public function addOperator(Operator $operator): self
{
$this->operators[$operator->operator] = $operator;
return $this;
@@ -222,7 +222,7 @@ class MathExecutor
*
* @return array<callable>
*/
- protected function defaultFunctions() : array
+ protected function defaultFunctions(): array
{
return [
'abs' => function ($arg) {
@@ -459,7 +459,7 @@ class MathExecutor
* @return MathExecutor
* @throws ReflectionException
*/
- public function addFunction(string $name, ?callable $function = null, ?int $places = null) : self
+ public function addFunction(string $name, ?callable $function = null, ?int $places = null): self
{
$this->functions[$name] = new CustomFunction($name, $function, $places);
return $this;
@@ -470,7 +470,7 @@ class MathExecutor
*
* @return array<string, float>
*/
- protected function defaultVars() : array
+ protected function defaultVars(): array
{
return [
'pi' => 3.14159265359,
@@ -483,7 +483,7 @@ class MathExecutor
*
* @return array<string, float|string>
*/
- public function getVars() : array
+ public function getVars(): array
{
return $this->variables;
}
@@ -493,11 +493,14 @@ class MathExecutor
*
* @param string $variable
* @return integer|float
- * @throws UnknownVariableException
+ * @throws UnknownVariableException if VarNotFoundHandler is not set
*/
public function getVar(string $variable)
{
if (!array_key_exists($variable, $this->variables)) {
+ if ($this->onVarNotFound) {
+ return call_user_func($this->onVarNotFound, $variable);
+ }
throw new UnknownVariableException("Variable ({$variable}) not set");
}
return $this->variables[$variable];
@@ -510,7 +513,7 @@ class MathExecutor
* @param int|float $value
* @return MathExecutor
*/
- public function setVar(string $variable, $value) : self
+ public function setVar(string $variable, $value): self
{
if (!is_scalar($value) && $value !== null) {
$type = gettype($value);
@@ -522,6 +525,16 @@ class MathExecutor
}
/**
+ * Test to see if a variable exists
+ *
+ * @param string $variable
+ */
+ public function varExists(string $variable): bool
+ {
+ return array_key_exists($variable, $this->variables);
+ }
+
+ /**
* Add variables to executor
*
* @param array<string, float|int|string> $variables
@@ -529,7 +542,7 @@ class MathExecutor
* @return MathExecutor
* @throws \Exception
*/
- public function setVars(array $variables, bool $clear = true) : self
+ public function setVars(array $variables, bool $clear = true): self
{
if ($clear) {
$this->removeVars();
@@ -560,7 +573,7 @@ class MathExecutor
* @param string $variable
* @return MathExecutor
*/
- public function removeVar(string $variable) : self
+ public function removeVar(string $variable): self
{
unset($this->variables[$variable]);
return $this;
@@ -570,7 +583,7 @@ class MathExecutor
* Remove all variables and the variable not found handler
* @return MathExecutor
*/
- public function removeVars() : self
+ public function removeVars(): self
{
$this->variables = [];
$this->onVarNotFound = null;
@@ -593,7 +606,7 @@ class MathExecutor
* @return array<string, CustomFunction> containing callback and places indexed by
* function name
*/
- public function getFunctions() : array
+ public function getFunctions(): array
{
return $this->functions;
}
@@ -603,7 +616,7 @@ class MathExecutor
*
* @return MathExecutor
*/
- public function setDivisionByZeroIsZero() : self
+ public function setDivisionByZeroIsZero(): self
{
$this->addOperator(new Operator("/", false, 180, function ($a, $b) {
if ($b == 0) {
@@ -618,7 +631,7 @@ class MathExecutor
* Get cache array with tokens
* @return array<string, Token[]>
*/
- public function getCache() : array
+ public function getCache(): array
{
return $this->cache;
}
@@ -626,7 +639,7 @@ class MathExecutor
/**
* Clear token's cache
*/
- public function clearCache() : void
+ public function clearCache(): void
{
$this->cache = [];
}