jwadhams/json-logic-php
Evaluate JsonLogic rules in PHP to share and store business logic across frontend and backend. Parses JSON-formatted logic (arrays/objects), supports nesting, comparisons, boolean ops, and data-driven evaluation via JsonLogic::apply with input data.
reduce feature with support for expression-based initial values (e.g., var-resolved contexts) expands use cases for:
reduce over an order’s items with an initial value tied to a user’s discount tier).if/var combinations (e.g., "Start with the user’s balance, then reduce by each transaction").reduce over approval steps with an initial state from var('current_status')).Collection::reduce() for database-agnostic aggregations.GROUP BY/HAVING logic via JSON rules (e.g., "Reduce by var('user_segment')").reduce behavior (initial values can still be literals).Collection::reduce(), ensure the package’s reduce operator aligns with Laravel’s method signature (e.g., (accumulator, current, index) => accumulator).JSON_EXTRACT or CAST to resolve var-based initial values in window functions (e.g., SUM(...) OVER (PARTITION BY var('category'))).reduce rules with context-aware initial values (e.g., Rule::resolveInitialValue($rule, $context)).var references in initial values (e.g., var('initial') where initial depends on itself).Collection and Query Builder to ensure operator parity.reduce itself). Mitigate by:
Cache::remember("reduce_initial_{$contextHash}", ...)).reduce + var chains could hit PHP’s recursion limit. Test with:
ini_set('xdebug.max_nesting_level', 200); // For CI testing
var('user_input')), validate allowed expressions (e.g., whitelist var, +/- operators).var calls) could exhaust memory. Set a max evaluation depth.JsonLogic\Evaluator::setMaxDepth()).reduce).var resolver or a custom Laravel context provider?var not found)? Default to 0? Throw an exception?reduce with expression-based initial values in CI? Mock the context provider?var resolution in non-PHP contexts?reduce operations to detect regressions?Illuminate\Support\Collection with a jsonLogicReduce() method:
public function jsonLogicReduce(string $rule, mixed $initialValue = null, callable $resolver = null): mixed
{
$evaluator = app(JsonLogicEvaluator::class);
return $this->reduce(function ($carry, $item) use ($evaluator, $rule) {
return $evaluator->evaluate($rule, ['item' => $item, 'carry' => $carry]);
}, $resolver ? $resolver($initialValue) : $initialValue);
}
var in initial values:
$result = $collection->jsonLogicReduce(
'{"reduce": ["var('item.price')", {"+": ["var('carry')", 1]}]}',
['initial' => 'var(user.discount)'],
fn($val) => $evaluator->evaluate($val, ['user' => auth()->user()])
);
reduceRule accessor to models to hydrate rules with context:
public function getReduceRuleAttribute($value)
{
return (new JsonLogicEvaluator)->evaluate(
$value,
['initial' => $this->initialValueFromContext()]
);
}
jsonb_path_query_first to resolve var in window functions:
SELECT
id,
SUM(price) OVER (
PARTITION BY category
) AS running_total
FROM products
WHERE category = jsonb_path_query_first('data->>category', '$')
reduce:
{"reduce": [0, {"+": ["var('item')", 1]}]}).array_reduce.var-resolved expressions (e.g., {"reduce": ["var('user.balance')", ...]}).rule_type column to your rules table to distinguish between old and new reduce syntax.reduce rules to the new format.Cache::remember(
"reduce_initial_{$contextHash}",
now()->addHours(1),
fn() => $evaluator->evaluate($initialExpression, $context)
);
reduce operator to support named arguments.array_reduce or Collection::reduce.reduce with expression-based initial values.composer.json to pin to 1.5.1.$this->app->singleton(JsonLogicEvaluator::class, function ($app) {
return new JsonLogicEvaluator(new ContextResolver($app['auth']));
});
initial_expression to your rules table.var-resolved initial values.reduce operation in your codebase with the new syntax.reduce evaluations with context hashes for debugging:
Log::debug('Reduce evaluated', [
'rule' => $rule,
'initial_value' => $initialValue,
'context_hash' => md5(json_encode($context)),
How can I help you explore Laravel packages today?