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): 27
ups_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": "90909491-da74-4759-8c30-95865f3903fe",
    "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": {
        "5e9d2235-d346-4286-a362-ea576a5b2158": {
            "id": "5e9d2235-d346-4286-a362-ea576a5b2158",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "free",
            "from": "shipping_decision",
            "to": "free_shipping"
        },
        "aab94e76-a8eb-4962-a5ee-48fbf6854d43": {
            "id": "aab94e76-a8eb-4962-a5ee-48fbf6854d43",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "ups",
            "from": "shipping_decision",
            "to": "ups_shipping"
        },
        "06241784-38ca-46e3-a9d6-7282b553e6a9": {
            "id": "06241784-38ca-46e3-a9d6-7282b553e6a9",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "shipping_decision"
        }
    },
    "outTransitions": {
        "shipping_decision": [
            "5e9d2235-d346-4286-a362-ea576a5b2158",
            "aab94e76-a8eb-4962-a5ee-48fbf6854d43"
        ]
    },
    "inTransitions": {
        "free_shipping": [
            "5e9d2235-d346-4286-a362-ea576a5b2158"
        ],
        "ups_shipping": [
            "aab94e76-a8eb-4962-a5ee-48fbf6854d43"
        ],
        "shipping_decision": [
            "06241784-38ca-46e3-a9d6-7282b553e6a9"
        ]
    },
    "tokens": {
        "202d0dc9-572d-47d1-b2fd-3acffc9ed05a": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "202d0dc9-572d-47d1-b2fd-3acffc9ed05a",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "cbaee8e2-aac1-433c-bf1d-9546c58c214d",
                    "transitionId": "06241784-38ca-46e3-a9d6-7282b553e6a9",
                    "weight": 1,
                    "state": "opened",
                    "time": 15450781530357
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "5104e9e1-3188-4f02-9280-f02fcc4ae2af",
                    "transitionId": "06241784-38ca-46e3-a9d6-7282b553e6a9",
                    "weight": 1,
                    "state": "passed",
                    "time": 15450781530361
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "0d5d9639-a9ac-4bb2-b272-04ca97a8724e",
                    "transitionId": "aab94e76-a8eb-4962-a5ee-48fbf6854d43",
                    "weight": 1,
                    "state": "opened",
                    "time": 15450781530363
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "e344e412-7d76-4832-9474-973e836a3149",
                    "transitionId": "aab94e76-a8eb-4962-a5ee-48fbf6854d43",
                    "weight": 1,
                    "state": "passed",
                    "time": 15450781530367
                }
            ],
            "total_price": 27
        }
    }
}