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): 80
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": "051f50f0-3daa-48cb-8571-ab17f572e084",
    "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": {
        "5e04c361-28e4-4ee9-b563-936859f98084": {
            "id": "5e04c361-28e4-4ee9-b563-936859f98084",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "free",
            "from": "shipping_decision",
            "to": "free_shipping"
        },
        "f91d1f06-d37b-4f48-9121-c6906cc0e19f": {
            "id": "f91d1f06-d37b-4f48-9121-c6906cc0e19f",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "ups",
            "from": "shipping_decision",
            "to": "ups_shipping"
        },
        "42f25616-24e4-462a-bd04-05b335c2e7fc": {
            "id": "42f25616-24e4-462a-bd04-05b335c2e7fc",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "shipping_decision"
        }
    },
    "outTransitions": {
        "shipping_decision": [
            "5e04c361-28e4-4ee9-b563-936859f98084",
            "f91d1f06-d37b-4f48-9121-c6906cc0e19f"
        ]
    },
    "inTransitions": {
        "free_shipping": [
            "5e04c361-28e4-4ee9-b563-936859f98084"
        ],
        "ups_shipping": [
            "f91d1f06-d37b-4f48-9121-c6906cc0e19f"
        ],
        "shipping_decision": [
            "42f25616-24e4-462a-bd04-05b335c2e7fc"
        ]
    },
    "tokens": {
        "87c47232-1f8f-4a28-bb41-866d9a82024b": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "87c47232-1f8f-4a28-bb41-866d9a82024b",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "e3d04d06-af9b-4f29-97fc-056a11fdeb1b",
                    "transitionId": "42f25616-24e4-462a-bd04-05b335c2e7fc",
                    "weight": 1,
                    "state": "opened",
                    "time": 15479191428310
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "26f7eb5b-26f9-479e-aec0-f7d7af21b07c",
                    "transitionId": "42f25616-24e4-462a-bd04-05b335c2e7fc",
                    "weight": 1,
                    "state": "passed",
                    "time": 15479191428315
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "45570039-e9e9-499a-a8c4-b4e73272f21f",
                    "transitionId": "5e04c361-28e4-4ee9-b563-936859f98084",
                    "weight": 1,
                    "state": "opened",
                    "time": 15479191428318
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "8e833f1b-0280-44cc-85f4-7de7d08dde7f",
                    "transitionId": "5e04c361-28e4-4ee9-b563-936859f98084",
                    "weight": 1,
                    "state": "passed",
                    "time": 15479191428324
                }
            ],
            "total_price": 80
        }
    }
}