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": "abe672fc-7ecb-460e-9919-85f702adc657",
    "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": {
        "557882f7-2692-42b1-b770-db57a4bf3e75": {
            "id": "557882f7-2692-42b1-b770-db57a4bf3e75",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "free",
            "from": "shipping_decision",
            "to": "free_shipping"
        },
        "ea4608c7-e792-44df-9140-6f547a69e48a": {
            "id": "ea4608c7-e792-44df-9140-6f547a69e48a",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "ups",
            "from": "shipping_decision",
            "to": "ups_shipping"
        },
        "b676d31d-2268-4057-9a20-95e5a0df0ba1": {
            "id": "b676d31d-2268-4057-9a20-95e5a0df0ba1",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "shipping_decision"
        }
    },
    "outTransitions": {
        "shipping_decision": [
            "557882f7-2692-42b1-b770-db57a4bf3e75",
            "ea4608c7-e792-44df-9140-6f547a69e48a"
        ]
    },
    "inTransitions": {
        "free_shipping": [
            "557882f7-2692-42b1-b770-db57a4bf3e75"
        ],
        "ups_shipping": [
            "ea4608c7-e792-44df-9140-6f547a69e48a"
        ],
        "shipping_decision": [
            "b676d31d-2268-4057-9a20-95e5a0df0ba1"
        ]
    },
    "tokens": {
        "a936829d-6392-4297-9f55-f0fa8a4b7369": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "a936829d-6392-4297-9f55-f0fa8a4b7369",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "2c14f9ce-5cc9-4700-bae8-db155172f922",
                    "transitionId": "b676d31d-2268-4057-9a20-95e5a0df0ba1",
                    "weight": 1,
                    "state": "opened",
                    "time": 15508414705547
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "fea1a0eb-60a5-4232-8ed8-424e440b58aa",
                    "transitionId": "b676d31d-2268-4057-9a20-95e5a0df0ba1",
                    "weight": 1,
                    "state": "passed",
                    "time": 15508414705552
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "8a3d20ee-13bc-4ace-af25-75dec477aaaa",
                    "transitionId": "557882f7-2692-42b1-b770-db57a4bf3e75",
                    "weight": 1,
                    "state": "opened",
                    "time": 15508414705555
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "6eb7689e-9bb2-4712-933c-a062ee79d74d",
                    "transitionId": "557882f7-2692-42b1-b770-db57a4bf3e75",
                    "weight": 1,
                    "state": "passed",
                    "time": 15508414705560
                }
            ],
            "total_price": 89
        }
    }
}