Refactored \RTEX\Objects\Program\Instructions > ArrayGet

This commit is contained in:
Netkas 2022-12-26 16:26:44 -05:00
parent 6a077bb5a1
commit b6ae2efc82

View file

@ -8,8 +8,9 @@
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\KeyException;
use RTEX\Interfaces\InstructionInterface; use RTEX\Interfaces\InstructionInterface;
class ArrayGet implements InstructionInterface class ArrayGet implements InstructionInterface
@ -51,8 +52,7 @@
/** /**
* @param mixed $variable * @param mixed $variable
* @throws UnsupportedVariableType * @throws InstructionException
* @throws MalformedInstructionException
* @noinspection PhpMissingParamTypeInspection * @noinspection PhpMissingParamTypeInspection
*/ */
public function setArray($variable): void public function setArray($variable): void
@ -71,8 +71,7 @@
/** /**
* @param mixed $value * @param mixed $value
* @throws MalformedInstructionException * @throws InstructionException
* @throws UnsupportedVariableType
* @noinspection PhpMissingParamTypeInspection * @noinspection PhpMissingParamTypeInspection
*/ */
public function setValue($value): void public function setValue($value): void
@ -84,7 +83,6 @@
* 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
{ {
@ -97,8 +95,7 @@
/** /**
* @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
{ {
@ -111,12 +108,12 @@
/** /**
* @param Engine $engine * @param Engine $engine
* @return void * @return mixed
* @throws UnsupportedVariableType * @throws EvaluationException
* @throws KeyException
*/ */
public function eval(Engine $engine) public function eval(Engine $engine): mixed
{ {
$queryParts = explode('.', $engine->eval($this->Value)); $queryParts = explode('.', $engine->eval($this->Value));
$value = $engine->getEnvironment()->getRuntimeVariable($engine->eval($this->Array)); $value = $engine->getEnvironment()->getRuntimeVariable($engine->eval($this->Array));
@ -124,12 +121,10 @@
{ {
if (is_array($value) && array_key_exists($queryPart, $value)) if (is_array($value) && array_key_exists($queryPart, $value))
{ {
$value = $value[$queryPart]; return $value[$queryPart];
}
else
{
return null;
} }
throw new KeyException(sprintf('Key "%s" does not exist in array', $queryPart));
} }
return $value; return $value;
@ -137,7 +132,6 @@
/** /**
* @inheritDoc * @inheritDoc
* @throws UnsupportedVariableType
*/ */
public function __toString(): string public function __toString(): string
{ {