| @@ 17-70 (lines=54) @@ | ||
| 14 | /** |
|
| 15 | * Inserts a value into an array. |
|
| 16 | */ |
|
| 17 | final class InsertAction extends AbstractAction |
|
| 18 | { |
|
| 19 | /** The output action socket. */ |
|
| 20 | const SOCKET_OUTPUT = 'out'; |
|
| 21 | ||
| 22 | /** The array variable socket. */ |
|
| 23 | const SOCKET_ARRAY = 'array'; |
|
| 24 | ||
| 25 | /** The index variable socket. */ |
|
| 26 | const SOCKET_INDEX = 'index'; |
|
| 27 | ||
| 28 | /** The value variable socket. */ |
|
| 29 | const SOCKET_VALUE = 'value'; |
|
| 30 | ||
| 31 | /** |
|
| 32 | * Initializes a new instance of this class. |
|
| 33 | */ |
|
| 34 | public function __construct() |
|
| 35 | { |
|
| 36 | parent::__construct(); |
|
| 37 | ||
| 38 | $this->createSocket(self::SOCKET_OUTPUT); |
|
| 39 | $this->createSocket(self::SOCKET_ARRAY); |
|
| 40 | $this->createSocket(self::SOCKET_INDEX); |
|
| 41 | $this->createSocket(self::SOCKET_VALUE); |
|
| 42 | } |
|
| 43 | ||
| 44 | /** |
|
| 45 | * Executes the node's logic. |
|
| 46 | */ |
|
| 47 | public function execute() |
|
| 48 | { |
|
| 49 | $array = $this->getSocket(self::SOCKET_ARRAY)->getNode(0); |
|
| 50 | if ($array === null) { |
|
| 51 | throw new \InvalidArgumentException('No array variable provided.'); |
|
| 52 | } |
|
| 53 | ||
| 54 | $index = $this->getSocket(self::SOCKET_INDEX)->getNode(0); |
|
| 55 | if ($index === null || $index->getValue() === null) { |
|
| 56 | throw new \InvalidArgumentException('No index variable provided.'); |
|
| 57 | } |
|
| 58 | ||
| 59 | $value = $this->getSocket(self::SOCKET_VALUE)->getNode(0); |
|
| 60 | if ($value === null || $value->getValue() === null) { |
|
| 61 | throw new \InvalidArgumentException('No value variable provided.'); |
|
| 62 | } |
|
| 63 | ||
| 64 | $arrayValue = $array->getValue(); |
|
| 65 | array_splice($arrayValue, $index->getValue(), 0, $value->getValue()); |
|
| 66 | $array->setValue($arrayValue); |
|
| 67 | ||
| 68 | $this->activate(self::SOCKET_OUTPUT); |
|
| 69 | } |
|
| 70 | } |
|
| 71 | ||
| @@ 17-79 (lines=63) @@ | ||
| 14 | /** |
|
| 15 | * The base final class for simple math actions. |
|
| 16 | */ |
|
| 17 | abstract class AbstractAction extends BaseAbstractAction |
|
| 18 | { |
|
| 19 | /** The output action socket. */ |
|
| 20 | const SOCKET_OUTPUT = 'out'; |
|
| 21 | ||
| 22 | /** The left variable socket. */ |
|
| 23 | const SOCKET_LFT = 'lft'; |
|
| 24 | ||
| 25 | /** The right variable socket. */ |
|
| 26 | const SOCKET_RGT = 'rgt'; |
|
| 27 | ||
| 28 | /** The result variable socket. */ |
|
| 29 | const SOCKET_RESULT = 'result'; |
|
| 30 | ||
| 31 | /** |
|
| 32 | * Initializes a new instance of this class. |
|
| 33 | */ |
|
| 34 | public function __construct() |
|
| 35 | { |
|
| 36 | parent::__construct(); |
|
| 37 | ||
| 38 | $this->createSocket(self::SOCKET_OUTPUT); |
|
| 39 | $this->createSocket(self::SOCKET_LFT); |
|
| 40 | $this->createSocket(self::SOCKET_RGT); |
|
| 41 | $this->createSocket(self::SOCKET_RESULT); |
|
| 42 | } |
|
| 43 | ||
| 44 | /** |
|
| 45 | * Executes the node's logic. |
|
| 46 | */ |
|
| 47 | public function execute() |
|
| 48 | { |
|
| 49 | $lft = $this->getSocket(self::SOCKET_LFT)->getNode(0); |
|
| 50 | if ($lft === null || $lft->getValue() === null) { |
|
| 51 | throw new \InvalidArgumentException('No left variable provided.'); |
|
| 52 | } |
|
| 53 | ||
| 54 | $rgt = $this->getSocket(self::SOCKET_RGT)->getNode(0); |
|
| 55 | if ($rgt === null || $rgt->getValue() === null) { |
|
| 56 | throw new \InvalidArgumentException('No right variable provided.'); |
|
| 57 | } |
|
| 58 | ||
| 59 | $result = $this->getSocket(self::SOCKET_RESULT)->getNode(0); |
|
| 60 | if ($result === null) { |
|
| 61 | throw new \InvalidArgumentException('No result variable provided.'); |
|
| 62 | } |
|
| 63 | ||
| 64 | $calculatedValue = $this->calculateValue($lft->getValue(), $rgt->getValue()); |
|
| 65 | ||
| 66 | $result->setValue($calculatedValue); |
|
| 67 | ||
| 68 | $this->activate(self::SOCKET_OUTPUT); |
|
| 69 | } |
|
| 70 | ||
| 71 | /** |
|
| 72 | * The method that calculates the value. |
|
| 73 | * |
|
| 74 | * @param double|float|int $lft The left value. |
|
| 75 | * @param double|float|int $rgt The right value. |
|
| 76 | * @return double|float|int Returns the calculated value. |
|
| 77 | */ |
|
| 78 | abstract protected function calculateValue($lft, $rgt); |
|
| 79 | } |
|
| 80 | ||