Added base files, with the basic ability to add/remove variables from the Runtime.
This commit is contained in:
parent
aef02c4bff
commit
7e5207c45a
19 changed files with 906 additions and 0 deletions
94
src/RTEX/Objects/Program/Instructions/GetVariable.php
Normal file
94
src/RTEX/Objects/Program/Instructions/GetVariable.php
Normal file
|
@ -0,0 +1,94 @@
|
|||
<?php
|
||||
|
||||
namespace RTEX\Objects\Program\Instructions;
|
||||
|
||||
use RTEX\Abstracts\InstructionType;
|
||||
use RTEX\Classes\Utilities;
|
||||
use RTEX\Engine;
|
||||
use RTEX\Exceptions\Core\UnsupportedVariableType;
|
||||
use RTEX\Interfaces\InstructionInterface;
|
||||
|
||||
class GetVariable implements InstructionInterface
|
||||
{
|
||||
/**
|
||||
* The name of the variable to select
|
||||
*
|
||||
* @var string|integer|boolean|float|null|InstructionInterface|InstructionInterface[]
|
||||
*/
|
||||
private $Variable;
|
||||
|
||||
/**
|
||||
* Returns the type of instruction
|
||||
*
|
||||
* @return string
|
||||
* @see InstructionType
|
||||
*/
|
||||
public function getType(): string
|
||||
{
|
||||
return InstructionType::GetVariable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array representation of the object
|
||||
*
|
||||
* @return array
|
||||
* @throws UnsupportedVariableType
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'type' => $this->getType(),
|
||||
'_' => [
|
||||
'variable' => Utilities::toArray($this->Variable)
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns
|
||||
*
|
||||
* @return bool|float|int|InstructionInterface|string|null
|
||||
* @noinspection PhpMissingReturnTypeInspection
|
||||
*/
|
||||
public function getVariable()
|
||||
{
|
||||
return $this->Variable;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool|float|int|InstructionInterface|string|null $variable
|
||||
* @throws UnsupportedVariableType
|
||||
*/
|
||||
public function setVariable($variable): void
|
||||
{
|
||||
switch(Utilities::determineType($variable))
|
||||
{
|
||||
|
||||
|
||||
default:
|
||||
$this->Variable = $variable;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new GetVariable instruction from an array representation
|
||||
*
|
||||
* @throws UnsupportedVariableType
|
||||
*/
|
||||
public static function fromArray(array $data): InstructionInterface
|
||||
{
|
||||
$instruction = new GetVariable();
|
||||
$instruction->setVariable($data['variable'] ?? null);
|
||||
return $instruction;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function eval(Engine $engine)
|
||||
{
|
||||
return $engine->getEnvironment()->getRuntimeVariable(
|
||||
$engine->eval($this->Variable)
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue