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): 39
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": "96aac18c-be0f-4b18-9357-d27d0a972ef8",
    "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": {
        "9e2fd2c3-57f6-4654-833a-81d8caa0dc00": {
            "id": "9e2fd2c3-57f6-4654-833a-81d8caa0dc00",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "free",
            "from": "shipping_decision",
            "to": "free_shipping"
        },
        "26019927-3ce8-45d6-8e6b-8463d5f90580": {
            "id": "26019927-3ce8-45d6-8e6b-8463d5f90580",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "ups",
            "from": "shipping_decision",
            "to": "ups_shipping"
        },
        "3f752456-97c5-4f70-bb2f-93919824337c": {
            "id": "3f752456-97c5-4f70-bb2f-93919824337c",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "shipping_decision"
        }
    },
    "outTransitions": {
        "shipping_decision": [
            "9e2fd2c3-57f6-4654-833a-81d8caa0dc00",
            "26019927-3ce8-45d6-8e6b-8463d5f90580"
        ]
    },
    "inTransitions": {
        "free_shipping": [
            "9e2fd2c3-57f6-4654-833a-81d8caa0dc00"
        ],
        "ups_shipping": [
            "26019927-3ce8-45d6-8e6b-8463d5f90580"
        ],
        "shipping_decision": [
            "3f752456-97c5-4f70-bb2f-93919824337c"
        ]
    },
    "tokens": {
        "524bac2f-858a-43a0-9fa1-4ca4f5ce81e6": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "524bac2f-858a-43a0-9fa1-4ca4f5ce81e6",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "d208e257-0782-40c9-b886-ca32c11a6d5c",
                    "transitionId": "3f752456-97c5-4f70-bb2f-93919824337c",
                    "weight": 1,
                    "state": "opened",
                    "time": 15857766528077
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "01c643cf-660b-4253-9a0e-477cdf39b0ee",
                    "transitionId": "3f752456-97c5-4f70-bb2f-93919824337c",
                    "weight": 1,
                    "state": "passed",
                    "time": 15857766528081
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "77495ec6-8eaf-458c-87ba-94ef85587a2a",
                    "transitionId": "26019927-3ce8-45d6-8e6b-8463d5f90580",
                    "weight": 1,
                    "state": "opened",
                    "time": 15857766528084
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "cc1463dd-7a32-45a9-8050-15d7f7a43590",
                    "transitionId": "26019927-3ce8-45d6-8e6b-8463d5f90580",
                    "weight": 1,
                    "state": "passed",
                    "time": 15857766528088
                }
            ],
            "total_price": 39
        }
    }
}