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): 72
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": "bdee358a-8ffb-46d2-8acf-cea5460ed7f6",
    "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": {
        "82c0b512-c687-47a8-9614-d51b61b6bead": {
            "id": "82c0b512-c687-47a8-9614-d51b61b6bead",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "free",
            "from": "shipping_decision",
            "to": "free_shipping"
        },
        "7b8ef393-36e8-4015-a5d3-3096339fe37a": {
            "id": "7b8ef393-36e8-4015-a5d3-3096339fe37a",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "ups",
            "from": "shipping_decision",
            "to": "ups_shipping"
        },
        "09660b2f-606c-47ca-b808-985508066e49": {
            "id": "09660b2f-606c-47ca-b808-985508066e49",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "shipping_decision"
        }
    },
    "outTransitions": {
        "shipping_decision": [
            "82c0b512-c687-47a8-9614-d51b61b6bead",
            "7b8ef393-36e8-4015-a5d3-3096339fe37a"
        ]
    },
    "inTransitions": {
        "free_shipping": [
            "82c0b512-c687-47a8-9614-d51b61b6bead"
        ],
        "ups_shipping": [
            "7b8ef393-36e8-4015-a5d3-3096339fe37a"
        ],
        "shipping_decision": [
            "09660b2f-606c-47ca-b808-985508066e49"
        ]
    },
    "tokens": {
        "1e54e2b8-4d59-4b34-9a8e-969839a253cf": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "1e54e2b8-4d59-4b34-9a8e-969839a253cf",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "f7f41cdb-c5bf-45c6-888e-4825799354cc",
                    "transitionId": "09660b2f-606c-47ca-b808-985508066e49",
                    "weight": 1,
                    "state": "opened",
                    "time": 15421504966223
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "6602f399-fbc6-4c03-a595-cd29b01f86c3",
                    "transitionId": "09660b2f-606c-47ca-b808-985508066e49",
                    "weight": 1,
                    "state": "passed",
                    "time": 15421504966231
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "42eea6c8-a95e-4d0f-a11c-8612bc44142b",
                    "transitionId": "82c0b512-c687-47a8-9614-d51b61b6bead",
                    "weight": 1,
                    "state": "opened",
                    "time": 15421504966236
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "fb24fe1d-c55d-48d3-9892-a0053978cb01",
                    "transitionId": "82c0b512-c687-47a8-9614-d51b61b6bead",
                    "weight": 1,
                    "state": "passed",
                    "time": 15421504966242
                }
            ],
            "total_price": 72
        }
    }
}