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: 75
item price: 74
total price: 149
shipping price: 14.9
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": "e975e320-5d96-44b9-a9f7-8b348f63d316", "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": { "48d4f40b-59b8-4d6d-a9bc-6357921245c2": { "id": "48d4f40b-59b8-4d6d-a9bc-6357921245c2", "weight": 1, "async": false, "active": true, "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json", "from": "calculate_total_price", "to": "calculate_shipping_price" }, "ccecf9df-b5c0-45dc-89aa-525fa9264bdb": { "id": "ccecf9df-b5c0-45dc-89aa-525fa9264bdb", "weight": 1, "async": false, "active": true, "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json", "to": "calculate_total_price" } }, "outTransitions": { "calculate_total_price": [ "48d4f40b-59b8-4d6d-a9bc-6357921245c2" ] }, "inTransitions": { "calculate_shipping_price": [ "48d4f40b-59b8-4d6d-a9bc-6357921245c2" ], "calculate_total_price": [ "ccecf9df-b5c0-45dc-89aa-525fa9264bdb" ] }, "tokens": { "78fdbb2f-f4ca-4315-a271-105123387c26": { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json", "id": "78fdbb2f-f4ca-4315-a271-105123387c26", "transitions": [ { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json", "id": "1f5f0b77-ffa0-419a-96ce-42082cead23d", "transitionId": "ccecf9df-b5c0-45dc-89aa-525fa9264bdb", "weight": 1, "state": "opened", "time": 15757103283502 }, { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json", "id": "9b031a3e-f395-44a2-adcf-109851e754ee", "transitionId": "ccecf9df-b5c0-45dc-89aa-525fa9264bdb", "weight": 1, "state": "passed", "time": 15757103283507 }, { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json", "id": "3aefb925-79f6-47f0-b03b-13fb16cb9038", "transitionId": "48d4f40b-59b8-4d6d-a9bc-6357921245c2", "weight": 1, "state": "opened", "time": 15757103283510 }, { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json", "id": "1fd1ab16-917a-4c53-b1da-3c2eb8ea2cff", "transitionId": "48d4f40b-59b8-4d6d-a9bc-6357921245c2", "weight": 1, "state": "passed", "time": 15757103283514 } ], "items": [ { "price": 75 }, { "price": 74 } ], "total_price": 149, "shipping_price": 14.9 } } }