Updated \RTEX\Classes > InstructionBuilder

This commit is contained in:
Netkas 2022-12-30 04:26:15 -05:00
parent 5ed3b01889
commit 21cd927fbe

View file

@ -4,21 +4,27 @@
use RTEX\Abstracts\InstructionType; use RTEX\Abstracts\InstructionType;
use RTEX\Exceptions\InstructionException; use RTEX\Exceptions\InstructionException;
use RTEX\Interfaces\InstructionInterface; use RTEX\Interfaces\InstructionInterface;
use RTEX\Objects\Program\Instructions\ArrayGet; use RTEX\Objects\Instructions\Arithmetic\Absolute;
use RTEX\Objects\Program\Instructions\Divide; use RTEX\Objects\Instructions\Arithmetic\Divide;
use RTEX\Objects\Program\Instructions\Equals; use RTEX\Objects\Instructions\Arithmetic\Floor;
use RTEX\Objects\Program\Instructions\GetVariable; use RTEX\Objects\Instructions\Arithmetic\Modulo;
use RTEX\Objects\Program\Instructions\GreaterThan; use RTEX\Objects\Instructions\Arithmetic\Multiply;
use RTEX\Objects\Program\Instructions\GreaterThanOrEquals; use RTEX\Objects\Instructions\Arithmetic\Power;
use RTEX\Objects\Program\Instructions\Invoke; use RTEX\Objects\Instructions\Arithmetic\Round;
use RTEX\Objects\Program\Instructions\LessThan; use RTEX\Objects\Instructions\Arithmetic\SquareRoot;
use RTEX\Objects\Program\Instructions\LessThanOrEquals; use RTEX\Objects\Instructions\Arithmetic\Subtract;
use RTEX\Objects\Program\Instructions\Modulo; use RTEX\Objects\Instructions\Arithmetic\Sum;
use RTEX\Objects\Program\Instructions\Multiply; use RTEX\Objects\Instructions\Base\ArrayGet;
use RTEX\Objects\Program\Instructions\Power; use RTEX\Objects\Instructions\Base\ArraySet;
use RTEX\Objects\Program\Instructions\SetVariable; use RTEX\Objects\Instructions\Base\GetVariable;
use RTEX\Objects\Program\Instructions\Subtract; use RTEX\Objects\Instructions\Base\Invoke;
use RTEX\Objects\Program\Instructions\Sum; use RTEX\Objects\Instructions\Base\SetVariable;
use RTEX\Objects\Instructions\Comparators\Equals;
use RTEX\Objects\Instructions\Comparators\GreaterThan;
use RTEX\Objects\Instructions\Comparators\GreaterThanOrEqual;
use RTEX\Objects\Instructions\Comparators\LessThan;
use RTEX\Objects\Instructions\Comparators\LessThanOrEqual;
use RTEX\Objects\Instructions\Comparators\NotEquals;
class InstructionBuilder class InstructionBuilder
{ {
@ -28,9 +34,8 @@
* @param $value * @param $value
* @return array|mixed|InstructionInterface|null * @return array|mixed|InstructionInterface|null
* @throws InstructionException * @throws InstructionException
* @noinspection PhpMissingReturnTypeInspection
*/ */
public static function fromRaw($value) public static function fromRaw($value): mixed
{ {
// Check if it's a supported variable type // Check if it's a supported variable type
if(!Validate::supportedVariableType($value)) if(!Validate::supportedVariableType($value))
@ -42,30 +47,34 @@
// Return the constructed InstructionInterface object // Return the constructed InstructionInterface object
return match ($value['type']) return match ($value['type'])
{ {
// Arithmetic operations
InstructionType::Absolute => Absolute::fromArray($value['_']),
InstructionType::Divide => Divide::fromArray($value['_']),
InstructionType::Floor => Floor::fromArray($value['_']),
InstructionType::Modulo => Modulo::fromArray($value['_']),
InstructionType::Multiply => Multiply::fromArray($value['_']),
InstructionType::Power => Power::fromArray($value['_']),
InstructionType::Round => Round::fromArray($value['_']),
InstructionType::SquareRoot => SquareRoot::fromArray($value['_']),
InstructionType::Subtract => Subtract::fromArray($value['_']),
InstructionType::Sum => Sum::fromArray($value['_']),
// Base instructions // Base instructions
InstructionType::Invoke => Invoke::fromArray($value['_']), InstructionType::Invoke => Invoke::fromArray($value['_']),
// Variables and constants
InstructionType::GetVariable => GetVariable::fromArray($value['_']), InstructionType::GetVariable => GetVariable::fromArray($value['_']),
InstructionType::SetVariable => SetVariable::fromArray($value['_']), InstructionType::SetVariable => SetVariable::fromArray($value['_']),
// Math operations
InstructionType::Equals => Equals::fromArray($value['_']),
InstructionType::Sum => Sum::fromArray($value['_']),
InstructionType::Subtract => Subtract::fromArray($value['_']),
InstructionType::Multiply => Multiply::fromArray($value['_']),
InstructionType::Divide => Divide::fromArray($value['_']),
InstructionType::Modulo => Modulo::fromArray($value['_']),
InstructionType::Power => Power::fromArray($value['_']),
InstructionType::GreaterThan => GreaterThan::fromArray($value['_']),
InstructionType::GreaterThanOrEquals => GreaterThanOrEquals::fromArray($value['_']),
InstructionType::LessThan => LessThan::fromArray($value['_']),
InstructionType::LessThanOrEquals => LessThanOrEquals::fromArray($value['_']),
// Array operations
InstructionType::ArrayGet => ArrayGet::fromArray($value['_']), InstructionType::ArrayGet => ArrayGet::fromArray($value['_']),
InstructionType::ArraySet => ArraySet::fromArray($value['_']),
// Default // Comparators
InstructionType::Equals => Equals::fromArray($value['_']),
InstructionType::GreaterThan => GreaterThan::fromArray($value['_']),
InstructionType::GreaterThanOrEqual => GreaterThanOrEqual::fromArray($value['_']),
InstructionType::LessThan => LessThan::fromArray($value['_']),
InstructionType::LessThanOrEqual => LessThanOrEqual::fromArray($value['_']),
InstructionType::NotEquals => NotEquals::fromArray($value['_']),
// Default (Unknown)
default => throw new InstructionException(sprintf('Unknown instruction type "%s"', $value['type'])), default => throw new InstructionException(sprintf('Unknown instruction type "%s"', $value['type'])),
}; };
} }
@ -99,139 +108,31 @@
]; ];
} }
/**
* Constructs a new get variable instruction
*
* @param $name
* @return InstructionInterface
*/
public static function getVariable($name): InstructionInterface
{
$instruction = new GetVariable();
$instruction->setVariable($name);
return $instruction;
}
/** /**
* Constructs a new set variable instruction * Returns the absolute value of a number
* *
* @param $name * @param $value float|int|InstructionInterface The number to get the absolute value of
* @param $value
* @return InstructionInterface * @return InstructionInterface
* @throws InstructionException
*/ */
public static function setVariable($name, $value): InstructionInterface public static function abs(InstructionInterface|float|int $value): InstructionInterface
{ {
$instruction = new SetVariable(); $instruction = new Absolute();
$instruction->setVariable($name);
$instruction->setValue($value); $instruction->setValue($value);
return $instruction; return $instruction;
} }
/** /**
* @param string $method * Divides two numbers and returns the result
* @param array $parameters
* @param bool $fail_on_error
* @return InstructionInterface
*/
public static function invoke(string $method, array $parameters, bool $fail_on_error=true): InstructionInterface
{
$exploded = explode('.', $method);
//if(count($exploded) !== 2)
// throw new UndefinedMethodException(sprintf('Invalid method name "%s"', $method));
$instruction = new Invoke();
$instruction->setNamespace($exploded[0]);
$instruction->setMethod($exploded[1]);
$instruction->setParameters($parameters);
$instruction->setFailOnError($fail_on_error);
return $instruction;
}
/**
* Creates a new equals instruction from two values or instructions
* (or a combination of both)
* *
* @param $a * @param $a float|int|InstructionInterface The dividend
* @param $b * @param $b float|int|InstructionInterface The divisor
* @return InstructionInterface * @return InstructionInterface
* @throws InstructionException
*/ */
public static function equals($a, $b): InstructionInterface public static function div(InstructionInterface|float|int $a, InstructionInterface|float|int $b): InstructionInterface
{
$instruction = new Equals();
$instruction->setA($a);
$instruction->setB($b);
return $instruction;
}
/**
* Creates a new sum instruction from two values or instructions
* (or a combination of both)
*
* @param $a
* @param $b
* @return InstructionInterface
* @noinspection PhpUnused
*/
public static function sum($a, $b): InstructionInterface
{
$instruction = new Sum();
$instruction->setA($a);
$instruction->setB($b);
return $instruction;
}
/**
* Creates a new subtract instruction from two values or instructions
* (or a combination of both)
*
* @param $a
* @param $b
* @return InstructionInterface
* @noinspection PhpUnused
*/
public static function subtract($a, $b): InstructionInterface
{
$instruction = new Subtract();
$instruction->setA($a);
$instruction->setB($b);
return $instruction;
}
/**
* Creates a new multiply instruction from two values or instructions
* (or a combination of both)
*
* @param $a
* @param $b
* @return InstructionInterface
* @noinspection PhpUnused
*/
public static function multiply($a, $b): InstructionInterface
{
$instruction = new Multiply();
$instruction->setA($a);
$instruction->setB($b);
return $instruction;
}
/**
* Creates a new divide instruction from two values or instructions
* (or a combination of both)
*
* @param $a
* @param $b
* @return InstructionInterface
* @noinspection PhpUnused
*/
public static function divide($a, $b): InstructionInterface
{ {
$instruction = new Divide(); $instruction = new Divide();
$instruction->setA($a); $instruction->setA($a);
@ -241,15 +142,29 @@
} }
/** /**
* Creates a new modulo instruction from two values or instructions * Returns the largest integer less than or equal to a number
* (or a combination of both)
* *
* @param $a * @param $value float|int|InstructionInterface The number to get the floor of
* @param $b
* @return InstructionInterface * @return InstructionInterface
* @noinspection PhpUnused * @throws InstructionException
*/ */
public static function modulo($a, $b): InstructionInterface public static function floor(InstructionInterface|float|int $value): InstructionInterface
{
$instruction = new Floor();
$instruction->setValue($value);
return $instruction;
}
/**
* Returns the remainder of a division
*
* @param $a float|int|InstructionInterface The dividend
* @param $b float|int|InstructionInterface The divisor
* @return InstructionInterface
* @throws InstructionException
*/
public static function mod(InstructionInterface|float|int $a, InstructionInterface|float|int $b): InstructionInterface
{ {
$instruction = new Modulo(); $instruction = new Modulo();
$instruction->setA($a); $instruction->setA($a);
@ -259,15 +174,31 @@
} }
/** /**
* Creates a new power instruction from two values or instructions * Multiplies two numbers and returns the result
* (or a combination of both)
* *
* @param $a * @param $a float|int|InstructionInterface The first number
* @param $b * @param $b float|int|InstructionInterface The second number
* @return InstructionInterface * @return InstructionInterface
* @noinspection PhpUnused * @throws InstructionException
*/ */
public static function power($a, $b): InstructionInterface public static function mul(InstructionInterface|float|int $a, InstructionInterface|float|int $b): InstructionInterface
{
$instruction = new Multiply();
$instruction->setA($a);
$instruction->setB($b);
return $instruction;
}
/**
* Raises a number to a power and returns the result
*
* @param $a float|int|InstructionInterface The base number
* @param $b float|int|InstructionInterface The exponent
* @return InstructionInterface
* @throws InstructionException
*/
public static function pow(InstructionInterface|float|int $a, InstructionInterface|float|int $b): InstructionInterface
{ {
$instruction = new Power(); $instruction = new Power();
$instruction->setA($a); $instruction->setA($a);
@ -277,15 +208,201 @@
} }
/** /**
* Creates a new greater than instruction from two values or instructions * Rounds a number to a given precision
* (or a combination of both)
* *
* @param $a * @param $value float|int|InstructionInterface The number to round
* @param $b * @param $precision int|InstructionInterface The precision to round to
* @return InstructionInterface * @return InstructionInterface
* @noinspection PhpUnused * @throws InstructionException
*/ */
public static function greaterThan($a, $b): InstructionInterface public static function round(InstructionInterface|float|int $value, InstructionInterface|int $precision=0): InstructionInterface
{
$instruction = new Round();
$instruction->setValue($value);
$instruction->setPrecision($precision);
return $instruction;
}
/**
* Returns the square root of a number
*
* @param $value float|int|InstructionInterface The number to get the square root of
* @return InstructionInterface
* @throws InstructionException
*/
public static function sqrt(InstructionInterface|float|int $value): InstructionInterface
{
$instruction = new SquareRoot();
$instruction->setValue($value);
return $instruction;
}
/**
* Subtracts two numbers and returns the result
*
* @param $a float|int|InstructionInterface The minuend
* @param $b float|int|InstructionInterface The subtrahend
* @return InstructionInterface
* @throws InstructionException
*/
public static function sub(InstructionInterface|float|int $a, InstructionInterface|float|int $b): InstructionInterface
{
$instruction = new Subtract();
$instruction->setA($a);
$instruction->setB($b);
return $instruction;
}
/**
* Adds two numbers and returns the result
*
* @param $a float|int|InstructionInterface The first number
* @param $b float|int|InstructionInterface The second number
* @return InstructionInterface
* @throws InstructionException
*/
public static function sum(InstructionInterface|float|int $a, InstructionInterface|float|int $b): InstructionInterface
{
$instruction = new Sum();
$instruction->setA($a);
$instruction->setB($b);
return $instruction;
}
/**
* Returns the requested key from an array
*
* @param InstructionInterface|array $array The array to get the key from
* @param InstructionInterface|string $key The key to get (can be a string or an instruction) (eg; "foo" or "foo.bar")
* @return InstructionInterface
* @throws InstructionException
*/
public static function array_get(InstructionInterface|array $array, InstructionInterface|string $key): InstructionInterface
{
$instruction = new ArrayGet();
$instruction->setArray($array);
$instruction->setKey($key);
return $instruction;
}
/**
* Sets the requested key in an array
*
* @param InstructionInterface|array $array The array to set the key in
* @param InstructionInterface|string $key The key to get (can be a string or an instruction) (eg; "foo" or "foo.bar")
* @param mixed $value The value to set the key to
* @return InstructionInterface
* @throws InstructionException
*/
public static function array_set(InstructionInterface|array $array, InstructionInterface|string $key, mixed $value): InstructionInterface
{
$instruction = new ArraySet();
$instruction->setArray($array);
$instruction->setKey($key);
$instruction->setValue($value);
return $instruction;
}
/**
* Gets an existing variable from the environment
*
* @param InstructionInterface|string $name The name of the variable to get
* @return InstructionInterface
* @throws InstructionException
*/
public static function get(InstructionInterface|string $name): InstructionInterface
{
$instruction = new GetVariable();
$instruction->setVariableName($name);
return $instruction;
}
/**
* Invokes a callable with the given arguments
*
* @param InstructionInterface|string $callable The callable to invoke
* @param array $parameters The parameters to pass to the callable
* @param bool|InstructionBuilder $fail_on_error Whether to fail on error or not
* @return InstructionInterface
* @throws InstructionException
*/
public static function invoke(InstructionInterface|string $callable, array $parameters=[], bool|InstructionBuilder $fail_on_error=true): InstructionInterface
{
$instruction = new Invoke();
$instruction->setCallable($callable);
$instruction->setParameters($parameters);
$instruction->setFailOnError($fail_on_error);
return $instruction;
}
/**
* Sets a variable in the environment
*
* @param InstructionInterface|string $name The name of the variable to set
* @param mixed $value The value to set the variable to
* @return InstructionInterface
* @throws InstructionException
*/
public static function set(InstructionInterface|string $name, mixed $value): InstructionInterface
{
$instruction = new SetVariable();
$instruction->setName($name);
$instruction->setValue($value);
return $instruction;
}
/**
* Returns true if the two values are equal
*
* @param InstructionInterface|float|int|string $a The first value
* @param InstructionInterface|float|int|string $b The second value
* @return InstructionInterface
* @throws InstructionException
*/
public static function eq(InstructionInterface|float|int|string $a, InstructionInterface|float|int|string $b): InstructionInterface
{
$instruction = new Equals();
$instruction->setA($a);
$instruction->setB($b);
return $instruction;
}
/**
* Returns true if the two values are not equal
*
* @param InstructionInterface|float|int|string $a The first value
* @param InstructionInterface|float|int|string $b The second value
* @return InstructionInterface
* @throws InstructionException
*/
public static function neq(InstructionInterface|float|int|string $a, InstructionInterface|float|int|string $b): InstructionInterface
{
$instruction = new NotEquals();
$instruction->setA($a);
$instruction->setB($b);
return $instruction;
}
/**
* Returns true if the first value is greater than the second value
*
* @param InstructionInterface|float|int|string $a The first value
* @param InstructionInterface|float|int|string $b The second value
* @return InstructionInterface
* @throws InstructionException
*/
public static function gt(InstructionInterface|float|int|string $a, InstructionInterface|float|int|string $b): InstructionInterface
{ {
$instruction = new GreaterThan(); $instruction = new GreaterThan();
$instruction->setA($a); $instruction->setA($a);
@ -295,17 +412,16 @@
} }
/** /**
* Creates a new greater than or equals instruction from two values or instructions * Returns true if the first value is greater than or equal to the second value
* (or a combination of both)
* *
* @param $a * @param InstructionInterface|float|int|string $a The first value
* @param $b * @param InstructionInterface|float|int|string $b The second value
* @return InstructionInterface * @return InstructionInterface
* @noinspection PhpUnused * @throws InstructionException
*/ */
public static function greaterThanOrEquals($a, $b): InstructionInterface public static function gte(InstructionInterface|float|int|string $a, InstructionInterface|float|int|string $b): InstructionInterface
{ {
$instruction = new GreaterThanOrEquals(); $instruction = new GreaterThanOrEqual();
$instruction->setA($a); $instruction->setA($a);
$instruction->setB($b); $instruction->setB($b);
@ -313,15 +429,14 @@
} }
/** /**
* Creates a new less than instruction from two values or instructions * Returns true if the first value is less than the second value
* (or a combination of both)
* *
* @param $a * @param InstructionInterface|float|int|string $a The first value
* @param $b * @param InstructionInterface|float|int|string $b The second value
* @return InstructionInterface * @return InstructionInterface
* @noinspection PhpUnused * @throws InstructionException
*/ */
public static function lessThan($a, $b): InstructionInterface public static function lt(InstructionInterface|float|int|string $a, InstructionInterface|float|int|string $b): InstructionInterface
{ {
$instruction = new LessThan(); $instruction = new LessThan();
$instruction->setA($a); $instruction->setA($a);
@ -330,18 +445,18 @@
return $instruction; return $instruction;
} }
/** /**
* Creates a new less than or equals instruction from two values or instructions * Returns true if the first value is less than or equal to the second value
* (or a combination of both)
* *
* @param $a * @param InstructionInterface|float|int|string $a The first value
* @param $b * @param InstructionInterface|float|int|string $b The second value
* @return InstructionInterface * @return InstructionInterface
* @noinspection PhpUnused * @throws InstructionException
*/ */
public static function lessThanOrEquals($a, $b): InstructionInterface public static function lte(InstructionInterface|float|int|string $a, InstructionInterface|float|int|string $b): InstructionInterface
{ {
$instruction = new LessThanOrEquals(); $instruction = new LessThanOrEqual();
$instruction->setA($a); $instruction->setA($a);
$instruction->setB($b); $instruction->setB($b);