### Installing json-logic-php via Composer Source: https://github.com/jwadhams/json-logic-php/blob/master/README.md This command demonstrates the recommended way to install the `json-logic-php` library using Composer, a dependency manager for PHP. It adds the library to your project's dependencies. ```bash composer require jwadhams/json-logic-php ``` -------------------------------- ### Direct Download of JsonLogic.php via cURL Source: https://github.com/jwadhams/json-logic-php/blob/master/README.md This command shows how to directly download the `JsonLogic.php` source file using `curl`. This method is an alternative to Composer for manual installation, suitable for self-contained projects. ```bash curl -O https://raw.githubusercontent.com/jwadhams/json-logic-php/master/src/JWadhams/JsonLogic.php ``` -------------------------------- ### Applying Complex Data-Driven Rules in PHP Source: https://github.com/jwadhams/json-logic-php/blob/master/README.md This example demonstrates a complex JsonLogic rule that combines literal values with data-driven lookups using `var`. It checks if `temp` is less than 110 AND if `pie.filling` is 'apple', showcasing nested data access and compound logic. ```php $rules = [ "and" => [ [ "<" => [ [ "var" => "temp" ], 110 ] ], [ "==" => [ [ "var" => "pie.filling" ], "apple" ] ] ] ]; $data = [ "temp" => 100, "pie" => [ "filling" => "apple" ] ]; JWadhams\JsonLogic::apply($rules, $data); // true ``` -------------------------------- ### Applying Compound Logic with 'and' Operator in PHP Source: https://github.com/jwadhams/json-logic-php/blob/master/README.md This example shows how to nest JsonLogic rules using the `and` operator. It evaluates two sub-rules (`>` and `<`) and returns `true` only if both conditions are met. This demonstrates building more complex logical expressions. ```php JWadhams\JsonLogic::apply( [ "and" => [ [ ">" => [3,1] ], [ "<" => [1,3] ] ] ] ); // true ``` -------------------------------- ### Applying JsonLogic Rules from JSON String (Array/Object) Source: https://github.com/jwadhams/json-logic-php/blob/master/README.md This example illustrates how the `JWadhams\JsonLogic::apply` method can evaluate rules whether they are decoded from a JSON string into a PHP associative array (`true` for `$assoc`) or a standard object (`false` for `$assoc`). Both representations are accepted by the library. ```php $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 ``` -------------------------------- ### Accessing Data with 'var' Operator (Syntactic Sugar) in PHP Source: https://github.com/jwadhams/json-logic-php/blob/master/README.md This example illustrates the syntactic sugar for unary operators like `var`, allowing the value to be directly provided instead of wrapped in an array. The rule `[ "var" => "a" ]` directly accesses the 'a' key from the data, returning `1`. ```php JWadhams\JsonLogic::apply( [ "var" => "a" ], [ "a" => 1, "b" => 2 ] ); // 1 ``` -------------------------------- ### Applying Simple Equality Rule in PHP Source: https://github.com/jwadhams/json-logic-php/blob/master/README.md This snippet demonstrates the most basic usage of `JWadhams\JsonLogic::apply` to evaluate a simple equality rule. The operator `==` is the key, and its operands `[1, 1]` are provided as an array, resulting in `true`. ```php JWadhams\JsonLogic::apply( [ "==" => [1, 1] ] ); // true ``` -------------------------------- ### Applying 'Never' Rule in PHP Source: https://github.com/jwadhams/json-logic-php/blob/master/README.md This snippet demonstrates that if the first parameter to `JsonLogic::apply` is a non-object, non-associative-array boolean `false`, it is returned immediately, acting as an 'always false' rule, regardless of the data. ```php //Never JWadhams\JsonLogic::apply(false, $i_wasnt_even_supposed_to_be_here); // false ``` -------------------------------- ### JsonLogic Rule Representation in JSON Source: https://github.com/jwadhams/json-logic-php/blob/master/README.md This snippet demonstrates the basic structure of a JsonLogic rule expressed in JSON. A rule always consists of a single key (the operator) and an array of values (the operands). ```json {"==" : ["apples", "apples"]} ``` -------------------------------- ### Applying 'Always' Rule in PHP Source: https://github.com/jwadhams/json-logic-php/blob/master/README.md This snippet shows that if the first parameter to `JsonLogic::apply` is a non-object, non-associative-array boolean `true`, it is returned immediately, effectively acting as an 'always true' rule, ignoring any provided data. ```php //Always JWadhams\JsonLogic::apply(true, $data_will_be_ignored); // true ``` -------------------------------- ### Accessing Data with 'var' Operator (Array Syntax) in PHP Source: https://github.com/jwadhams/json-logic-php/blob/master/README.md This snippet demonstrates using the `var` operator to access data from a provided data object. The rule `[ "var" => ["a"] ]` retrieves the value associated with the key 'a' from the `$data` array, which is `1`. ```php JWadhams\JsonLogic::apply( [ "var" => ["a"] ], // Rule [ "a" => 1, "b" => 2 ] // Data ); // 1 ``` -------------------------------- ### Accessing Array Elements by Numeric Index with 'var' in PHP Source: https://github.com/jwadhams/json-logic-php/blob/master/README.md This snippet shows how the `var` operator can be used to access elements within a data array by their numeric index. The rule `[ "var" => 1 ]` retrieves the element at index 1 (second element) from the provided data array, which is 'banana'. ```php JWadhams\JsonLogic::apply( [ "var" => 1 ], [ "apple", "banana", "carrot" ] ); // "banana" ``` -------------------------------- ### Decoding JsonLogic Rules to PHP Associative Arrays Source: https://github.com/jwadhams/json-logic-php/blob/master/README.md This PHP snippet shows how to decode a JsonLogic rule string into a PHP associative array using `json_decode` with the `$assoc` parameter set to `true`. This is the typical representation for rules in PHP when working with the `json-logic-php` library. ```php json_decode('{"==" : ["apples", "apples"]}', true); // ["==" => ["apples", "apples"]] ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.