Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Json Logic Php Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Product Decisions This Supports

  • 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:

    • "Calculate the total discount for a user’s cart by reducing items, where the initial discount is fetched from their loyalty tier (stored in var)."
    • "Sum order values with a dynamic base threshold (e.g., 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):

    • Dynamic Aggregations: Evaluate rules like "If the reduced sum of a user’s past orders (with an initial value from their account balance) exceeds X, grant premium access."
    • Context-Aware Initialization: Rules where the starting state is derived from other variables (e.g., "Start the discount calculation with the user’s current points balance").
    • Hybrid Rule/Calculation Logic: Combine rule evaluation with arithmetic operations (e.g., "Apply a 10% discount to the reduced total of items, where the initial total is pulled from a session variable.").

When to Consider This Package

  • Adopt When:

    • Your rules require dynamic initial values for reductions or accumulations (e.g., starting a calculation with a variable from the context).
    • You’re using reduce operations in JSON Logic and need to reference external variables as the initial accumulator state.
    • Your application involves complex data transformations where the starting point of a computation depends on runtime data (e.g., user-specific thresholds, session state).
    • You prioritize declarative logic over imperative code for operations like aggregations, discounts, or validations.
  • Look Elsewhere If:

    • Your use case doesn’t involve reduce operations or initial value expressions (this change is niche).
    • You need high-performance numerical computations (consider a dedicated math library like MathPHP).
    • Your rules are stateless or don’t require dynamic context for initial values (the package remains a good fit, but this feature adds minimal value).
    • You’re constrained by legacy systems that don’t support JSON Logic’s reduce syntax or variable references.

How to Pitch It (Stakeholders)

For Executives:

*"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:

  • Personalized pricing: "Discount starts at your current points balance."
  • Real-time analytics: "Trigger an alert if the reduced sum of user actions (starting from their baseline) spikes."

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."


For Engineering:

*"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:

  • Pure JSON Logic for complex calculations: No need to pre-process data in PHP before passing it to the rule engine.
  • Context-aware reductions: Start accumulations with values from the current context (e.g., user attributes, session data).
  • Cleaner rule syntax: Avoid workarounds like hardcoding initial values or using custom functions.

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:

  • Learning curve: Teams new to JSON Logic may need training on reduce and variable references.
  • Performance: Evaluating expressions as initial values adds a tiny overhead (benchmark if latency is critical).

Proposal:

  • Pilot in a data-heavy feature (e.g., pricing engine, analytics dashboard) to validate the value.
  • Document the pattern for teams to adopt (e.g., "Use reduce with var() for dynamic initial states").
  • Pair with a UI (e.g., json-logic-js) to let non-technical users define these rules.

Alternatives considered:

  • Custom PHP logic: Would require more maintenance and lose the benefits of externalized rules.
  • Other libraries: No PHP-native alternative offers this exact JSON Logic reduce + variable integration."*
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor