Refactored Classes

This commit is contained in:
Netkas 2022-12-26 16:24:32 -05:00
parent 05428dca07
commit 6a077bb5a1
3 changed files with 68 additions and 73 deletions

View file

@ -1,10 +1,8 @@
<?php <?php
namespace RTEX\Classes; namespace RTEX\Classes;
use RTEX\Abstracts\InstructionType; use RTEX\Abstracts\InstructionType;
use RTEX\Exceptions\Core\MalformedInstructionException; use RTEX\Exceptions\InstructionException;
use RTEX\Exceptions\Core\UnsupportedVariableType;
use RTEX\Interfaces\InstructionInterface; use RTEX\Interfaces\InstructionInterface;
use RTEX\Objects\Program\Instructions\ArrayGet; use RTEX\Objects\Program\Instructions\ArrayGet;
use RTEX\Objects\Program\Instructions\Divide; use RTEX\Objects\Program\Instructions\Divide;
@ -29,8 +27,7 @@
* *
* @param $value * @param $value
* @return array|mixed|InstructionInterface|null * @return array|mixed|InstructionInterface|null
* @throws UnsupportedVariableType * @throws InstructionException
* @throws MalformedInstructionException
* @noinspection PhpMissingReturnTypeInspection * @noinspection PhpMissingReturnTypeInspection
*/ */
public static function fromRaw($value) public static function fromRaw($value)
@ -69,9 +66,10 @@
InstructionType::ArrayGet => ArrayGet::fromArray($value['_']), InstructionType::ArrayGet => ArrayGet::fromArray($value['_']),
// Default // 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 // Recursive call if it's an array
elseif(is_array($value)) elseif(is_array($value))
{ {
@ -92,7 +90,6 @@
* @param $type * @param $type
* @param $value * @param $value
* @return array * @return array
* @throws UnsupportedVariableType
*/ */
public static function toRaw($type, $value): array public static function toRaw($type, $value): array
{ {
@ -107,8 +104,6 @@
* *
* @param $name * @param $name
* @return InstructionInterface * @return InstructionInterface
* @throws MalformedInstructionException
* @throws UnsupportedVariableType
*/ */
public static function getVariable($name): InstructionInterface public static function getVariable($name): InstructionInterface
{ {
@ -124,8 +119,6 @@
* @param $name * @param $name
* @param $value * @param $value
* @return InstructionInterface * @return InstructionInterface
* @throws MalformedInstructionException
* @throws UnsupportedVariableType
*/ */
public static function setVariable($name, $value): InstructionInterface public static function setVariable($name, $value): InstructionInterface
{ {
@ -141,8 +134,6 @@
* @param array $parameters * @param array $parameters
* @param bool $fail_on_error * @param bool $fail_on_error
* @return InstructionInterface * @return InstructionInterface
* @throws MalformedInstructionException
* @throws UnsupportedVariableType
*/ */
public static function invoke(string $method, array $parameters, bool $fail_on_error=true): InstructionInterface public static function invoke(string $method, array $parameters, bool $fail_on_error=true): InstructionInterface
{ {
@ -167,8 +158,6 @@
* @param $a * @param $a
* @param $b * @param $b
* @return InstructionInterface * @return InstructionInterface
* @throws MalformedInstructionException
* @throws UnsupportedVariableType
*/ */
public static function equals($a, $b): InstructionInterface public static function equals($a, $b): InstructionInterface
{ {
@ -186,8 +175,7 @@
* @param $a * @param $a
* @param $b * @param $b
* @return InstructionInterface * @return InstructionInterface
* @throws MalformedInstructionException * @noinspection PhpUnused
* @throws UnsupportedVariableType
*/ */
public static function sum($a, $b): InstructionInterface public static function sum($a, $b): InstructionInterface
{ {
@ -205,8 +193,7 @@
* @param $a * @param $a
* @param $b * @param $b
* @return InstructionInterface * @return InstructionInterface
* @throws MalformedInstructionException * @noinspection PhpUnused
* @throws UnsupportedVariableType
*/ */
public static function subtract($a, $b): InstructionInterface public static function subtract($a, $b): InstructionInterface
{ {
@ -224,8 +211,7 @@
* @param $a * @param $a
* @param $b * @param $b
* @return InstructionInterface * @return InstructionInterface
* @throws MalformedInstructionException * @noinspection PhpUnused
* @throws UnsupportedVariableType
*/ */
public static function multiply($a, $b): InstructionInterface public static function multiply($a, $b): InstructionInterface
{ {
@ -243,8 +229,7 @@
* @param $a * @param $a
* @param $b * @param $b
* @return InstructionInterface * @return InstructionInterface
* @throws MalformedInstructionException * @noinspection PhpUnused
* @throws UnsupportedVariableType
*/ */
public static function divide($a, $b): InstructionInterface public static function divide($a, $b): InstructionInterface
{ {
@ -262,8 +247,7 @@
* @param $a * @param $a
* @param $b * @param $b
* @return InstructionInterface * @return InstructionInterface
* @throws MalformedInstructionException * @noinspection PhpUnused
* @throws UnsupportedVariableType
*/ */
public static function modulo($a, $b): InstructionInterface public static function modulo($a, $b): InstructionInterface
{ {
@ -281,8 +265,7 @@
* @param $a * @param $a
* @param $b * @param $b
* @return InstructionInterface * @return InstructionInterface
* @throws MalformedInstructionException * @noinspection PhpUnused
* @throws UnsupportedVariableType
*/ */
public static function power($a, $b): InstructionInterface public static function power($a, $b): InstructionInterface
{ {
@ -300,8 +283,7 @@
* @param $a * @param $a
* @param $b * @param $b
* @return InstructionInterface * @return InstructionInterface
* @throws MalformedInstructionException * @noinspection PhpUnused
* @throws UnsupportedVariableType
*/ */
public static function greaterThan($a, $b): InstructionInterface public static function greaterThan($a, $b): InstructionInterface
{ {
@ -319,8 +301,7 @@
* @param $a * @param $a
* @param $b * @param $b
* @return InstructionInterface * @return InstructionInterface
* @throws MalformedInstructionException * @noinspection PhpUnused
* @throws UnsupportedVariableType
*/ */
public static function greaterThanOrEquals($a, $b): InstructionInterface public static function greaterThanOrEquals($a, $b): InstructionInterface
{ {
@ -338,8 +319,7 @@
* @param $a * @param $a
* @param $b * @param $b
* @return InstructionInterface * @return InstructionInterface
* @throws MalformedInstructionException * @noinspection PhpUnused
* @throws UnsupportedVariableType
*/ */
public static function lessThan($a, $b): InstructionInterface public static function lessThan($a, $b): InstructionInterface
{ {
@ -357,8 +337,7 @@
* @param $a * @param $a
* @param $b * @param $b
* @return InstructionInterface * @return InstructionInterface
* @throws MalformedInstructionException * @noinspection PhpUnused
* @throws UnsupportedVariableType
*/ */
public static function lessThanOrEquals($a, $b): InstructionInterface public static function lessThanOrEquals($a, $b): InstructionInterface
{ {

View file

@ -2,13 +2,9 @@
namespace RTEX\Classes; namespace RTEX\Classes;
use RTEX\Abstracts\InstructionType; use RTEX\Abstracts\VariableType;
use RTEX\Abstracts\VariableTypes; use RTEX\Exceptions\Runtime\TypeException;
use RTEX\Exceptions\Core\MalformedInstructionException;
use RTEX\Exceptions\Core\UnsupportedVariableType;
use RTEX\Interfaces\InstructionInterface; use RTEX\Interfaces\InstructionInterface;
use RTEX\Objects\Program\Instructions\GetVariable;
use RTEX\Objects\Program\Instructions\SetVariable;
class Utilities class Utilities
{ {
@ -17,65 +13,87 @@
* *
* @param $input * @param $input
* @return string * @return string
* @throws UnsupportedVariableType * @throws TypeException
*/ */
public static function determineType($input): string public static function determineType($input): string
{ {
if ($input instanceof InstructionInterface) if ($input instanceof InstructionInterface)
return VariableTypes::Instruction; return VariableType::Instruction;
if (is_string($input)) if (is_string($input))
return VariableTypes::String; return VariableType::String;
if (is_int($input)) if (is_int($input))
return VariableTypes::Integer; return VariableType::Integer;
if (is_float($input)) if (is_float($input))
return VariableTypes::Float; return VariableType::Float;
if (is_bool($input)) if (is_bool($input))
return VariableTypes::Boolean; return VariableType::Boolean;
if (is_array($input))
return VariableType::Array;
if (is_null($input)) 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 * Returns a supported variable type to an array representation
* or a single value if it's not an array or an instruction
* *
* @param $input * @param $input
* @return array|mixed * @return array|mixed
* @throws UnsupportedVariableType * @noinspection PhpMissingReturnTypeInspection
*/ */
public static function toArray($input) 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(), $output = [];
default => $input, 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 * This cannot be used as a method of serialization
* @return InstructionInterface *
* @throws MalformedInstructionException * @param $input
* @throws UnsupportedVariableType * @return string
*/ */
public static function constructInstruction(array $array): InstructionInterface public static function entityToString($input): string
{ {
if(!isset($array['type'])) /** @var InstructionInterface $input */
throw new MalformedInstructionException(sprintf('Instruction type not specified')); if($input instanceof InstructionInterface)
if(!isset($array['_'])) return (string)$input;
throw new MalformedInstructionException(sprintf('Instruction data not specified'));
switch($array['type']) if(is_array($input))
{ {
case InstructionType::GetVariable: $output = [];
return GetVariable::fromArray($array['_']); foreach($input as $key => $value)
case InstructionType::SetVariable: $output[$key] = self::entityToString($value);
return SetVariable::fromArray($array['_']); return json_encode($output, JSON_UNESCAPED_SLASHES);
default:
throw new MalformedInstructionException(sprintf('Instruction type "%s" not supported', $array['type']));
} }
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';
} }
} }

View file

@ -3,8 +3,6 @@
namespace RTEX\Classes; namespace RTEX\Classes;
use Exception; use Exception;
use RTEX\Abstracts\VariableTypes;
use RTEX\Interfaces\InstructionInterface;
class Validate class Validate
{ {