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): 95
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": "88f93c82-62c0-461a-8e8c-2d5edf9f27ed",
    "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": {
        "4186eef5-7878-421b-a4bf-9b5042cf7746": {
            "id": "4186eef5-7878-421b-a4bf-9b5042cf7746",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "free",
            "from": "shipping_decision",
            "to": "free_shipping"
        },
        "bfb8b05c-1f4a-440a-8033-734abc85abfb": {
            "id": "bfb8b05c-1f4a-440a-8033-734abc85abfb",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "ups",
            "from": "shipping_decision",
            "to": "ups_shipping"
        },
        "9f09d17d-e3d4-44d3-a2bb-6424cffd5d92": {
            "id": "9f09d17d-e3d4-44d3-a2bb-6424cffd5d92",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "shipping_decision"
        }
    },
    "outTransitions": {
        "shipping_decision": [
            "4186eef5-7878-421b-a4bf-9b5042cf7746",
            "bfb8b05c-1f4a-440a-8033-734abc85abfb"
        ]
    },
    "inTransitions": {
        "free_shipping": [
            "4186eef5-7878-421b-a4bf-9b5042cf7746"
        ],
        "ups_shipping": [
            "bfb8b05c-1f4a-440a-8033-734abc85abfb"
        ],
        "shipping_decision": [
            "9f09d17d-e3d4-44d3-a2bb-6424cffd5d92"
        ]
    },
    "tokens": {
        "6087cca6-9d14-47ef-aef5-2a70d81a5808": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "6087cca6-9d14-47ef-aef5-2a70d81a5808",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "543a7462-0629-47e6-8a89-25c4eb3a883b",
                    "transitionId": "9f09d17d-e3d4-44d3-a2bb-6424cffd5d92",
                    "weight": 1,
                    "state": "opened",
                    "time": 15559370945099
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "12dd7d32-37d1-4431-9460-c33e4616f035",
                    "transitionId": "9f09d17d-e3d4-44d3-a2bb-6424cffd5d92",
                    "weight": 1,
                    "state": "passed",
                    "time": 15559370945107
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "1543f1b4-fe72-4cd7-a3a0-d20bcc49691f",
                    "transitionId": "4186eef5-7878-421b-a4bf-9b5042cf7746",
                    "weight": 1,
                    "state": "opened",
                    "time": 15559370945111
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "9595b2f6-6d25-4dae-a1af-6c20974f522b",
                    "transitionId": "4186eef5-7878-421b-a4bf-9b5042cf7746",
                    "weight": 1,
                    "state": "passed",
                    "time": 15559370945118
                }
            ],
            "total_price": 95
        }
    }
}