PHP code example of skrip42 / node-processor

1. Go to this page and download the library: Download skrip42/node-processor library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

skrip42 / node-processor example snippets


use Skrip42\NodeProcessor\Process;

use Skrip42\NodeProcessor\Node\Other\ValueNode;
use Skrip42\NodeProcessor\Node\Logical\CompareNode;

$process = new Process; //create new process;

//create all necessary nodes:
// as syntax: process->createNode($nodeClassName, ...$values) : NodeAbstract
$valNode1 = $process->createNode(ValueNode::class, 1); //you can pass start parameters to the node if NodeName, $outputNode, $inputNodeName, $inputNode)
$process->linkNode('out1', $process->getBeginNode(), 'emit', $valNode1); // you can get begin and end node
                                                                        // from getBeginNode and getEndNode methods
$process->linkNode('out2', $process->getBeginNode(), 'emit', $valNode2);
$process->linkNode('out', $valNode1, 'in1', $compareNode);
$process->linkNode('out', $valNode2, 'in2', $compareNode);
$process->linkNode('more', $compareNode, 'more', $process->getEndNode()); // end node has dynamically input name
$process->linkNode('less', $compareNode, 'less', $process->getEndNode());
//You can always leave output nodes empty; can input be left blank determined by node policy

$result = $process->run(); // running process
$state = $process->getState(); // you also can get current process state for debug you script

[
    'status' => currentProcessStatus,
    'output' => [ .. array of end node inputs .. ]
    'requests' =>  [ .. array of requests .. ]
]

$serialized = serialize($process);
$process = unserialize($serialized);

$process = Process::build(
    [
        'node' => [
            'vn1' => [ValueNode::class, 1],
            'vn2' => [ValueNode::class, 5],
            'cn' => [CompareNode::class],
        ],
        'link' => [
            ['out1', 'begin', 'emit', 'vn1'], //begin is the predefined start node name
            ['out2', 'begin', 'emit', 'vn2'],
            ['out', 'vn1', 'in1', 'cn'],
            ['out', 'vn2', 'in2', 'cn'],
            ['less', 'cn', 'less', 'end'], //end is the predefined end node name
            ['more', 'vn2', 'more', 'end'], 
        ]
    ]
);

$process = Process::build(
    [
        'node' => [
            'vn1' => [ValueNode::class, '{$val1}'], // template syntax: {$valueName}
            'vn2' => [ValueNode::class, '{$val2}'],
            'cn' => [CompareNode::class],
        ],
        'link' => [
            ['out1', 'begin', 'emit', 'vn1'], //begin is the predefined start node name
            ['out2', 'begin', 'emit', 'vn2'],
            ['out', 'vn1', 'in1', 'cn'],
            ['out', 'vn2', 'in2', 'cn'],
            ['less', 'cn', 'less', 'end'], //end is the predefined end node name
            ['more', 'vn2', 'more', 'end'], 
        ]
    ],
    [ //value list
        'val1' => 1,
        'val2' => 5
    ]
);

[
    ['uniqIdOfRequest' => 'request data'],
    .......
]

$process->setResponse('uniqIdOfRequest', 'response data');

static propery $scheme = [
    'input' => [                //input declaration
        'optionalInput' => [       //optional input named as 'optionalInput'
        ],
        '' => '~in\d+~', //pattern for available input names
        'property' => [
            '       'outputWithoutDefaultData' => [ //output named as 'outputWithoutDefaultData'
        ]
    ],
    'user_output' => [  //dynamic output declarated
    ],                  //if pattern property is null, all names available
]

$value = $this->input['inputName']['data']; //get value from 'inputName' input
$this->output['outputName']['data'] = $value; //set value to 'outputName' output (not emitted)

$this->emit('outputName');
//or you can set value and output:
$this->emit('ouputName', $sumeValue);

$this->sendRequest($data, $id=null); //if $id == null $id get automaticaly uniq identifier

public function setResponse($id, $data)
{
    ....do somethind
}