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): 16
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": "e67fdd5a-1d48-45f4-b447-f64652fc8a10",
    "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": {
        "7836e4a6-d295-46cb-8471-90c3aeb00f79": {
            "id": "7836e4a6-d295-46cb-8471-90c3aeb00f79",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "free",
            "from": "shipping_decision",
            "to": "free_shipping"
        },
        "3f40edad-163a-4176-99b9-f29d2faf2bc6": {
            "id": "3f40edad-163a-4176-99b9-f29d2faf2bc6",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "ups",
            "from": "shipping_decision",
            "to": "ups_shipping"
        },
        "6263798f-5037-47aa-9ccb-e89d20ac8f5d": {
            "id": "6263798f-5037-47aa-9ccb-e89d20ac8f5d",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "shipping_decision"
        }
    },
    "outTransitions": {
        "shipping_decision": [
            "7836e4a6-d295-46cb-8471-90c3aeb00f79",
            "3f40edad-163a-4176-99b9-f29d2faf2bc6"
        ]
    },
    "inTransitions": {
        "free_shipping": [
            "7836e4a6-d295-46cb-8471-90c3aeb00f79"
        ],
        "ups_shipping": [
            "3f40edad-163a-4176-99b9-f29d2faf2bc6"
        ],
        "shipping_decision": [
            "6263798f-5037-47aa-9ccb-e89d20ac8f5d"
        ]
    },
    "tokens": {
        "d5e200d7-5309-4f01-a2e0-98c5429104b6": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "d5e200d7-5309-4f01-a2e0-98c5429104b6",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "c6baeea5-44a7-43f2-b968-1d5e25ee54cd",
                    "transitionId": "6263798f-5037-47aa-9ccb-e89d20ac8f5d",
                    "weight": 1,
                    "state": "opened",
                    "time": 16031659230381
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "23562533-161f-40e1-a48d-bed06bbb0498",
                    "transitionId": "6263798f-5037-47aa-9ccb-e89d20ac8f5d",
                    "weight": 1,
                    "state": "passed",
                    "time": 16031659230386
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "64556e37-abc7-4933-9a65-c7c364273478",
                    "transitionId": "3f40edad-163a-4176-99b9-f29d2faf2bc6",
                    "weight": 1,
                    "state": "opened",
                    "time": 16031659230388
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "3fd7ca9c-eeb8-48f1-ba2b-b26a1862c6c7",
                    "transitionId": "3f40edad-163a-4176-99b9-f29d2faf2bc6",
                    "weight": 1,
                    "state": "passed",
                    "time": 16031659230392
                }
            ],
            "total_price": 16
        }
    }
}