From 3c5f659385f99d66f6d6f7cdc1bb54f1174ca694 Mon Sep 17 00:00:00 2001 From: Reinhold Kainhofer <reinhold@kainhofer.com> Date: Thu, 25 Jul 2013 20:53:17 +0200 Subject: [PATCH] Implament in operators and array function --- rules_shipping_advanced.php | 8 ++++++-- rules_shipping_base.php | 4 +++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/rules_shipping_advanced.php b/rules_shipping_advanced.php index 21bc32d..1d68c18 100644 --- a/rules_shipping_advanced.php +++ b/rules_shipping_advanced.php @@ -106,6 +106,7 @@ class plgVmShipmentRules_Shipping_Advanced extends plgVmShipmentRules_Shipping_B class ShippingRule_Advanced extends ShippingRule { var $operators = array( ".-" => 100, ".+" => 100, + " in " => 80, "^" => 70, "*" => 60, "/" => 60, "%" => 60, "+" => 50, "-" => 50, @@ -130,7 +131,10 @@ class ShippingRule_Advanced extends ShippingRule { $str_re = '/("[^"]*"|\'[^\']*\')/'; $strings = preg_split($str_re, $expression, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY); // Then split all other parts of the expression at the operators - $op_re = ':\s*( OR |&&| AND |<=|=>|>=|=>|<>|!=|==|<|=|>|~|\+|-|\*|/|%|\(|\)|\^|,)\s*:'; +// TODO 1: This fails for expressions like "(1>0) OR zip in array(1,4,1080)" +// TODO 2: Better handle the spaces around in-line text operators! +// (state2 in array("TX", "WS", "MS")) OR zip in array(1,4,1080) + $op_re = ':\s*( OR |&&| AND | in |<=|=>|>=|=>|<>|!=|==|<|=|>|~|\+|-|\*|/|%|\(|\)|\^|,)\s*:'; $atoms = array(); foreach ($strings as $s) { if (preg_match($str_re, $s)) { @@ -185,7 +189,7 @@ class ShippingRule_Advanced extends ShippingRule { $atoms = $this->tokenize_expression ($rulepart); // Any of these indicate a comparison and thus a condition: - $condition_ops = array('<', '<=', '=<', '<>', '!=', '==', '>', '>=', '=>', '~', ' OR ', 'OR', 'AND', ' AND ', '&&'); + $condition_ops = array('<', '<=', '=<', '<>', '!=', '==', '>', '>=', '=>', '~', ' OR ', 'OR', 'AND', ' AND ', '&&', 'in', ' in '); $comparison_ops = array('<', '<=', '=<', '<>', '!=', '==', '>', '>=', '=>', '~'); $is_condition = false; $is_assignment = false; diff --git a/rules_shipping_base.php b/rules_shipping_base.php index e84e2f1..258aae3 100644 --- a/rules_shipping_base.php +++ b/rules_shipping_base.php @@ -761,6 +761,7 @@ class ShippingRule { switch ($func) { case "max": return max($args); break; case "min": return min($args); break; + case "array": return $args; break; } // No known function matches => print an error, return 0 JFactory::getApplication()->enqueueMessage(JText::sprintf('VMSHIPMENT_RULES_EVALUATE_UNKNOWN_FUNCTION', $function, $this->rulestring), 'error'); @@ -810,7 +811,8 @@ class ShippingRule { case '&&': case 'AND': case ' AND ': $res = true; foreach ($args as $a) { $res = ($res && $a); }; break; - case 'in': $needle = array_shift($args); $res = in_array($needle, $args); break; + case 'in': + case ' in ': $res = in_array($args[0], $args[1]); break; // Comparisons: case '<': -- GitLab