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): 32
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": "4a5ac278-0536-4751-ba36-7d002ca54727",
    "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": {
        "c3c1fac0-a68f-437f-9cf4-f7a5595f9817": {
            "id": "c3c1fac0-a68f-437f-9cf4-f7a5595f9817",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "free",
            "from": "shipping_decision",
            "to": "free_shipping"
        },
        "f88dd41a-6753-4191-899d-f15a8bda0a13": {
            "id": "f88dd41a-6753-4191-899d-f15a8bda0a13",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "ups",
            "from": "shipping_decision",
            "to": "ups_shipping"
        },
        "87f2af7d-fce6-48ab-aaff-de2f2ba67385": {
            "id": "87f2af7d-fce6-48ab-aaff-de2f2ba67385",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "shipping_decision"
        }
    },
    "outTransitions": {
        "shipping_decision": [
            "c3c1fac0-a68f-437f-9cf4-f7a5595f9817",
            "f88dd41a-6753-4191-899d-f15a8bda0a13"
        ]
    },
    "inTransitions": {
        "free_shipping": [
            "c3c1fac0-a68f-437f-9cf4-f7a5595f9817"
        ],
        "ups_shipping": [
            "f88dd41a-6753-4191-899d-f15a8bda0a13"
        ],
        "shipping_decision": [
            "87f2af7d-fce6-48ab-aaff-de2f2ba67385"
        ]
    },
    "tokens": {
        "cba2c75a-1543-4c4f-ba5a-753c4ec4bcf8": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "cba2c75a-1543-4c4f-ba5a-753c4ec4bcf8",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "c3ec4f81-3854-4f8a-9f57-e6d2f826d53c",
                    "transitionId": "87f2af7d-fce6-48ab-aaff-de2f2ba67385",
                    "weight": 1,
                    "state": "opened",
                    "time": 15757131897687
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "85f369fa-eb5a-445c-bb6b-cd9378bb1599",
                    "transitionId": "87f2af7d-fce6-48ab-aaff-de2f2ba67385",
                    "weight": 1,
                    "state": "passed",
                    "time": 15757131897691
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "feb0c1d0-034e-4dba-bc37-4824cffe236f",
                    "transitionId": "f88dd41a-6753-4191-899d-f15a8bda0a13",
                    "weight": 1,
                    "state": "opened",
                    "time": 15757131897694
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "d87793ab-b475-4503-9a73-6df62d2ecd3d",
                    "transitionId": "f88dd41a-6753-4191-899d-f15a8bda0a13",
                    "weight": 1,
                    "state": "passed",
                    "time": 15757131897698
                }
            ],
            "total_price": 32
        }
    }
}