LaravelPackages.net
Acme Inc.
Toggle sidebar
elepunk/evaluator

Laravel package for symfony expression language component

192
9
v1.0.1
About elepunk/evaluator

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

Last updated

Evaluator

Build Status Scrutinizer Code Quality Coverage Status Latest Stable Version Latest Unstable Version License SensioLabsInsight

A Laravel package and Orchestra extension for symfony/expression-language component.

Installation

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

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

Quick Installation

composer require "elepunk/evaluator=1.0.*"

Setup

If you are using Orchestra Platform, you can simply enable the extension or add the service provider. This will also load the Evaluator alias automatically.

'providers' => [
	'Elepunk\Evaluator\EvaluatorServiceProvider'
];

Adapter

This package provide Orchesta Memory as the default driver.

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