parscore/laravel-parscore

A lightweight and extensible rules engine for Laravel

dev-main 2025-05-11 20:14 UTC

This package is not auto-updated.

Last update: 2025-05-17 09:34:38 UTC


README

A lightweight and extensible rules engine for Laravel. Parse and evaluate conditions with a simple syntax: fn[param1,param2,...]. Built with pure PHP for maximum compatibility and zero external dependencies.

Installation

Install via Composer:

composer require parscore/laravel-parscore

The service provider is automatically registered via Laravel's auto-discovery.

Usage

use ParsCore\Laravel\ParsCore;

$parser = app(ParsCore::class);
$result = $parser->parse('AND[equals[1,1],greater_than[10,5]]');

if ($result) {
    echo "Condition met!";
}

Example Syntax

  • Check if values are equal and one is greater than another:

    AND[equals[1,1],greater_than[10,5]]
    
  • Nested conditions:

    OR[AND[equals[1,1],greater_than[10,5]],NOT[less_than[2,3]]]
    

Supported Commands

Extending ParsCore

  • Define custom commands in app/ParsCore/CustomCommands.php to extend functionality without affecting package updates:

    <?php
    
    use ParsCore\Laravel\ParsCore;
    
    ParsCore::registerCommand('is_positive', function ($params) {
        return $params[0] > 0;
    }, [
        'params' => [
            ['type' => 'any', 'required' => true],
        ],
        'type' => 'condition',
    ]);
    
    ParsCore::registerCommand('contains', function ($params) {
        return str_contains($params[0], $params[1]);
    }, [
        'params' => [
            ['type' => 'any', 'required' => true],
            ['type' => 'any', 'required' => true],
        ],
        'type' => 'condition',
    ]);
  • Then use them:

    $parser = app(ParsCore::class);
    $result = $parser->parse('is_positive[5]'); // true
    $result = $parser->parse('contains[hello world,world]'); // true

Contributing

Submit issues or pull requests to the GitHub repository. We welcome contributions to make ParsCore even better!

License

This package is open-sourced under the .