Refactored GetVariable, added documentation
This commit is contained in:
parent
0cbb927066
commit
876621f618
2 changed files with 64 additions and 22 deletions
34
docs/get.md
Normal file
34
docs/get.md
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
# get
|
||||||
|
|
||||||
|
Gets an existing variable from the environment.
|
||||||
|
|
||||||
|
## Parameters
|
||||||
|
|
||||||
|
* name (`string`, `instruction`) - The name of the variable to get.
|
||||||
|
|
||||||
|
## Return
|
||||||
|
|
||||||
|
(`any`) - The value of the variable.
|
||||||
|
|
||||||
|
## Exceptions
|
||||||
|
|
||||||
|
* `EvaluationException` - If there was an error while evaluating one or more parameters.
|
||||||
|
* `TypeException` - If one or more parameters are not of the expected type.
|
||||||
|
* `NameException` - If the variable does not exist.
|
||||||
|
|
||||||
|
|
||||||
|
## Instruction Example
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"type": "get",
|
||||||
|
"_": {
|
||||||
|
"name": "foo"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Last Updated
|
||||||
|
|
||||||
|
Monday, December 29th, 2022.
|
||||||
|
Written by [Netkas](https://git.n64.cc/netkas)
|
|
@ -8,8 +8,10 @@
|
||||||
use RTEX\Classes\InstructionBuilder;
|
use RTEX\Classes\InstructionBuilder;
|
||||||
use RTEX\Classes\Utilities;
|
use RTEX\Classes\Utilities;
|
||||||
use RTEX\Engine;
|
use RTEX\Engine;
|
||||||
use RTEX\Exceptions\Core\MalformedInstructionException;
|
use RTEX\Exceptions\EvaluationException;
|
||||||
use RTEX\Exceptions\Core\UnsupportedVariableType;
|
use RTEX\Exceptions\InstructionException;
|
||||||
|
use RTEX\Exceptions\Runtime\NameException;
|
||||||
|
use RTEX\Exceptions\Runtime\TypeException;
|
||||||
use RTEX\Interfaces\InstructionInterface;
|
use RTEX\Interfaces\InstructionInterface;
|
||||||
|
|
||||||
class GetVariable implements InstructionInterface
|
class GetVariable implements InstructionInterface
|
||||||
|
@ -19,7 +21,7 @@
|
||||||
*
|
*
|
||||||
* @var mixed
|
* @var mixed
|
||||||
*/
|
*/
|
||||||
private $Variable;
|
private $VariableName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the type of instruction
|
* Returns the type of instruction
|
||||||
|
@ -36,33 +38,42 @@
|
||||||
* Returns
|
* Returns
|
||||||
*
|
*
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @noinspection PhpMissingReturnTypeInspection
|
|
||||||
* @noinspection PhpUnused
|
* @noinspection PhpUnused
|
||||||
*/
|
*/
|
||||||
public function getVariable()
|
public function getVariableName(): mixed
|
||||||
{
|
{
|
||||||
return $this->Variable;
|
return $this->VariableName;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $variable
|
* @param mixed $variable
|
||||||
* @throws MalformedInstructionException
|
* @throws InstructionException
|
||||||
* @throws UnsupportedVariableType
|
|
||||||
* @noinspection PhpMissingParamTypeInspection
|
* @noinspection PhpMissingParamTypeInspection
|
||||||
*/
|
*/
|
||||||
public function setVariable($variable): void
|
public function setVariableName($variable): void
|
||||||
{
|
{
|
||||||
$this->Variable = InstructionBuilder::fromRaw($variable);
|
$this->VariableName = InstructionBuilder::fromRaw($variable);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @param Engine $engine
|
||||||
* @throws UnsupportedVariableType
|
* @return mixed
|
||||||
|
* @throws EvaluationException
|
||||||
|
* @throws NameException
|
||||||
|
* @throws TypeException
|
||||||
*/
|
*/
|
||||||
public function eval(Engine $engine)
|
public function eval(Engine $engine): mixed
|
||||||
{
|
{
|
||||||
|
$variable = $engine->eval($this->VariableName);
|
||||||
|
|
||||||
|
if(!is_string($variable))
|
||||||
|
throw new TypeException(sprintf('Expected string, got %s', Utilities::getType($variable)));
|
||||||
|
|
||||||
|
if (!$engine->getEnvironment()->variableExists($variable))
|
||||||
|
throw new NameException("Variable '$variable' is not defined");
|
||||||
|
|
||||||
return $engine->getEnvironment()->getRuntimeVariable(
|
return $engine->getEnvironment()->getRuntimeVariable(
|
||||||
$engine->eval($this->Variable)
|
$engine->eval($this->VariableName)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,12 +81,11 @@
|
||||||
* Returns an array representation of the object
|
* Returns an array representation of the object
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
* @throws UnsupportedVariableType
|
|
||||||
*/
|
*/
|
||||||
public function toArray(): array
|
public function toArray(): array
|
||||||
{
|
{
|
||||||
return InstructionBuilder::toRaw(self::getType(), [
|
return InstructionBuilder::toRaw(self::getType(), [
|
||||||
'variable' => $this->Variable
|
'name' => $this->VariableName
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,26 +94,24 @@
|
||||||
*
|
*
|
||||||
* @param array $data
|
* @param array $data
|
||||||
* @return InstructionInterface
|
* @return InstructionInterface
|
||||||
* @throws MalformedInstructionException
|
* @throws InstructionException
|
||||||
* @throws UnsupportedVariableType
|
|
||||||
*/
|
*/
|
||||||
public static function fromArray(array $data): InstructionInterface
|
public static function fromArray(array $data): InstructionInterface
|
||||||
{
|
{
|
||||||
$instruction = new self();
|
$instruction = new self();
|
||||||
$instruction->setVariable($data['variable'] ?? null);
|
$instruction->setVariableName($data['name'] ?? null);
|
||||||
|
|
||||||
return $instruction;
|
return $instruction;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
* @throws UnsupportedVariableType
|
|
||||||
*/
|
*/
|
||||||
public function __toString(): string
|
public function __toString(): string
|
||||||
{
|
{
|
||||||
return sprintf(
|
return sprintf(
|
||||||
self::getType() . ' (variable: %s)',
|
self::getType() . ' (name: %s)',
|
||||||
Utilities::entityToString($this->Variable)
|
Utilities::entityToString($this->VariableName)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue