Condition

The example shows how to go only part of out transitions.

For this, out transitions should have names and a condition behavior returns one of them.

Setup


mkdir ~/pvm-condition
cd ~/pvm-condition
composer req formapro/pvm:0.4.x-dev makasim/values:0.5.x-dev

# create such file with the code below
php condition.php

Result

condition.php

Total price (free if >= 50): 72
free_shipping

condition.php


<?php
use Formapro\Pvm\DefaultBehaviorRegistry;
use 
Formapro\Pvm\ProcessBuilder;
use 
Formapro\Pvm\ProcessEngine;
use 
Formapro\Pvm\Token;

require_once 
__DIR__.'/vendor/autoload.php';

$process = (new ProcessBuilder())
    ->
createNode('shipping_decision''shipping_decision')->end()
    ->
createNode('free_shipping''print_label')->end()
    ->
createNode('ups_shipping''print_label')->end()

    ->
createTransition('shipping_decision''free_shipping''free')->end()
    ->
createTransition('shipping_decision''ups_shipping''ups')->end()
    ->
createStartTransition('shipping_decision')->end()

    ->
getProcess()
;

$engine = new ProcessEngine(new DefaultBehaviorRegistry([
    
'print_label' => function(Token $token) {
        echo 
$token->getTo()->getId().PHP_EOL;
    },
    
'shipping_decision' => function(Token $token) {
        echo 
'Total price (free if >= 50): '$token->getValue('total_price').PHP_EOL;

        return 
$token->getValue('total_price') > 50 'free' 'ups';
    }
]));

$token $engine->createTokenFor($process->getStartTransition());
$token->setValue('total_price'rand(10100));

$engine->proceed($token);
{
    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Process.json",
    "id": "58d3f34a-2c5c-48ec-9446-2789eada2664",
    "nodes": {
        "shipping_decision": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json",
            "id": "shipping_decision",
            "behavior": "shipping_decision"
        },
        "free_shipping": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json",
            "id": "free_shipping",
            "behavior": "print_label"
        },
        "ups_shipping": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json",
            "id": "ups_shipping",
            "behavior": "print_label"
        }
    },
    "transitions": {
        "2576b4f3-32af-4fbc-adef-0edb2dc90afa": {
            "id": "2576b4f3-32af-4fbc-adef-0edb2dc90afa",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "free",
            "from": "shipping_decision",
            "to": "free_shipping"
        },
        "da4ad0c9-ff2b-469a-9fc5-d5ffcaa0e0ee": {
            "id": "da4ad0c9-ff2b-469a-9fc5-d5ffcaa0e0ee",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "ups",
            "from": "shipping_decision",
            "to": "ups_shipping"
        },
        "9361046e-fd15-43d4-a6c5-e138ed5ca0e9": {
            "id": "9361046e-fd15-43d4-a6c5-e138ed5ca0e9",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "shipping_decision"
        }
    },
    "outTransitions": {
        "shipping_decision": [
            "2576b4f3-32af-4fbc-adef-0edb2dc90afa",
            "da4ad0c9-ff2b-469a-9fc5-d5ffcaa0e0ee"
        ]
    },
    "inTransitions": {
        "free_shipping": [
            "2576b4f3-32af-4fbc-adef-0edb2dc90afa"
        ],
        "ups_shipping": [
            "da4ad0c9-ff2b-469a-9fc5-d5ffcaa0e0ee"
        ],
        "shipping_decision": [
            "9361046e-fd15-43d4-a6c5-e138ed5ca0e9"
        ]
    },
    "tokens": {
        "218ee04f-b080-4c26-b9b7-d23cd62b81a4": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "218ee04f-b080-4c26-b9b7-d23cd62b81a4",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "d1befdac-a3ce-422c-937e-9b9f91f6e4c9",
                    "transitionId": "9361046e-fd15-43d4-a6c5-e138ed5ca0e9",
                    "weight": 1,
                    "state": "opened",
                    "time": 15715001292036
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "2a3fb3f4-dee9-436c-8a49-56f78d7c8468",
                    "transitionId": "9361046e-fd15-43d4-a6c5-e138ed5ca0e9",
                    "weight": 1,
                    "state": "passed",
                    "time": 15715001292040
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "31fa3931-1386-44bf-afdf-4f17d4879c37",
                    "transitionId": "2576b4f3-32af-4fbc-adef-0edb2dc90afa",
                    "weight": 1,
                    "state": "opened",
                    "time": 15715001292043
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "fa0d5594-4048-4ef1-b03f-dc55914c6d22",
                    "transitionId": "2576b4f3-32af-4fbc-adef-0edb2dc90afa",
                    "weight": 1,
                    "state": "passed",
                    "time": 15715001292047
                }
            ],
            "total_price": 72
        }
    }
}