maniaba/rule-engine

A flexible and extensible framework for defining and evaluating rules and conditions, and executing corresponding actions based on specified conditions

dev-develop 2025-07-21 08:38 UTC

This package is auto-updated.

Last update: 2025-07-21 08:38:37 UTC


README

PHPUnit PHPStan Psalm Docs

PHP License

The Rule Engine is a flexible and extensible framework for defining and evaluating rules and conditions, and executing corresponding actions based on specified conditions.

Requirements

  • PHP 8.1 or higher
  • Composer

Installation

Install the package via Composer:

composer require maniaba/rule-engine

For detailed installation instructions and verification, see the Installation Documentation.

Key Features

  • Define rules using a structured array configuration
  • Combine multiple conditions with logical operators (AND, OR)
  • Execute actions based on condition results
  • Extend with custom conditions and actions
  • Validate rule configurations

Quick Example

<?php

use Maniaba\RuleEngine\Builders\ArrayBuilder;
use Maniaba\RuleEngine\Context\ArrayContext;

// Create a builder
$builder = new ArrayBuilder();

// Register an action
$builder->actions()->registerAction('printMessage', function(ArrayContext $context, string $message) {
    echo "Message: {$message}\n";
    return true;
});

// Define a simple rule
$config = [
    'node' => 'condition',
    'if' => [
        'node' => 'context',
        'contextName' => 'value',
        'operator' => 'greaterThan',
        'value' => 10,
    ],
    'then' => [
        'node' => 'action',
        'actionName' => 'printMessage',
        'arguments' => [
            'message' => 'Value is greater than 10',
        ],
    ],
];

// Build and execute the rule
$ruleSet = $builder->build($config);
$context = new ArrayContext(['value' => 15]);
$evaluator = new Maniaba\RuleEngine\Evaluators\BasicEvaluator();
$evaluator->execute(clone $ruleSet, $context);

Documentation

Comprehensive documentation is available at https://maniaba.github.io/rule-engine/, including:

Find yourself stuck using the package? Found a bug? Do you have general questions or suggestions for improving the rule engine? Feel free to create an issue on GitHub, we'll try to address it as soon as possible.

Testing

composer test

Changelog

All notable changes to this project are documented in the CHANGELOG.md file.

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for details.

Security

If you discover a security vulnerability, please send an email to maniaba@outlook.com instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.