How to use data-driven rules with the 'var' operator
masterTo make rules dynamic, use the var operator to access values from a provided data object. This allows you to mix static literals with dynamic data.
Accessing keys
You can access associative array keys using ["var" => "key_name"]. The library also supports syntactic sugar for unary operators, allowing you to skip the array wrapper: ["var" => "key_name"].
Accessing numeric indices
You can access array elements by numeric index using ["var" => index].
Nested data access
You can access nested properties using dot notation within the var operator (e.g., ["var" => "parent.child"]).
// Accessing a key
JWadhams\JsonLogic::apply([ "var" => "a" ], [ "a" => 1, "b" => 2 ]);
// returns 1
// Syntactic sugar for unary operators
JWadhams\JsonLogic::apply([ "var" => "a" ], [ "a" => 1, "b" => 2 ]);
// returns 1
// Accessing numeric index
JWadhams\JsonLogic::apply([ "var" => 1 ], [ "apple", "banana", "carrot" ]);
// returns "banana"
// Complex nested rule
$rules = [ "and" => [
[ "<" => [ [ "var" => "temp" ], 110 ] ],
[ "==" => [ [ "var" => "pie.filling" ], "apple" ] ]
] ];
$data = [ "temp" => 100, "pie" => [ "filling" => "apple" ] ];
JWadhams\JsonLogic::apply($rules, $data);
// returns true