PHP code example of mb4it / conditionable

1. Go to this page and download the library: Download mb4it/conditionable 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/ */

    

mb4it / conditionable example snippets


$result = (new ConditionTree())
    ->where(fn() => time(), '>', 0)
    ->result();
//true
    
//Или использовать функцию
$result = condition()
    ->where(fn() => time(), '>', 0)
    ->result();
// true


class User {
    public string $name = 'John';
    public int $age = 25;
}

$user = new User();

$result = condition()
    ->context($user, 'u')
    ->where('u.name', 'John')
    ->where('u.age', '>=', 18)
    ->result();
// true

// или
$result = condition(['u' => $user])
    ->where('u.name', 'John')
    ->where('u.age', '>=', 18)
    ->result();
// true

use MB\Support\Conditionable\Traits\Conditionable;

class Product {
    use Conditionable;
    
    public float $price = 150.0;
    public string $category = 'electronics';
}

$product = new Product();

// При использовании Trait классе
// Его алиас для контекста -> this
$result = $product->condition()
    ->where('this.price', '>', 100)
    ->where('this.category', 'electronics')
    ->result();

// true

$result = $product->check(['this.price', '>', 100], ['this.category', 'electronics']);
// true

condition()
    ->logicOr() // по умолчанию 'and'
    ->where('field1', 'value1')
    ->where('field2', 'value2')
    ->calculate()
    ->result();

// Или
condition()
    ->orGroup(
        function(ConditionTree $tree) { 
            $tree
                ->where('field1', 'value1') 
                ->where('field2', 'value2');
        }
    )
    ->result();

// Отрицание всего условия
$tree = condition()
    ->where('field', 'value')
    ->negative()
    ->result();

// Отрицание отдельного условия
condition()
    ->notGroup(fn (ConditionTree $cond) => $cond->where('field', 'value'))
    ->result();

$inner = condition()
    ->logicOr()
    ->where('field1', 'value1')
    ->where('field2', 'value2')

$result = condition()
    ->where('mainField', 'mainValue')
    ->where($inner)
    ->result();

$array = ['age' => 25];
$policy = ['minAge' => 18];

condition()
    ->context($array, 'data')
    ->context($policy, 'rule')
    ->where('data.age', '>=', 'rule.minAge')
    ->result();

condition()
    ->where(fn() => getValue(), '>', 10)
    ->where([SomeClass::class, 'staticMethod'], '=', 'expected')
    ->result();

$result = condition()
    ->where('field', 'value')
    ->calculate()
    ->then(
        function(bool $isTrue, Registry $context) { ... }
    )
    ->result();

use MB\Support\Conditionable\Contracts\ActionInterface;
use MB\Support\Conditionable\Actions\ActionRegistry;

class CustomAction implements ActionInterface
{
    public static function operator(): string
    {
        return 'custom_operator';
    }

    public static function handle($a, $b): bool
    {
        // ваша логика
        return $a === $b;
    }
}

// Регистрация
ActionRegistry::register(CustomAction::class);

// Использование
condition()
    ->where('field', 'custom_operator', 'value')
    ->result();