Refactored Classes
This commit is contained in:
parent
05428dca07
commit
6a077bb5a1
3 changed files with 68 additions and 73 deletions
|
@ -1,10 +1,8 @@
|
|||
<?php
|
||||
|
||||
namespace RTEX\Classes;
|
||||
|
||||
use RTEX\Abstracts\InstructionType;
|
||||
use RTEX\Exceptions\Core\MalformedInstructionException;
|
||||
use RTEX\Exceptions\Core\UnsupportedVariableType;
|
||||
use RTEX\Exceptions\InstructionException;
|
||||
use RTEX\Interfaces\InstructionInterface;
|
||||
use RTEX\Objects\Program\Instructions\ArrayGet;
|
||||
use RTEX\Objects\Program\Instructions\Divide;
|
||||
|
@ -29,8 +27,7 @@
|
|||
*
|
||||
* @param $value
|
||||
* @return array|mixed|InstructionInterface|null
|
||||
* @throws UnsupportedVariableType
|
||||
* @throws MalformedInstructionException
|
||||
* @throws InstructionException
|
||||
* @noinspection PhpMissingReturnTypeInspection
|
||||
*/
|
||||
public static function fromRaw($value)
|
||||
|
@ -69,9 +66,10 @@
|
|||
InstructionType::ArrayGet => 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
|
||||
{
|
||||
|
|
|
@ -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';
|
||||
}
|
||||
|
||||
}
|
|
@ -3,8 +3,6 @@
|
|||
namespace RTEX\Classes;
|
||||
|
||||
use Exception;
|
||||
use RTEX\Abstracts\VariableTypes;
|
||||
use RTEX\Interfaces\InstructionInterface;
|
||||
|
||||
class Validate
|
||||
{
|
||||
|
|
Loading…
Add table
Reference in a new issue