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): 89
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": "120756b9-c9e0-4492-8fff-e253a7011582",
    "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": {
        "fd0be724-96c2-47ae-ad93-b7b5686831e9": {
            "id": "fd0be724-96c2-47ae-ad93-b7b5686831e9",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "free",
            "from": "shipping_decision",
            "to": "free_shipping"
        },
        "f1572dbb-676b-465d-a11d-b6c7d1d0193e": {
            "id": "f1572dbb-676b-465d-a11d-b6c7d1d0193e",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "ups",
            "from": "shipping_decision",
            "to": "ups_shipping"
        },
        "af89d90d-a8fd-4ac5-81c5-be14e64a6c95": {
            "id": "af89d90d-a8fd-4ac5-81c5-be14e64a6c95",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "shipping_decision"
        }
    },
    "outTransitions": {
        "shipping_decision": [
            "fd0be724-96c2-47ae-ad93-b7b5686831e9",
            "f1572dbb-676b-465d-a11d-b6c7d1d0193e"
        ]
    },
    "inTransitions": {
        "free_shipping": [
            "fd0be724-96c2-47ae-ad93-b7b5686831e9"
        ],
        "ups_shipping": [
            "f1572dbb-676b-465d-a11d-b6c7d1d0193e"
        ],
        "shipping_decision": [
            "af89d90d-a8fd-4ac5-81c5-be14e64a6c95"
        ]
    },
    "tokens": {
        "c969b1ce-03b9-47e2-a574-e1ecf0f8c767": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "c969b1ce-03b9-47e2-a574-e1ecf0f8c767",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "3fbd4b3c-29de-4824-881e-7bb59557dda4",
                    "transitionId": "af89d90d-a8fd-4ac5-81c5-be14e64a6c95",
                    "weight": 1,
                    "state": "opened",
                    "time": 15606703966747
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "445b2aad-4561-42bf-80ac-be50b0201f41",
                    "transitionId": "af89d90d-a8fd-4ac5-81c5-be14e64a6c95",
                    "weight": 1,
                    "state": "passed",
                    "time": 15606703966754
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "ae283998-78a1-4c91-9664-0b32ab7457a4",
                    "transitionId": "fd0be724-96c2-47ae-ad93-b7b5686831e9",
                    "weight": 1,
                    "state": "opened",
                    "time": 15606703966758
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "7c89510b-64f7-4aa3-a9f7-fa238079bc18",
                    "transitionId": "fd0be724-96c2-47ae-ad93-b7b5686831e9",
                    "weight": 1,
                    "state": "passed",
                    "time": 15606703966765
                }
            ],
            "total_price": 89
        }
    }
}