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): 91
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": "dcc7fe7c-4fca-4e78-99e8-a7e512dabb48",
    "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": {
        "8c164da3-e589-4bc5-9650-ce31ece81a31": {
            "id": "8c164da3-e589-4bc5-9650-ce31ece81a31",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "free",
            "from": "shipping_decision",
            "to": "free_shipping"
        },
        "52344438-18d1-4629-bbfa-5c00f63154e5": {
            "id": "52344438-18d1-4629-bbfa-5c00f63154e5",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "ups",
            "from": "shipping_decision",
            "to": "ups_shipping"
        },
        "546ca1a4-68c6-4db4-918d-039e04580950": {
            "id": "546ca1a4-68c6-4db4-918d-039e04580950",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "shipping_decision"
        }
    },
    "outTransitions": {
        "shipping_decision": [
            "8c164da3-e589-4bc5-9650-ce31ece81a31",
            "52344438-18d1-4629-bbfa-5c00f63154e5"
        ]
    },
    "inTransitions": {
        "free_shipping": [
            "8c164da3-e589-4bc5-9650-ce31ece81a31"
        ],
        "ups_shipping": [
            "52344438-18d1-4629-bbfa-5c00f63154e5"
        ],
        "shipping_decision": [
            "546ca1a4-68c6-4db4-918d-039e04580950"
        ]
    },
    "tokens": {
        "2bdd4180-6e83-471b-b351-bf513ff8b336": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "2bdd4180-6e83-471b-b351-bf513ff8b336",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "0b576ad7-eca0-484c-885d-706f1f9b5e76",
                    "transitionId": "546ca1a4-68c6-4db4-918d-039e04580950",
                    "weight": 1,
                    "state": "opened",
                    "time": 16107364367882
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "db25022b-6295-47f1-9f27-017cca5c6a76",
                    "transitionId": "546ca1a4-68c6-4db4-918d-039e04580950",
                    "weight": 1,
                    "state": "passed",
                    "time": 16107364367886
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "93d481a4-2c0a-488a-b6bb-e91b9a0b130c",
                    "transitionId": "8c164da3-e589-4bc5-9650-ce31ece81a31",
                    "weight": 1,
                    "state": "opened",
                    "time": 16107364367889
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "f588f385-8a16-4f81-8711-5bc2c20cb362",
                    "transitionId": "8c164da3-e589-4bc5-9650-ce31ece81a31",
                    "weight": 1,
                    "state": "passed",
                    "time": 16107364367893
                }
            ],
            "total_price": 91
        }
    }
}