diff --git a/language/en-GB/en-GB.plg_vmshipment_rules_shipping.ini b/language/en-GB/en-GB.plg_vmshipment_rules_shipping.ini index 91c1d037f9633b630de1daa761f4ce1ebcf78486..35b3253f3930e2222461cec1458558714db3ea1e 100644 --- a/language/en-GB/en-GB.plg_vmshipment_rules_shipping.ini +++ b/language/en-GB/en-GB.plg_vmshipment_rules_shipping.ini @@ -52,4 +52,7 @@ VMSHIPMENT_RULES_EVALUATE_UNKNOWN_OPERATOR="Unknown operator '%s' encountered du VMSHIPMENT_RULES_EVALUATE_UNKNOWN_FUNCTION="Unknown function '%s' encountered during evaluation of rule '%s'." VMSHIPMENT_RULES_EVALUATE_UNKNOWN_ERROR="Unknown error occurred during evaluation of rule '%s'." VMSHIPMENT_RULES_EVALUATE_ASSIGNMENT_TOPLEVEL="Assignments are not allows inside expressions (rule given was '%s')" -VMSHIPMENT_RULES_EVALUATE_UNKNOWN_VALUE="Evaluation yields unknown value while evaluating rule part '%s'." \ No newline at end of file +VMSHIPMENT_RULES_EVALUATE_UNKNOWN_VALUE="Evaluation yields unknown value while evaluating rule part '%s'." + +VMSHIPMENT_RULES_EVALUATE_LISTFUNCTION_ARGS="List function '%s' requires all arguments to be lists. (Full rule: '%s')" +VMSHIPMENT_RULES_EVALUATE_LISTFUNCTION_UNKNOWN="Unknown list function '%s' encountered. (Full rule: '%s')" diff --git a/rules_shipping_base.php b/rules_shipping_base.php index bd580f78ebd4bc42820c28db2f31cf9efbc1406e..776b8b3547b383fcdcae19940a1709ae6d886103 100644 --- a/rules_shipping_base.php +++ b/rules_shipping_base.php @@ -766,6 +766,35 @@ class ShippingRule { return true; } + function evaluateListFunction ($function, $args) { + # First make sure that all arguments are actually lists: + $allarrays = True; + foreach ($args as $a) { + $allarrays = $allarrays && is_array($a); + } + if (!$allarrays) { + JFactory::getApplication()->enqueueMessage(JText::sprintf('VMSHIPMENT_RULES_EVALUATE_LISTFUNCTION_ARGS', $function, $this->rulestring), 'error'); + return false; + + } + switch ($func) { + case "length": return count($args[0]); break; + case "union": + case "join": return call_user_func_array( "array_merge" , $args); break; + case "complement": return call_user_func_array( "array_diff" , $args); break; + case "intersection": return call_user_func_array( "array_intersect" , $args); break; + case "issubset": # Remove all of superset's elements to see if anything else is left: + return !array_diff($args[0], $args[1]); break; + case "contains": # Remove all of superset's elements to see if anything else is left: + # Notice the different argument order compared to issubset! + return !array_diff($args[1], $args[0]); break; + case "list_equal": return array_unique($args[0])==array_unique($args[1]); break; + default: + JFactory::getApplication()->enqueueMessage(JText::sprintf('VMSHIPMENT_RULES_EVALUATE_LISTFUNCTION_UNKNOWN', $function, $this->rulestring), 'error'); + return false; + } + } + function evaluateFunction ($function, $args) { $func = strtolower($function); // Functions with no argument: @@ -792,6 +821,20 @@ class ShippingRule { case "not": return !$args[0]; break; } } + if (count($args) == 2) { + switch ($func) { + case "digit": return substr($args[0], $args[1]-1, 1); break; + } + } + if (count($args) == 3) { + switch ($func) { + case "substring": return substr($args[0], $args[1], $args[2]); break; + } + } + // List functions + if (in_array($func, array("length", "complement", "issubset", "contains", "union", "join", "intersection", "list_equal"))) { + return evaluateListFunction ($func, args); + } // Functions with variable number of args switch ($func) { case "max": return max($args); break;