hoaproject / Ruler

The Hoa\Ruler library.
https://hoa-project.net
625 stars 66 forks source link

Invert operator #119

Closed TheDeadCode closed 6 years ago

TheDeadCode commented 6 years ago

Hello, is it possible to do something like "human" not in ["alien", "animals"] ?

vonglasow commented 6 years ago

Hello,

It's possible to use not but the syntax should be

not("human" in ["alien", "animals"])

<?php

require 'vendor/autoload.php';

$ruler = new Hoa\Ruler\Ruler();

// 1. Write a rule.
$rule  = 'group in ["customer", "guest"] and points > 30';

// 2. Create a context.
$context           = new Hoa\Ruler\Context();
$context['group']  = 'customer';
$context['points'] = function () {
    return 42;
};

// 3. Assert!
var_dump(
    $ruler->assert($rule, $context)
);

/**
 * Will output:
 *     bool(true)
 */

$rule  = 'not (group in ["customer", "guest"]) and points > 30';

$context           = new Hoa\Ruler\Context();
$context['group']  = 'othergroup';
$context['points'] = function () {
    return 42;
};

var_dump(
    $ruler->assert($rule, $context)
);

/**
 * Will output:
 *     bool(true)
 */
Hywan commented 6 years ago

I'm closing the issue since the answer is valid. If you feel you have something to add, feel free to comment and I will reopend it.