Download the PHP package jwadhams/json-logic-php without Composer
On this page you can find all versions of the php package jwadhams/json-logic-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jwadhams/json-logic-php
More information about jwadhams/json-logic-php
Files in jwadhams/json-logic-php
Package json-logic-php
Short Description Build rules with complex comparisons and boolean operators, serialized as JSON, and execute them in PHP
License MIT
Informations about the package json-logic-php
json-logic-php
This parser accepts JsonLogic rules and executes them in PHP.
The JsonLogic format is designed to allow you to share rules (logic) between front-end and back-end code (regardless of language difference), even to store logic along with a record in a database. JsonLogic is documented extensively at JsonLogic.com, including examples of every supported operation and a place to try out rules in your browser.
The same format can also be executed in JavaScript by the library json-logic-js
Examples
A note about types
This is a PHP interpreter of a format designed to be transmitted and stored as JSON. So it makes sense to conceptualize the rules in JSON.
Expressed in JSON, a JsonLogic rule is always one key, with an array of values.
PHP has a way to express associative arrays as literals, and no object equivalent, so all these examples are written as if JsonLogic rules were decoded with json_decode
's $assoc
parameter set true, e.g.
The library will happily accept either associative arrays or objects:
Simple
This is a simple test, equivalent to 1 == 1
. A few things about the format:
- The operator is always in the "key" position. There is only one key per JsonLogic rule.
- The values are typically an array.
- Each value can be a string, number, boolean, array, or null
Compound
Here we're beginning to nest rules.
In an infix language (like PHP) this could be written as:
Data-Driven
Obviously these rules aren't very interesting if they can only take static literal data. Typically JsonLogic::apply
will be called with a rule object and a data object. You can use the var
operator to get attributes of the data object:
If you like, we support syntactic sugar on unary operators to skip the array around values:
You can also use the var
operator to access an array by numeric index:
Here's a complex rule that mixes literals and data. The pie isn't ready to eat unless it's cooler than 110 degrees, and filled with apples.
Always and Never
Sometimes the rule you want to process is "Always" or "Never." If the first parameter passed to JsonLogic::apply
is a non-object, non-associative-array, it is returned immediately.
Installation
The best way to install this library is via Composer:
If that doesn't suit you, and you want to manage updates yourself, the entire library is self-contained in src/JWadhams/JsonLogic.php
and you can download it straight into your project as you see fit.