From 876621f61843c923788c13564f4347f007f86867 Mon Sep 17 00:00:00 2001 From: Netkas Date: Thu, 29 Dec 2022 14:21:38 -0500 Subject: [PATCH] Refactored GetVariable, added documentation --- docs/get.md | 34 ++++++++++++ .../Program/Instructions/GetVariable.php | 52 +++++++++++-------- 2 files changed, 64 insertions(+), 22 deletions(-) create mode 100644 docs/get.md diff --git a/docs/get.md b/docs/get.md new file mode 100644 index 0000000..2662fc9 --- /dev/null +++ b/docs/get.md @@ -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) \ No newline at end of file diff --git a/src/RTEX/Objects/Program/Instructions/GetVariable.php b/src/RTEX/Objects/Program/Instructions/GetVariable.php index f7583ca..0b958f6 100644 --- a/src/RTEX/Objects/Program/Instructions/GetVariable.php +++ b/src/RTEX/Objects/Program/Instructions/GetVariable.php @@ -8,8 +8,10 @@ use RTEX\Classes\InstructionBuilder; use RTEX\Classes\Utilities; use RTEX\Engine; - use RTEX\Exceptions\Core\MalformedInstructionException; - use RTEX\Exceptions\Core\UnsupportedVariableType; + use RTEX\Exceptions\EvaluationException; + use RTEX\Exceptions\InstructionException; + use RTEX\Exceptions\Runtime\NameException; + use RTEX\Exceptions\Runtime\TypeException; use RTEX\Interfaces\InstructionInterface; class GetVariable implements InstructionInterface @@ -19,7 +21,7 @@ * * @var mixed */ - private $Variable; + private $VariableName; /** * Returns the type of instruction @@ -36,33 +38,42 @@ * Returns * * @return mixed - * @noinspection PhpMissingReturnTypeInspection * @noinspection PhpUnused */ - public function getVariable() + public function getVariableName(): mixed { - return $this->Variable; + return $this->VariableName; } /** * @param mixed $variable - * @throws MalformedInstructionException - * @throws UnsupportedVariableType + * @throws InstructionException * @noinspection PhpMissingParamTypeInspection */ - public function setVariable($variable): void + public function setVariableName($variable): void { - $this->Variable = InstructionBuilder::fromRaw($variable); + $this->VariableName = InstructionBuilder::fromRaw($variable); } /** - * @inheritDoc - * @throws UnsupportedVariableType + * @param Engine $engine + * @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( - $engine->eval($this->Variable) + $engine->eval($this->VariableName) ); } @@ -70,12 +81,11 @@ * Returns an array representation of the object * * @return array - * @throws UnsupportedVariableType */ public function toArray(): array { return InstructionBuilder::toRaw(self::getType(), [ - 'variable' => $this->Variable + 'name' => $this->VariableName ]); } @@ -84,26 +94,24 @@ * * @param array $data * @return InstructionInterface - * @throws MalformedInstructionException - * @throws UnsupportedVariableType + * @throws InstructionException */ public static function fromArray(array $data): InstructionInterface { $instruction = new self(); - $instruction->setVariable($data['variable'] ?? null); + $instruction->setVariableName($data['name'] ?? null); return $instruction; } /** * @inheritDoc - * @throws UnsupportedVariableType */ public function __toString(): string { return sprintf( - self::getType() . ' (variable: %s)', - Utilities::entityToString($this->Variable) + self::getType() . ' (name: %s)', + Utilities::entityToString($this->VariableName) ); } } \ No newline at end of file