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.
Dynamic Rule Engine for Business Logic: Expands support for complex data transformations within rules (e.g., aggregating user behavior metrics, calculating dynamic initial values for reductions). The new reduce expression enhancement allows rules to reference external variables or computed values as the initial accumulator state, enabling richer logic like:
var)."reduce(order_items, '+', var('min_order_value')))."
This reduces the need for pre-processing data in PHP code, keeping logic purely declarative in JSON.Roadmap Flexibility: Strengthens use cases for data-driven workflows where initial state depends on runtime context (e.g., A/B testing where control groups start with different baseline values). Aligns with trends in feature-driven development, where rules adapt to experimental conditions without code changes.
Build vs. Buy: Further validates the package as a drop-in solution for PHP teams needing advanced JSON Logic features without custom development. The incremental improvement reduces friction for teams already using the package for simpler rules.
Use Cases (Updated):
Adopt When:
reduce operations in JSON Logic and need to reference external variables as the initial accumulator state.Look Elsewhere If:
reduce operations or initial value expressions (this change is niche).reduce syntax or variable references.*"This update supercharges our rule engine’s ability to handle dynamic calculations—like starting a discount or aggregation with a value pulled from the user’s profile or session. For example, we could now calculate a user’s total lifetime value and apply a context-aware discount in a single rule, without touching the codebase. This is a game-changer for features like:
It’s the difference between writing a monolithic PHP function for every calculation and letting product teams define logic in plain JSON. Zero dev time, 100% flexibility."*
ROI Hook: "For a feature like dynamic loyalty discounts, this could cut our time-to-market for new promotions from weeks to hours—while reducing bugs from hardcoded logic."
*"The 1.5.1 update adds support for evaluating the initial value of a reduce as an expression (e.g., reduce(items, '+', var('user_balance'))). This is a powerful but subtle improvement that unlocks:
Example Use Case:
{
"reduce": ["cart_items", "+", {"var": "user_points"}],
">==": [?, 1000]
}
*Translates to: "If the sum of cart items (starting from the user’s points) is ≥ 1000, apply a rule."
Trade-offs:
reduce and variable references.Proposal:
reduce with var() for dynamic initial states").Alternatives considered:
reduce + variable integration."*How can I help you explore Laravel packages today?