2017 © Pedro Peláez
 

library json-logic-php

Build rules with complex comparisons and boolean operators, serialized as JSON, and execute them in PHP

image

jwadhams/json-logic-php

Build rules with complex comparisons and boolean operators, serialized as JSON, and execute them in PHP

  • Thursday, October 5, 2017
  • by jwadhams
  • Repository
  • 5 Watchers
  • 44 Stars
  • 27,222 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 9 Forks
  • 3 Open issues
  • 23 Versions
  • 17 % Grown

The README.md

json-logic-php

This parser accepts JsonLogic rules and executes them in PHP., (*1)

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., (*2)

The same format can also be executed in JavaScript by the library json-logic-js, (*3)

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., (*4)

Expressed in JSON, a JsonLogic rule is always one key, with an array of values., (*5)

{"==" : ["apples", "apples"]}

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., (*6)

json_decode('{"==" : ["apples", "apples"]}', true);
// ["==" => ["apples", "apples"]]

The library will happily accept either associative arrays or objects:, (*7)

$rule = '{"==":["apples", "apples"]}';

//Decode the JSON string to an array, and evaluate it.
JWadhams\JsonLogic::apply( json_decode($rule, true) );
// true

//Decode the JSON string to an object, and evaluate it.
JWadhams\JsonLogic::apply( json_decode($rule, false) );
// true

Simple

JWadhams\JsonLogic::apply( [ "==" => [1, 1] ] );
// true

This is a simple test, equivalent to 1 == 1. A few things about the format:, (*8)

  1. The operator is always in the "key" position. There is only one key per JsonLogic rule.
  2. The values are typically an array.
  3. Each value can be a string, number, boolean, array, or null

Compound

Here we're beginning to nest rules., (*9)

JWadhams\JsonLogic::apply(
    [ "and" => [
        [ ">" => [3,1] ],
        [ "<" => [1,3] ]
    ] ]
);
// true

In an infix language (like PHP) this could be written as:, (*10)

( (3 > 1) and (1 < 3) )

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:, (*11)

JWadhams\JsonLogic::apply(
    [ "var" => ["a"] ], // Rule
    [ "a" => 1, "b" => 2 ]   // Data
);
// 1

If you like, we support syntactic sugar on unary operators to skip the array around values:, (*12)

JWadhams\JsonLogic::apply(
    [ "var" => "a" ],
    [ "a" => 1, "b" => 2 ]
);
// 1

You can also use the var operator to access an array by numeric index:, (*13)

JWadhams\JsonLogic::apply(
    [ "var" => 1 ],
    [ "apple", "banana", "carrot" ]
);
// "banana"

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., (*14)

$rules = [ "and" => [
    [ "<" => [ [ "var" => "temp" ], 110 ] ],
    [ "==" => [ [ "var" => "pie.filling" ], "apple" ] ]
] ];

$data = [ "temp" => 100, "pie" => [ "filling" => "apple" ] ];

JWadhams\JsonLogic::apply($rules, $data);
// true

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., (*15)

//Always
JWadhams\JsonLogic::apply(true, $data_will_be_ignored);
// true

//Never
JWadhams\JsonLogic::apply(false, $i_wasnt_even_supposed_to_be_here);
// false

Installation

The best way to install this library is via Composer:, (*16)

composer require jwadhams/json-logic-php

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., (*17)

curl -O https://raw.githubusercontent.com/jwadhams/json-logic-php/master/src/JWadhams/JsonLogic.php

The Versions

05/10 2017

dev-master

9999999-dev

Build rules with complex comparisons and boolean operators, serialized as JSON, and execute them in PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Jeremy Wadhams

05/10 2017

1.3.10

1.3.10.0

Build rules with complex comparisons and boolean operators, serialized as JSON, and execute them in PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Jeremy Wadhams

08/09 2017

1.3.9

1.3.9.0

Build rules with complex comparisons and boolean operators, serialized as JSON, and execute them in PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Jeremy Wadhams

15/07 2017

1.3.8

1.3.8.0

Build rules with complex comparisons and boolean operators, serialized as JSON, and execute them in PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Jeremy Wadhams

14/07 2017

dev-toSQL

dev-toSQL

Build rules with complex comparisons and boolean operators, serialized as JSON, and execute them in PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Jeremy Wadhams

05/04 2017

1.3.7

1.3.7.0

Build rules with complex comparisons and boolean operators, serialized as JSON, and execute them in PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Jeremy Wadhams

27/02 2017

1.3.6

1.3.6.0

Build rules with complex comparisons and boolean operators, serialized as JSON, and execute them in PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Jeremy Wadhams

18/10 2016

1.3.5

1.3.5.0

Build rules with complex comparisons and boolean operators, serialized as JSON, and execute them in PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Jeremy Wadhams

13/07 2016

1.3.4

1.3.4.0

Build rules with complex comparisons and boolean operators, serialized as JSON, and execute them in PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Jeremy Wadhams

17/06 2016

1.3.3

1.3.3.0

Build rules with complex comparisons and boolean operators, serialized as JSON, and execute them in PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Jeremy Wadhams

02/03 2016

1.3.2

1.3.2.0

Build rules with complex comparisons and boolean operators, serialized as JSON, and execute them in PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Jeremy Wadhams

29/02 2016

1.3.1

1.3.1.0

Build rules with complex comparisons and boolean operators, serialized as JSON, and execute them in PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Jeremy Wadhams

26/02 2016

1.3.0

1.3.0.0

Build rules with complex comparisons and boolean operators, serialized as JSON, and execute them in PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Jeremy Wadhams

20/11 2015

1.2.6

1.2.6.0

Build rules with complex comparisons and boolean operators, serialized as JSON, and execute them in PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Jeremy Wadhams

17/11 2015

1.2.5

1.2.5.0

Build rules with complex comparisons and boolean operators, serialized as JSON, and execute them in PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Jeremy Wadhams

16/11 2015

1.2.4

1.2.4.0

Build rules with complex comparisons and boolean operators, serialized as JSON, and execute them in PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Jeremy Wadhams

16/11 2015

1.2.3

1.2.3.0

Build rules with complex comparisons and boolean operators, serialized as JSON, and execute them in PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Jeremy Wadhams

16/11 2015

1.2.2

1.2.2.0

Build rules with complex comparisons and boolean operators, serialized as JSON, and execute them in PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Jeremy Wadhams

04/11 2015

1.2.1

1.2.1.0

Build rules with complex comparisons and boolean operators, serialized as JSON, and execute them in PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Jeremy Wadhams

21/10 2015

1.2.0

1.2.0.0

Build rules with complex comparisons and boolean operators, serialized as JSON, and execute them in PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Jeremy Wadhams

17/10 2015

1.1.0

1.1.0.0

Build rules with complex comparisons and boolean operators, serialized as JSON, and execute them in PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Jeremy Wadhams

15/10 2015

1.0.1

1.0.1.0

Build rules with complex comparisons and boolean operators, serialized as JSON, and execute them in PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Jeremy Wadhams

15/10 2015

1.0.0

1.0.0.0

Build rules with complex comparisons and boolean operators, serialized as JSON, and execute them in PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Jeremy Wadhams