From 6a077bb5a1563d6cd704013ded241cf586c33f90 Mon Sep 17 00:00:00 2001 From: Netkas Date: Mon, 26 Dec 2022 16:24:32 -0500 Subject: [PATCH] Refactored Classes --- src/RTEX/Classes/InstructionBuilder.php | 49 ++++---------- src/RTEX/Classes/Utilities.php | 90 +++++++++++++++---------- src/RTEX/Classes/Validate.php | 2 - 3 files changed, 68 insertions(+), 73 deletions(-) diff --git a/src/RTEX/Classes/InstructionBuilder.php b/src/RTEX/Classes/InstructionBuilder.php index 445c76b..f247278 100644 --- a/src/RTEX/Classes/InstructionBuilder.php +++ b/src/RTEX/Classes/InstructionBuilder.php @@ -1,10 +1,8 @@ ArrayGet::fromArray($value['_']), // Default - //default => throw new UnsupportedVariableType($value['type']), + default => throw new InstructionException(sprintf('Unknown instruction type "%s"', $value['type'])), }; } + // Recursive call if it's an array elseif(is_array($value)) { @@ -92,7 +90,6 @@ * @param $type * @param $value * @return array - * @throws UnsupportedVariableType */ public static function toRaw($type, $value): array { @@ -107,8 +104,6 @@ * * @param $name * @return InstructionInterface - * @throws MalformedInstructionException - * @throws UnsupportedVariableType */ public static function getVariable($name): InstructionInterface { @@ -124,8 +119,6 @@ * @param $name * @param $value * @return InstructionInterface - * @throws MalformedInstructionException - * @throws UnsupportedVariableType */ public static function setVariable($name, $value): InstructionInterface { @@ -141,8 +134,6 @@ * @param array $parameters * @param bool $fail_on_error * @return InstructionInterface - * @throws MalformedInstructionException - * @throws UnsupportedVariableType */ public static function invoke(string $method, array $parameters, bool $fail_on_error=true): InstructionInterface { @@ -167,8 +158,6 @@ * @param $a * @param $b * @return InstructionInterface - * @throws MalformedInstructionException - * @throws UnsupportedVariableType */ public static function equals($a, $b): InstructionInterface { @@ -186,8 +175,7 @@ * @param $a * @param $b * @return InstructionInterface - * @throws MalformedInstructionException - * @throws UnsupportedVariableType + * @noinspection PhpUnused */ public static function sum($a, $b): InstructionInterface { @@ -205,8 +193,7 @@ * @param $a * @param $b * @return InstructionInterface - * @throws MalformedInstructionException - * @throws UnsupportedVariableType + * @noinspection PhpUnused */ public static function subtract($a, $b): InstructionInterface { @@ -224,8 +211,7 @@ * @param $a * @param $b * @return InstructionInterface - * @throws MalformedInstructionException - * @throws UnsupportedVariableType + * @noinspection PhpUnused */ public static function multiply($a, $b): InstructionInterface { @@ -243,8 +229,7 @@ * @param $a * @param $b * @return InstructionInterface - * @throws MalformedInstructionException - * @throws UnsupportedVariableType + * @noinspection PhpUnused */ public static function divide($a, $b): InstructionInterface { @@ -262,8 +247,7 @@ * @param $a * @param $b * @return InstructionInterface - * @throws MalformedInstructionException - * @throws UnsupportedVariableType + * @noinspection PhpUnused */ public static function modulo($a, $b): InstructionInterface { @@ -281,8 +265,7 @@ * @param $a * @param $b * @return InstructionInterface - * @throws MalformedInstructionException - * @throws UnsupportedVariableType + * @noinspection PhpUnused */ public static function power($a, $b): InstructionInterface { @@ -300,8 +283,7 @@ * @param $a * @param $b * @return InstructionInterface - * @throws MalformedInstructionException - * @throws UnsupportedVariableType + * @noinspection PhpUnused */ public static function greaterThan($a, $b): InstructionInterface { @@ -319,8 +301,7 @@ * @param $a * @param $b * @return InstructionInterface - * @throws MalformedInstructionException - * @throws UnsupportedVariableType + * @noinspection PhpUnused */ public static function greaterThanOrEquals($a, $b): InstructionInterface { @@ -338,8 +319,7 @@ * @param $a * @param $b * @return InstructionInterface - * @throws MalformedInstructionException - * @throws UnsupportedVariableType + * @noinspection PhpUnused */ public static function lessThan($a, $b): InstructionInterface { @@ -357,8 +337,7 @@ * @param $a * @param $b * @return InstructionInterface - * @throws MalformedInstructionException - * @throws UnsupportedVariableType + * @noinspection PhpUnused */ public static function lessThanOrEquals($a, $b): InstructionInterface { diff --git a/src/RTEX/Classes/Utilities.php b/src/RTEX/Classes/Utilities.php index 8f511ac..51f3829 100644 --- a/src/RTEX/Classes/Utilities.php +++ b/src/RTEX/Classes/Utilities.php @@ -2,13 +2,9 @@ namespace RTEX\Classes; - use RTEX\Abstracts\InstructionType; - use RTEX\Abstracts\VariableTypes; - use RTEX\Exceptions\Core\MalformedInstructionException; - use RTEX\Exceptions\Core\UnsupportedVariableType; + use RTEX\Abstracts\VariableType; + use RTEX\Exceptions\Runtime\TypeException; use RTEX\Interfaces\InstructionInterface; - use RTEX\Objects\Program\Instructions\GetVariable; - use RTEX\Objects\Program\Instructions\SetVariable; class Utilities { @@ -17,65 +13,87 @@ * * @param $input * @return string - * @throws UnsupportedVariableType + * @throws TypeException */ public static function determineType($input): string { if ($input instanceof InstructionInterface) - return VariableTypes::Instruction; + return VariableType::Instruction; if (is_string($input)) - return VariableTypes::String; + return VariableType::String; if (is_int($input)) - return VariableTypes::Integer; + return VariableType::Integer; if (is_float($input)) - return VariableTypes::Float; + return VariableType::Float; if (is_bool($input)) - return VariableTypes::Boolean; + return VariableType::Boolean; + if (is_array($input)) + return VariableType::Array; if (is_null($input)) - return VariableTypes::Null; + return VariableType::Null; - throw new UnsupportedVariableType(gettype($input)); + throw new TypeException(gettype($input)); } /** * Returns a supported variable type to an array representation + * or a single value if it's not an array or an instruction * * @param $input * @return array|mixed - * @throws UnsupportedVariableType + * @noinspection PhpMissingReturnTypeInspection */ public static function toArray($input) { - return match (self::determineType($input)) + if($input instanceof InstructionInterface) + return $input->toArray(); + + if(is_array($input)) { - VariableTypes::Instruction => $input->toArray(), - default => $input, - }; + $output = []; + foreach($input as $key => $value) + $output[$key] = self::toArray($value); + return $output; + } + + return $input; } /** - * Constructs an instruction from an array representation + * Returns a string representation of a variable type + * or an instruction type if it's an instruction * - * @param array $array - * @return InstructionInterface - * @throws MalformedInstructionException - * @throws UnsupportedVariableType + * This cannot be used as a method of serialization + * + * @param $input + * @return string */ - public static function constructInstruction(array $array): InstructionInterface + public static function entityToString($input): string { - if(!isset($array['type'])) - throw new MalformedInstructionException(sprintf('Instruction type not specified')); - if(!isset($array['_'])) - throw new MalformedInstructionException(sprintf('Instruction data not specified')); + /** @var InstructionInterface $input */ + if($input instanceof InstructionInterface) + return (string)$input; - switch($array['type']) + if(is_array($input)) { - case InstructionType::GetVariable: - return GetVariable::fromArray($array['_']); - case InstructionType::SetVariable: - return SetVariable::fromArray($array['_']); - default: - throw new MalformedInstructionException(sprintf('Instruction type "%s" not supported', $array['type'])); + $output = []; + foreach($input as $key => $value) + $output[$key] = self::entityToString($value); + return json_encode($output, JSON_UNESCAPED_SLASHES); } + + if(is_string($input)) + return "'$input'"; + if(is_int($input)) + return 'int(' . $input . ')'; + if(is_float($input)) + return 'float(' . $input . ')'; + if(is_bool($input)) + return $input ? 'True' : 'False'; + if(is_null($input)) + return 'NULL'; + + return 'Unknown'; } + } \ No newline at end of file diff --git a/src/RTEX/Classes/Validate.php b/src/RTEX/Classes/Validate.php index 606c358..92f7b8d 100644 --- a/src/RTEX/Classes/Validate.php +++ b/src/RTEX/Classes/Validate.php @@ -3,8 +3,6 @@ namespace RTEX\Classes; use Exception; - use RTEX\Abstracts\VariableTypes; - use RTEX\Interfaces\InstructionInterface; class Validate {