Code Monkey home page Code Monkey logo

rude.php's Introduction

Rude.php

Rude.php is a PHP implementation of the rule-based control-flow pattern Rude.

Usage

Creating an instance

use Kaiser\Rude\{Rude, Rule};

$rude = new Rude();

Adding a rule

$rude->addRule(new Rule('func1', 'SimpleClass::simpleClassStaticFunc', [$simpleObj, 'simpleClassFunc']));

// Rule constructor
public function __construct(callable $condition, ?callable $yes, ?callable $no)

addRule accepts a Rule object. The Rule constructor takes three arguments: the condition to check, the function to call when the result is true, and the function to call when it is false. Each argument must be a callable; the yes and the no callbacks are nullable.

The return value of conditions must be true for proceeding with the yes callback and false with the no branch. When a condition returns null, Rude exits the condition chain. In this case, the yes and no callbacks can be null. These conditions are usually exit points.

Checking conditions

Checking conditions based on the applied rules is triggered by calling $rude->check().

$rude->check('func1');

This specifies the entry point in the condition chain and can be set to any valid rule condition.

See the examples folder for more details.

Benefits

  • Rude allows for an on-demand execution of a chain of dynamic if-then-else statements - hereinafter referred to as rules.
  • The control flow is easy to manage and the logic can be modified by simply changing the callbacks in the rules.
  • The chain of condition checking can be exited or paused at any given point.
  • The position in the rule hierarchy can be stored and the execution resumed at a later stage by setting the entry point.
  • Each rule is seen as a separate and independent logical unit.
  • Individual rules and groups of rules can be easily moved around.
  • Rules can be generated dynamically or loaded from a datasource.
  • The dispatcher makes it possible to ditch the rigid static conditional model in favour of a considerably more flexible one.

rude.php's People

Contributors

kaisersparpick avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.