Commit inicial - WordPress Análisis de Precios Unitarios

- WordPress core y plugins
- Tema Twenty Twenty-Four configurado
- Plugin allow-unfiltered-html.php simplificado
- .gitignore configurado para excluir wp-config.php y uploads

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
root
2025-11-03 21:04:30 -06:00
commit a22573bf0b
24068 changed files with 4993111 additions and 0 deletions

View File

@@ -0,0 +1,85 @@
<?php
/**
* This file is part of the MathExecutor package
*
* (c) Alexander Kiryukhin
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code
*/
namespace NXP\Classes;
use NXP\Exception\IncorrectExpressionException;
use NXP\Exception\UnknownFunctionException;
use NXP\Exception\UnknownOperatorException;
use NXP\Exception\UnknownVariableException;
/**
* @author Alexander Kiryukhin <a.kiryukhin@mail.ru>
*/
class Calculator
{
/**
* @todo PHP8: Use constructor property promotion -> public function __construct(private array $functions, private array $operators)
*
* @param array<string, CustomFunction> $functions
* @param array<Operator> $operators
*/
public function __construct(private array $functions, private array $operators)
{
}
/**
* Calculate array of tokens in reverse polish notation
*
* @param Token[] $tokens
* @param array<string, float|string> $variables
*
* @throws UnknownVariableException
* @throws IncorrectExpressionException
* @return int|float|string|null
*/
public function calculate(array $tokens, array $variables, ?callable $onVarNotFound = null)
{
/** @var Token[] $stack */
$stack = [];
foreach ($tokens as $token) {
if (Token::Literal === $token->type || Token::String === $token->type) {
$stack[] = $token;
} elseif (Token::Variable === $token->type) {
$variable = $token->value;
$value = null;
if (\array_key_exists($variable, $variables)) {
$value = $variables[$variable];
} elseif ($onVarNotFound) {
$value = \call_user_func($onVarNotFound, $variable);
} else {
throw new UnknownVariableException($variable);
}
$stack[] = new Token(Token::Literal, $value, $variable);
} elseif (Token::Function === $token->type) {
if (! \array_key_exists($token->value, $this->functions)) {
throw new UnknownFunctionException($token->value);
}
$stack[] = $this->functions[$token->value]->execute($stack, $token->paramCount);
} elseif (Token::Operator === $token->type) {
if (! \array_key_exists($token->value, $this->operators)) {
throw new UnknownOperatorException($token->value);
}
$stack[] = $this->operators[$token->value]->execute($stack);
}
}
$result = \array_pop($stack);
if (null === $result || ! empty($stack)) {
throw new IncorrectExpressionException('Stack must be empty');
}
return $result->value;
}
}

View File

