1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* NextFlow (http://github.com/nextflow) |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/nextflow/nextflow-php for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2014-2016 NextFlow (http://github.com/nextflow) |
7
|
|
|
* @license https://raw.github.com/nextflow/nextflow-php/master/LICENSE MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace NextFlow\Arrays\Action; |
11
|
|
|
|
12
|
|
|
use NextFlow\Core\Action\AbstractAction; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Inserts a value into an array. |
16
|
|
|
*/ |
17
|
|
View Code Duplication |
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
|
|
|
|