PHP code example of nicoswd / symfony-rule-engine-bundle

1. Go to this page and download the library: Download nicoswd/symfony-rule-engine-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nicoswd / symfony-rule-engine-bundle example snippets




// in AppKernel::registerBundles()
$bundles = [
    // ...
    new nicoSWD\RuleBundle\RuleBundle(),
    // ...
];



$rule = '[1, 4, 3].join(glue) === "1-4-3"';
$variables = ['glue' => '-'];

$result = $this->get('rule_parser')->isTrue($rule, $variables);
var_dump($result); // bool(true)



namespace AppBundle\Functions;

use nicoSWD\Rules\Core\CallableUserFunction;
use nicoSWD\Rules\Tokens\BaseToken;
use nicoSWD\Rules\Tokens\TokenArray;

class ArrayFilter implements CallableUserFunction
{
    /**
     * @param BaseToken $param
     * @param BaseToken $param ...
     * @return BaseToken
     */
    public function call($param = null)
    {
        // Make sure this functions only works on arrays
        if (!$param instanceof TokenArray) {
            throw new \InvalidArgumentException();
        }

        return new TokenArray(
            array_values(
                array_filter(
                    $param->toArray()
                )
            )
        );
    }

    public function getName(): string
    {
        return 'array_filter';
    }
}



$rule = 'array_filter([0, false, 1]) === [1]';

$result = $this->get('rule_parser')->isTrue($rule);
var_dump($result); // bool(true)
yaml
AppBundle\Functions\:
    resource: '../../src/AppBundle/Functions'
    tags: ['nico_swd.rule.function']