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): 96
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": "804b9301-2c88-41fc-92d7-4472e199a088",
    "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": {
        "90a9ff76-3ba9-4541-ae9d-879d43301cf0": {
            "id": "90a9ff76-3ba9-4541-ae9d-879d43301cf0",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "free",
            "from": "shipping_decision",
            "to": "free_shipping"
        },
        "1a4f4ea2-be79-4c69-b60c-571bf3d64c52": {
            "id": "1a4f4ea2-be79-4c69-b60c-571bf3d64c52",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "ups",
            "from": "shipping_decision",
            "to": "ups_shipping"
        },
        "0a2c3ff4-ce00-46b4-82e1-2afeae47e55e": {
            "id": "0a2c3ff4-ce00-46b4-82e1-2afeae47e55e",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "shipping_decision"
        }
    },
    "outTransitions": {
        "shipping_decision": [
            "90a9ff76-3ba9-4541-ae9d-879d43301cf0",
            "1a4f4ea2-be79-4c69-b60c-571bf3d64c52"
        ]
    },
    "inTransitions": {
        "free_shipping": [
            "90a9ff76-3ba9-4541-ae9d-879d43301cf0"
        ],
        "ups_shipping": [
            "1a4f4ea2-be79-4c69-b60c-571bf3d64c52"
        ],
        "shipping_decision": [
            "0a2c3ff4-ce00-46b4-82e1-2afeae47e55e"
        ]
    },
    "tokens": {
        "a3bbdb4f-5c33-4ee2-9d42-f4391a9efa0f": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "a3bbdb4f-5c33-4ee2-9d42-f4391a9efa0f",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "23c87f1a-1c8c-4b5f-aefa-962c139e0581",
                    "transitionId": "0a2c3ff4-ce00-46b4-82e1-2afeae47e55e",
                    "weight": 1,
                    "state": "opened",
                    "time": 15823456310700
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "91e39a85-d5f1-4fc4-8134-4afdba11c407",
                    "transitionId": "0a2c3ff4-ce00-46b4-82e1-2afeae47e55e",
                    "weight": 1,
                    "state": "passed",
                    "time": 15823456310705
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "aa6b1a1b-4a67-477a-8ab5-07e457cb1a8a",
                    "transitionId": "90a9ff76-3ba9-4541-ae9d-879d43301cf0",
                    "weight": 1,
                    "state": "opened",
                    "time": 15823456310708
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "e7891c70-992f-4a19-a177-c33cfa347b1d",
                    "transitionId": "90a9ff76-3ba9-4541-ae9d-879d43301cf0",
                    "weight": 1,
                    "state": "passed",
                    "time": 15823456310712
                }
            ],
            "total_price": 96
        }
    }
}