LaravelPackages.net
Acme Inc.
Toggle sidebar
periloso/evaluator

Symfony Expression Language for Laravel

160
3
1.3.1
About periloso/evaluator

periloso/evaluator is a Laravel package for symfony expression language for laravel. It currently has 3 GitHub stars and 160 downloads on Packagist (latest version 1.3.1). Install it with composer require periloso/evaluator. Discover more Laravel packages by periloso or browse all Laravel packages to compare alternatives.

Last updated

Evaluator

Symfony Expression Language module for Laravel.

Installation

Simply update the composer.json file and run composer install.

"require": {
	"periloso/evaluator": "1.0.*"
}

Quick Installation

composer require "periloso/evaluator=1.0.*"

How To Use

Evaluating an expression

$test = [
    'foo' => 10,
    'bar' => 5
];

echo Evaluator::evaluate('foo > bar', $test); //this will return true

You can also save the expression rule.

$test = [
    'foo' => 10,
    'bar' => 5
];

Evaluator::expression()->add('test', 'foo > bar');

echo Evaluator::evaluateRule('test', $test); //this will return true

For supported expressions, visit the Symfony Expression Language Component.

Condition

Let say we want to implement 10% tax to our collection.

$item = [
    'price' => 100
];

$condition = [
    'target' => 'price',
    'action' => '10%',
    'rule' => 'price > 50'
];

Evaluator::expression()->add('tax', $condition);

$calculated = Evaluator::condition('tax', $item);

Item with multiplier.

$item = [
	'price' => 50,
	'quantity' => 2
];

$condition = [
    'target' => 'price',
    'action' => '10%',
    'rule' => 'price > 50',
    'multiplier' => 'quantity'
];

Evaluator::expression()->add('tax', $condition);

$calculated = Evaluator::condition('tax', $item);
Comments