The example shows how two tasks could be executed one by one.
mkdir ~/pvm-sequence
cd ~/pvm-sequence
composer req formapro/pvm:0.4.x-dev makasim/values:0.5.x-dev
# create such file with the code below
php sequence.php
sequence.php
item price: 85
item price: 30
total price: 115
shipping price: 11.5
sequence.php
<?php
use Formapro\Pvm\DefaultBehaviorRegistry;
use Formapro\Pvm\ProcessEngine;
use Formapro\Pvm\Token;
use Formapro\Pvm\ProcessBuilder;
require_once __DIR__.'/vendor/autoload.php';
$process = (new ProcessBuilder())
->createNode('calculate_total_price', 'calculate_total_price')->end()
->createNode('calculate_shipping_price', 'calculate_shipping_price')->end()
->createTransition('calculate_total_price', 'calculate_shipping_price')->end()
->createStartTransition('calculate_total_price')->end()
->getProcess()
;
$engine = new ProcessEngine(new DefaultBehaviorRegistry([
'calculate_total_price' => function(Token $token) {
$totalPrice = 0;
foreach ($token->getValue('items') as $item) {
echo 'item price: '.$item['price'].PHP_EOL;
$totalPrice += $item['price'];
}
$token->setValue('total_price', $totalPrice);
echo 'total price: '.$totalPrice.PHP_EOL;
},
'calculate_shipping_price' => function(Token $token) {
$totalPrice = $token->getValue('total_price');
$token->setValue('shipping_price', $totalPrice * 0.1);
echo 'shipping price: '.$token->getValue('shipping_price').PHP_EOL;
},
]));
$token = $engine->createTokenFor($process->getStartTransition());
$token->setValue('items.0.price', rand(10, 100));
$token->setValue('items.1.price', rand(10, 100));
$engine->proceed($token);
{ "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Process.json", "id": "33959d05-54f5-4b4b-b17d-b3cd0ed0090a", "nodes": { "calculate_total_price": { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json", "id": "calculate_total_price", "behavior": "calculate_total_price" }, "calculate_shipping_price": { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json", "id": "calculate_shipping_price", "behavior": "calculate_shipping_price" } }, "transitions": { "f991b0f6-43fb-4bfb-a43f-3455d77e2047": { "id": "f991b0f6-43fb-4bfb-a43f-3455d77e2047", "weight": 1, "async": false, "active": true, "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json", "from": "calculate_total_price", "to": "calculate_shipping_price" }, "b61631bd-3489-431a-9437-8994f26445ce": { "id": "b61631bd-3489-431a-9437-8994f26445ce", "weight": 1, "async": false, "active": true, "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json", "to": "calculate_total_price" } }, "outTransitions": { "calculate_total_price": [ "f991b0f6-43fb-4bfb-a43f-3455d77e2047" ] }, "inTransitions": { "calculate_shipping_price": [ "f991b0f6-43fb-4bfb-a43f-3455d77e2047" ], "calculate_total_price": [ "b61631bd-3489-431a-9437-8994f26445ce" ] }, "tokens": { "a8cc0436-07d1-499e-b2db-aef54738015d": { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json", "id": "a8cc0436-07d1-499e-b2db-aef54738015d", "transitions": [ { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json", "id": "c991408a-b248-40e1-9c8f-6b5a596083a8", "transitionId": "b61631bd-3489-431a-9437-8994f26445ce", "weight": 1, "state": "opened", "time": 16107334746285 }, { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json", "id": "a9ae5b26-c12c-409f-864c-064fa28ff770", "transitionId": "b61631bd-3489-431a-9437-8994f26445ce", "weight": 1, "state": "passed", "time": 16107334746289 }, { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json", "id": "00581c2f-62b2-45f7-93ed-545ec8fde005", "transitionId": "f991b0f6-43fb-4bfb-a43f-3455d77e2047", "weight": 1, "state": "opened", "time": 16107334746291 }, { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json", "id": "5d7311d5-7011-49bf-8d1e-9a6a5daacb53", "transitionId": "f991b0f6-43fb-4bfb-a43f-3455d77e2047", "weight": 1, "state": "passed", "time": 16107334746296 } ], "items": [ { "price": 85 }, { "price": 30 } ], "total_price": 115, "shipping_price": 11.5 } } }