@@ -0,0 +1,63 @@
<?php
namespace NXP\Classes;
use NXP\Exception\IncorrectNumberOfFunctionParametersException;
use ReflectionException;
use ReflectionFunction;
class CustomFunction
{
/**
* @var callable $function
*/
public $function;
private bool $isVariadic;
private int $totalParamCount;
private int $requiredParamCount;
/**
* CustomFunction constructor.
*
* @throws ReflectionException
*/
public function __construct(public string $name, callable $function)
{
$this->function = $function;
$reflection = (new ReflectionFunction($function));
$this->isVariadic = $reflection->isVariadic();
$this->totalParamCount = $reflection->getNumberOfParameters();
$this->requiredParamCount = $reflection->getNumberOfRequiredParameters();
}
/**
* @param array<Token> $stack
*
* @throws IncorrectNumberOfFunctionParametersException
*/
public function execute(array &$stack, int $paramCountInStack) : Token
{
if ($paramCountInStack < $this->requiredParamCount) {
throw new IncorrectNumberOfFunctionParametersException($this->name);
}
if ($paramCountInStack > $this->totalParamCount && ! $this->isVariadic) {
throw new IncorrectNumberOfFunctionParametersException($this->name);
}
$args = [];
if ($paramCountInStack > 0) {
for ($i = 0; $i < $paramCountInStack; $i++) {
\array_unshift($args, \array_pop($stack)->value);
}
}
$result = \call_user_func_array($this->function, $args);
return new Token(Token::Literal, $result);
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace NXP\Classes;
use NXP\Exception\IncorrectExpressionException;
use ReflectionFunction;
class Operator
{
/**
* @var callable(\SplStack)
*/
public $function;
public int $places = 0;
/**
* Operator constructor.
*/
public function __construct(public string $operator, public bool $isRightAssoc, public int $priority, callable $function)
{
$this->function = $function;
$reflection = new ReflectionFunction($function);
$this->places = $reflection->getNumberOfParameters();
}
/**
* @param array<Token> $stack
*
* @throws IncorrectExpressionException
*/
public function execute(array &$stack) : Token
{
if (\count($stack) < $this->places) {
throw new IncorrectExpressionException();
}
$args = [];
for ($i = 0; $i < $this->places; $i++) {
\array_unshift($args, \array_pop($stack)->value);
}
$result = \call_user_func_array($this->function, $args);
return new Token(Token::Literal, $result);
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace NXP\Classes;
class Token
{
public const Literal = 'literal';
public const Variable = 'variable';
public const Operator = 'operator';
public const LeftParenthesis = 'LP';
public const RightParenthesis = 'RP';
public const Function = 'function';
public const ParamSeparator = 'separator';
public const String = 'string';
public const Space = 'space';
public ?int $paramCount = null;//to store function parameter count in stack
/**
* Token constructor.
*
*/
public function __construct(public string $type, public mixed $value, public ?string $name = null)
{
}
}

View File

@@ -0,0 +1,411 @@
<?php
/**
* This file is part of the MathExecutor package
*
* (c) Alexander Kiryukhin
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code
*/
namespace NXP\Classes;
use NXP\Exception\IncorrectBracketsException;
use NXP\Exception\UnknownOperatorException;
use RuntimeException;
use SplStack;
/**
* @author Alexander Kiryukhin <a.kiryukhin@mail.ru>
*/
class Tokenizer
{
/** @var array<Token> */
public array $tokens = [];
private string $numberBuffer = '';
private string $stringBuffer = '';
private bool $allowNegative = true;
private bool $inSingleQuotedString = false;
private bool $inDoubleQuotedString = false;
/**
* Tokenizer constructor.
* @param Operator[] $operators
*/
public function __construct(private string $input, private array $operators)
{
}
public function tokenize() : self
{
$isLastCharEscape = false;
foreach (\str_split($this->input) as $ch) {
switch (true) {
case $this->inSingleQuotedString:
if ('\\' === $ch) {
if ($isLastCharEscape) {
$this->stringBuffer .= '\\';
$isLastCharEscape = false;
} else {
$isLastCharEscape = true;
}
continue 2;
} elseif ("'" === $ch) {
if ($isLastCharEscape) {
$this->stringBuffer .= "'";
$isLastCharEscape = false;
} else {
$this->tokens[] = new Token(Token::String, $this->stringBuffer);
$this->inSingleQuotedString = false;
$this->stringBuffer = '';
}
continue 2;
}
if ($isLastCharEscape) {
$this->stringBuffer .= '\\';
$isLastCharEscape = false;
}
$this->stringBuffer .= $ch;
continue 2;
case $this->inDoubleQuotedString:
if ('\\' === $ch) {
if ($isLastCharEscape) {
$this->stringBuffer .= '\\';
$isLastCharEscape = false;
} else {
$isLastCharEscape = true;
}
continue 2;
} elseif ('"' === $ch) {
if ($isLastCharEscape) {
$this->stringBuffer .= '"';
$isLastCharEscape = false;
} else {
$this->tokens[] = new Token(Token::String, $this->stringBuffer);
$this->inDoubleQuotedString = false;
$this->stringBuffer = '';
}
continue 2;
}
if ($isLastCharEscape) {
$this->stringBuffer .= '\\';
$isLastCharEscape = false;
}
$this->stringBuffer .= $ch;
continue 2;
case '[' === $ch:
$this->tokens[] = new Token(Token::Function, 'array');
$this->allowNegative = true;
$this->tokens[] = new Token(Token::LeftParenthesis, '');
continue 2;
case ' ' == $ch || "\n" == $ch || "\r" == $ch || "\t" == $ch:
$this->emptyNumberBufferAsLiteral();
$this->emptyStrBufferAsVariable();
$this->tokens[] = new Token(Token::Space, '');
continue 2;
case $this->isNumber($ch):
if ('' != $this->stringBuffer) {
$this->stringBuffer .= $ch;
continue 2;
}
$this->numberBuffer .= $ch;
$this->allowNegative = false;
break;
/** @noinspection PhpMissingBreakStatementInspection */
case 'e' === \strtolower($ch):
if (\strlen($this->numberBuffer) && \str_contains($this->numberBuffer, '.')) {
$this->numberBuffer .= 'e';
$this->allowNegative = false;
break;
}
// no break
// Intentionally fall through
case $this->isAlpha($ch):
if (\strlen($this->numberBuffer)) {
$this->emptyNumberBufferAsLiteral();
$this->tokens[] = new Token(Token::Operator, '*');
}
$this->allowNegative = false;
$this->stringBuffer .= $ch;
break;
case '"' == $ch:
$this->inDoubleQuotedString = true;
continue 2;
case "'" == $ch:
$this->inSingleQuotedString = true;
continue 2;
case $this->isDot($ch):
$this->numberBuffer .= $ch;
$this->allowNegative = false;
break;
case $this->isLP($ch):
if ('' != $this->stringBuffer) {
$this->tokens[] = new Token(Token::Function, $this->stringBuffer);
$this->stringBuffer = '';
} elseif (\strlen($this->numberBuffer)) {
$this->emptyNumberBufferAsLiteral();
$this->tokens[] = new Token(Token::Operator, '*');
}
$this->allowNegative = true;
$this->tokens[] = new Token(Token::LeftParenthesis, '');
break;
case $this->isRP($ch) || ']' === $ch :
$this->emptyNumberBufferAsLiteral();
$this->emptyStrBufferAsVariable();
$this->allowNegative = false;
$this->tokens[] = new Token(Token::RightParenthesis, '');
break;
case $this->isComma($ch):
$this->emptyNumberBufferAsLiteral();
$this->emptyStrBufferAsVariable();
$this->allowNegative = true;
$this->tokens[] = new Token(Token::ParamSeparator, '');
break;
default:
// special case for unary operations
if ('-' == $ch || '+' == $ch) {
if ($this->allowNegative) {
$this->allowNegative = false;
$this->tokens[] = new Token(Token::Operator, '-' == $ch ? 'uNeg' : 'uPos');
continue 2;
}
// could be in exponent, in which case negative should be added to the numberBuffer
if ($this->numberBuffer && 'e' == $this->numberBuffer[\strlen($this->numberBuffer) - 1]) {
$this->numberBuffer .= $ch;
continue 2;
}
}
$this->emptyNumberBufferAsLiteral();
$this->emptyStrBufferAsVariable();
if ('$' != $ch) {
if (\count($this->tokens) > 0) {
if (Token::Operator === $this->tokens[\count($this->tokens) - 1]->type) {
$this->tokens[\count($this->tokens) - 1]->value .= $ch;
} else {
$this->tokens[] = new Token(Token::Operator, $ch);
}
} else {
$this->tokens[] = new Token(Token::Operator, $ch);
}
}
$this->allowNegative = true;
}
}
$this->emptyNumberBufferAsLiteral();
$this->emptyStrBufferAsVariable();
return $this;
}
/**
* @throws IncorrectBracketsException
* @throws UnknownOperatorException
* @return Token[] Array of tokens in revers polish notation
*/
public function buildReversePolishNotation() : array
{
$tokens = [];
/** @var SplStack<Token> $stack */
$stack = new SplStack();
/**
* @var SplStack<int> $paramCounter
*/
$paramCounter = new SplStack();
foreach ($this->tokens as $token) {
switch ($token->type) {
case Token::Literal:
case Token::Variable:
case Token::String:
$tokens[] = $token;
if ($paramCounter->count() > 0 && 0 === $paramCounter->top()) {
$paramCounter->push($paramCounter->pop() + 1);
}
break;
case Token::Function:
if ($paramCounter->count() > 0 && 0 === $paramCounter->top()) {
$paramCounter->push($paramCounter->pop() + 1);
}
$stack->push($token);
$paramCounter->push(0);
break;
case Token::LeftParenthesis:
$stack->push($token);
break;
case Token::ParamSeparator:
while (Token::LeftParenthesis !== $stack->top()->type) {
if (0 === $stack->count()) {
throw new IncorrectBracketsException();
}
$tokens[] = $stack->pop();
}
$paramCounter->push($paramCounter->pop() + 1);
break;
case Token::Operator:
if (! \array_key_exists($token->value, $this->operators)) {
throw new UnknownOperatorException($token->value);
}
$op1 = $this->operators[$token->value];
while ($stack->count() > 0 && Token::Operator === $stack->top()->type) {
if (! \array_key_exists($stack->top()->value, $this->operators)) {
throw new UnknownOperatorException($stack->top()->value);
}
$op2 = $this->operators[$stack->top()->value];
if ($op2->priority >= $op1->priority) {
$tokens[] = $stack->pop();
continue;
}
break;
}
$stack->push($token);
break;
case Token::RightParenthesis:
while (true) {
try {
$ctoken = $stack->pop();
if (Token::LeftParenthesis === $ctoken->type) {
break;
}
$tokens[] = $ctoken;
} catch (RuntimeException) {
throw new IncorrectBracketsException();
}
}
if ($stack->count() > 0 && Token::Function == $stack->top()->type) {
/**
* @var Token $f
*/
$f = $stack->pop();
$f->paramCount = $paramCounter->pop();
$tokens[] = $f;
}
break;
case Token::Space:
//do nothing
}
}
while (0 !== $stack->count()) {
if (Token::LeftParenthesis === $stack->top()->type || Token::RightParenthesis === $stack->top()->type) {
throw new IncorrectBracketsException();
}
if (Token::Space === $stack->top()->type) {
$stack->pop();
continue;
}
$tokens[] = $stack->pop();
}
return $tokens;
}
private function isNumber(string $ch) : bool
{
return $ch >= '0' && $ch <= '9';
}
private function isAlpha(string $ch) : bool
{
return $ch >= 'a' && $ch <= 'z' || $ch >= 'A' && $ch <= 'Z' || '_' == $ch;
}
private function emptyNumberBufferAsLiteral() : void
{
if (\strlen($this->numberBuffer)) {
$this->tokens[] = new Token(Token::Literal, $this->numberBuffer);
$this->numberBuffer = '';
}
}
private function isDot(string $ch) : bool
{
return '.' == $ch;
}
private function isLP(string $ch) : bool
{
return '(' == $ch;
}
private function isRP(string $ch) : bool
{
return ')' == $ch;
}
private function emptyStrBufferAsVariable() : void
{
if ('' != $this->stringBuffer) {
$this->tokens[] = new Token(Token::Variable, $this->stringBuffer);
$this->stringBuffer = '';
}
}
private function isComma(string $ch) : bool
{
return ',' == $ch;
}
}

View File

@@ -0,0 +1,19 @@
<?php
/**
* This file is part of the MathExecutor package
*
* (c) Alexander Kiryukhin
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code
*/
namespace NXP\Exception;
/**
* @author Vitaliy Zhuk <zhuk2205@gmail.com>
*/
class DivisionByZeroException extends MathExecutorException
{
}

View File

@@ -0,0 +1,19 @@
<?php
/**
* This file is part of the MathExecutor package
*
* (c) Alexander Kiryukhin
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code
*/
namespace NXP\Exception;
/**
* @author Alexander Kiryukhin <a.kiryukhin@mail.ru>
*/
class IncorrectBracketsException extends MathExecutorException
{
}

View File

@@ -0,0 +1,19 @@
<?php
/**
* This file is part of the MathExecutor package
*
* (c) Alexander Kiryukhin
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code
*/
namespace NXP\Exception;
/**
* @author Vitaliy Zhuk <zhuk2205@gmail.com>
*/
class IncorrectExpressionException extends MathExecutorException
{
}

View File

@@ -0,0 +1,16 @@
<?php
/**
* This file is part of the MathExecutor package
*
* (c) Alexander Kiryukhin
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code
*/
namespace NXP\Exception;
class IncorrectFunctionParameterException extends MathExecutorException
{
}

View File

@@ -0,0 +1,16 @@
<?php
/**
* This file is part of the MathExecutor package
*
* (c) Alexander Kiryukhin
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code
*/
namespace NXP\Exception;
class IncorrectNumberOfFunctionParametersException extends MathExecutorException
{
}

View File

@@ -0,0 +1,19 @@
<?php
/**
* This file is part of the MathExecutor package
*
* (c) Alexander Kiryukhin
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code
*/
namespace NXP\Exception;
/**
* @author Vitaliy Zhuk <zhuk2205@gmail.com>
*/
class MathExecutorException extends \Exception
{
}

View File

@@ -0,0 +1,19 @@
<?php
/**
* This file is part of the MathExecutor package
*
* (c) Alexander Kiryukhin
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code
*/
namespace NXP\Exception;
/**
* @author Vitaliy Zhuk <zhuk2205@gmail.com>
*/
class UnknownFunctionException extends MathExecutorException
{
}

View File

@@ -0,0 +1,19 @@
<?php
/**
* This file is part of the MathExecutor package
*
* (c) Alexander Kiryukhin
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code
*/
namespace NXP\Exception;
/**
* @author Vitaliy Zhuk <zhuk2205@gmail.com>
*/
class UnknownOperatorException extends MathExecutorException
{
}

View File

@@ -0,0 +1,19 @@
<?php
/**
* This file is part of the MathExecutor package
*
* (c) Alexander Kiryukhin
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code
*/
namespace NXP\Exception;
/**
* @author Alexander Kiryukhin <a.kiryukhin@mail.ru>
*/
class UnknownVariableException extends MathExecutorException
{
}

View File

@@ -0,0 +1,548 @@
<?php
/**
* This file is part of the MathExecutor package
*
* (c) Alexander Kiryukhin
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code
*/
namespace NXP;
use NXP\Classes\Calculator;
use NXP\Classes\CustomFunction;
use NXP\Classes\Operator;
use NXP\Classes\Token;
use NXP\Classes\Tokenizer;
use NXP\Exception\DivisionByZeroException;
use NXP\Exception\MathExecutorException;
use NXP\Exception\UnknownVariableException;
use ReflectionException;
/**
* Class MathExecutor
* @package NXP
*/
class MathExecutor
{
/**
* Available variables
*
* @var array<string, float|string>
*/
protected array $variables = [];
/**
* @var callable|null
*/
protected $onVarNotFound = null;
/**
* @var callable|null
*/
protected $onVarValidation = null;
/**
* @var Operator[]
*/
protected array $operators = [];
/**
* @var array<string, CustomFunction>
*/
protected array $functions = [];
/**
* @var array<string, Token[]>
*/
protected array $cache = [];
/**
* Base math operators
*/
public function __construct()
{
$this->addDefaults();
}
public function __clone()
{
$this->addDefaults();
}
/**
* Add operator to executor
*
*/
public function addOperator(Operator $operator) : self
{
$this->operators[$operator->operator] = $operator;
return $this;
}
/**
* Execute expression
*
* @throws Exception\IncorrectExpressionException
* @throws Exception\UnknownOperatorException
* @throws UnknownVariableException
* @throws Exception\IncorrectBracketsException
* @return int|float|string|null
*/
public function execute(string $expression, bool $cache = true)
{
$cacheKey = $expression;
if (! \array_key_exists($cacheKey, $this->cache)) {
$tokens = (new Tokenizer($expression, $this->operators))->tokenize()->buildReversePolishNotation();
if ($cache) {
$this->cache[$cacheKey] = $tokens;
}
} else {
$tokens = $this->cache[$cacheKey];
}
$calculator = new Calculator($this->functions, $this->operators);
return $calculator->calculate($tokens, $this->variables, $this->onVarNotFound);
}
/**
* Add function to executor
*
* @param string $name Name of function
* @param callable|null $function Function
*
* @throws ReflectionException
* @throws Exception\IncorrectNumberOfFunctionParametersException
*/
public function addFunction(string $name, ?callable $function = null) : self
{
$this->functions[$name] = new CustomFunction($name, $function);
return $this;
}
/**
* Get all vars
*
* @return array<string, float|string>
*/
public function getVars() : array
{
return $this->variables;
}
/**
* Get a specific var
*
* @throws UnknownVariableException if VarNotFoundHandler is not set
*/
public function getVar(string $variable) : mixed
{
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];
}
/**
* Add variable to executor. To set a custom validator use setVarValidationHandler.
*
* @throws MathExecutorException if the value is invalid based on the default or custom validator
*/
public function setVar(string $variable, mixed $value) : self
{
if ($this->onVarValidation) {
\call_user_func($this->onVarValidation, $variable, $value);
}
$this->variables[$variable] = $value;
return $this;
}
/**
* Test to see if a variable exists
*
*/
public function varExists(string $variable) : bool
{
return \array_key_exists($variable, $this->variables);
}
/**
* Add variables to executor
*
* @param array<string, float|int|string> $variables
* @param bool $clear Clear previous variables
* @throws \Exception
*/
public function setVars(array $variables, bool $clear = true) : self
{
if ($clear) {
$this->removeVars();
}
foreach ($variables as $name => $value) {
$this->setVar($name, $value);
}
return $this;
}
/**
* Define a method that will be invoked when a variable is not found.
* The first parameter will be the variable name, and the returned value will be used as the variable value.
*
*
*/
public function setVarNotFoundHandler(callable $handler) : self
{
$this->onVarNotFound = $handler;
return $this;
}
/**
* 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
*
*/
public function setVarValidationHandler(?callable $handler) : self
{
$this->onVarValidation = $handler;
return $this;
}
/**
* Remove variable from executor
*
*/
public function removeVar(string $variable) : self
{
unset($this->variables[$variable]);
return $this;
}
/**
* Remove all variables and the variable not found handler
*/
public function removeVars() : self
{
$this->variables = [];
$this->onVarNotFound = null;
return $this;
}
/**
* Get all registered operators to executor
*
* @return array<Operator> of operator class names
*/
public function getOperators() : array
{
return $this->operators;
}
/**
* Get all registered functions
*
* @return array<string, CustomFunction> containing callback and places indexed by
* function name
*/
public function getFunctions() : array
{
return $this->functions;
}
/**
* Remove a specific operator
*/
public function removeOperator(string $operator) : self
{
unset($this->operators[$operator]);
return $this;
}
/**
* Set division by zero returns zero instead of throwing DivisionByZeroException
*/
public function setDivisionByZeroIsZero() : self
{
$this->addOperator(new Operator('/', false, 180, static fn($a, $b) => 0 == $b ? 0 : $a / $b));
return $this;
}
/**
* Get cache array with tokens
* @return array<string, Token[]>
*/
public function getCache() : array
{
return $this->cache;
}
/**
* Clear token's cache
*/
public function clearCache() : self
{
$this->cache = [];
return $this;
}
public function useBCMath(int $scale = 2) : self
{
\bcscale($scale);
$this->addOperator(new Operator('+', false, 170, static fn($a, $b) => \bcadd("{$a}", "{$b}")));
$this->addOperator(new Operator('-', false, 170, static fn($a, $b) => \bcsub("{$a}", "{$b}")));
$this->addOperator(new Operator('uNeg', false, 200, static fn($a) => \bcsub('0.0', "{$a}")));
$this->addOperator(new Operator('*', false, 180, static fn($a, $b) => \bcmul("{$a}", "{$b}")));
$this->addOperator(new Operator('/', false, 180, static function($a, $b) {
/** @todo PHP8: Use throw as expression -> static fn($a, $b) => 0 == $b ? throw new DivisionByZeroException() : $a / $b */
if (0 == $b) {
throw new DivisionByZeroException();
}
return \bcdiv("{$a}", "{$b}");
}));
$this->addOperator(new Operator('^', true, 220, static fn($a, $b) => \bcpow("{$a}", "{$b}")));
$this->addOperator(new Operator('%', false, 180, static fn($a, $b) => \bcmod("{$a}", "{$b}")));
return $this;
}
/**
* Set default operands and functions
* @throws ReflectionException
*/
protected function addDefaults() : self
{
foreach ($this->defaultOperators() as $name => $operator) {
[$callable, $priority, $isRightAssoc] = $operator;
$this->addOperator(new Operator($name, $isRightAssoc, $priority, $callable));
}
foreach ($this->defaultFunctions() as $name => $callable) {
$this->addFunction($name, $callable);
}
$this->onVarValidation = [$this, 'defaultVarValidation'];
$this->variables = $this->defaultVars();
return $this;
}
/**
* Get the default operators
*
* @return array<string, array{callable, int, bool}>
*/
protected function defaultOperators() : array
{
return [
'+' => [static fn($a, $b) => $a + $b, 170, false],
'-' => [static fn($a, $b) => $a - $b, 170, false],
// unary positive token
'uPos' => [static fn($a) => $a, 200, false],
// unary minus token
'uNeg' => [static fn($a) => 0 - $a, 200, false],
'*' => [static fn($a, $b) => $a * $b, 180, false],
'/' => [
static function($a, $b) {
/** @todo PHP8: Use throw as expression -> static fn($a, $b) => 0 == $b ? throw new DivisionByZeroException() : $a / $b */
if (0 == $b) {
throw new DivisionByZeroException();
}
return $a / $b;
},
180,
false
],
'^' => [static fn($a, $b) => $a ** $b, 220, true],
'%' => [static fn($a, $b) => $a % $b, 180, false],
'&&' => [static fn($a, $b) => $a && $b, 100, false],
'||' => [static fn($a, $b) => $a || $b, 90, false],
'==' => [static fn($a, $b) => \is_string($a) || \is_string($b) ? 0 == \strcmp((string)$a, (string)$b) : $a == $b, 140, false],
'!=' => [static fn($a, $b) => \is_string($a) || \is_string($b) ? 0 != \strcmp((string)$a, (string)$b) : $a != $b, 140, false],
'>=' => [static fn($a, $b) => $a >= $b, 150, false],
'>' => [static fn($a, $b) => $a > $b, 150, false],
'<=' => [static fn($a, $b) => $a <= $b, 150, false],
'<' => [static fn($a, $b) => $a < $b, 150, false],
'!' => [static fn($a) => ! $a, 190, false],
];
}
/**
* Gets the default functions as an array. Key is function name
* and value is the function as a closure.
*
* @return array<callable>
*/
protected function defaultFunctions() : array
{
return [
'abs' => static fn($arg) => \abs($arg),
'acos' => static fn($arg) => \acos($arg),
'acosh' => static fn($arg) => \acosh($arg),
'arcsin' => static fn($arg) => \asin($arg),
'arcctg' => static fn($arg) => M_PI / 2 - \atan($arg),
'arccot' => static fn($arg) => M_PI / 2 - \atan($arg),
'arccotan' => static fn($arg) => M_PI / 2 - \atan($arg),
'arcsec' => static fn($arg) => \acos(1 / $arg),
'arccosec' => static fn($arg) => \asin(1 / $arg),
'arccsc' => static fn($arg) => \asin(1 / $arg),
'arccos' => static fn($arg) => \acos($arg),
'arctan' => static fn($arg) => \atan($arg),
'arctg' => static fn($arg) => \atan($arg),
'array' => static fn(...$args) => $args,
'asin' => static fn($arg) => \asin($arg),
'atan' => static fn($arg) => \atan($arg),
'atan2' => static fn($arg1, $arg2) => \atan2($arg1, $arg2),
'atanh' => static fn($arg) => \atanh($arg),
'atn' => static fn($arg) => \atan($arg),
'avg' => static function($arg1, ...$args) {
if (\is_array($arg1)) {
if (0 === \count($arg1)) {
throw new \InvalidArgumentException('avg() must have at least one argument!');
}
return \array_sum($arg1) / \count($arg1);
}
$args = [$arg1, ...$args];
return \array_sum($args) / \count($args);
},
'bindec' => static fn($arg) => \bindec($arg),
'ceil' => static fn($arg) => \ceil($arg),
'cos' => static fn($arg) => \cos($arg),
'cosec' => static fn($arg) => 1 / \sin($arg),
'csc' => static fn($arg) => 1 / \sin($arg),
'cosh' => static fn($arg) => \cosh($arg),
'ctg' => static fn($arg) => \cos($arg) / \sin($arg),
'cot' => static fn($arg) => \cos($arg) / \sin($arg),
'cotan' => static fn($arg) => \cos($arg) / \sin($arg),
'cotg' => static fn($arg) => \cos($arg) / \sin($arg),
'ctn' => static fn($arg) => \cos($arg) / \sin($arg),
'decbin' => static fn($arg) => \decbin($arg),
'dechex' => static fn($arg) => \dechex($arg),
'decoct' => static fn($arg) => \decoct($arg),
'deg2rad' => static fn($arg) => \deg2rad($arg),
'exp' => static fn($arg) => \exp($arg),
'expm1' => static fn($arg) => \expm1($arg),
'floor' => static fn($arg) => \floor($arg),
'fmod' => static fn($arg1, $arg2) => \fmod($arg1, $arg2),
'hexdec' => static fn($arg) => \hexdec($arg),
'hypot' => static fn($arg1, $arg2) => \hypot($arg1, $arg2),
'if' => function($expr, $trueval, $falseval) {
if (true === $expr || false === $expr) {
$exres = $expr;
} else {
$exres = $this->execute($expr);
}
if ($exres) {
return $this->execute($trueval);
}
return $this->execute($falseval);
},
'intdiv' => static fn($arg1, $arg2) => \intdiv($arg1, $arg2),
'ln' => static fn($arg) => \log($arg),
'lg' => static fn($arg) => \log10($arg),
'log' => static fn($arg) => \log($arg),
'log10' => static fn($arg) => \log10($arg),
'log1p' => static fn($arg) => \log1p($arg),
'max' => static function($arg1, ...$args) {
if (\is_array($arg1) && 0 === \count($arg1)) {
throw new \InvalidArgumentException('max() must have at least one argument!');
}
return \max(\is_array($arg1) ? $arg1 : [$arg1, ...$args]);
},
'median' => static function($arg1, ...$args) {
if (\is_array($arg1)) {
if (0 === \count($arg1)) {
throw new \InvalidArgumentException('Array must contain at least one element!');
}
$finalArgs = $arg1;
} else {
$finalArgs = [$arg1, ...$args];
}
$count = \count($finalArgs);
\sort($finalArgs);
$index = \floor($count / 2);
return ($count & 1) ? $finalArgs[$index] : ($finalArgs[$index - 1] + $finalArgs[$index]) / 2;
},
'min' => static function($arg1, ...$args) {
if (\is_array($arg1) && 0 === \count($arg1)) {
throw new \InvalidArgumentException('min() must have at least one argument!');
}
return \min(\is_array($arg1) ? $arg1 : [$arg1, ...$args]);
},
'octdec' => static fn($arg) => \octdec($arg),
'pi' => static fn() => M_PI,
'pow' => static fn($arg1, $arg2) => $arg1 ** $arg2,
'rad2deg' => static fn($arg) => \rad2deg($arg),
'round' => static fn($num, int $precision = 0) => \round($num, $precision),
'sin' => static fn($arg) => \sin($arg),
'sinh' => static fn($arg) => \sinh($arg),
'sec' => static fn($arg) => 1 / \cos($arg),
'sqrt' => static fn($arg) => \sqrt($arg),
'tan' => static fn($arg) => \tan($arg),
'tanh' => static fn($arg) => \tanh($arg),
'tn' => static fn($arg) => \tan($arg),
'tg' => static fn($arg) => \tan($arg)
];
}
/**
* Returns the default variables names as key/value pairs
*
* @return array<string, float>
*/
protected function defaultVars() : array
{
return [
'pi' => 3.14159265359,
'e' => 2.71828182846
];
}
/**
* Default variable validation, ensures that the value is a scalar or array.
* @throws MathExecutorException if the value is not a scalar
*/
protected function defaultVarValidation(string $variable, mixed $value) : void
{
if (! \is_scalar($value) && ! \is_array($value) && null !== $value) {
$type = \gettype($value);
throw new MathExecutorException("Variable ({$variable}) type ({$type}) is not scalar or array!");
}
}
}