From 964341b580bbe3e9b63592dd5e77edaafc4ab327 Mon Sep 17 00:00:00 2001 From: Reinhold Kainhofer <reinhold@kainhofer.com> Date: Fri, 26 Jul 2013 18:06:01 +0200 Subject: [PATCH] Move operator lists inside the parseRulePart function --- rules_shipping_advanced.php | 49 +++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/rules_shipping_advanced.php b/rules_shipping_advanced.php index e32ccbc..df476b0 100644 --- a/rules_shipping_advanced.php +++ b/rules_shipping_advanced.php @@ -104,24 +104,6 @@ class plgVmShipmentRules_Shipping_Advanced extends plgVmShipmentRules_Shipping_B /** Extend the shipping rules by allowing arbitrary mathematical expressions */ class ShippingRule_Advanced extends ShippingRule { - var $operators = array( - ".-" => 100, ".+" => 100, - " in " => 80, - "^" => 70, - "*" => 60, "/" => 60, "%" => 60, - "+" => 50, "-" => 50, - "<" => 40, "<=" => 40, ">" => 40, ">=" => 40, "=>" => 40, "=<" => 40, - "==" => 40, "!=" => 40, "<>" => 40, "~" => 40, - "&&" => 21, " AND " => 21, "AND" => 21, - " OR " => 20, "OR" => 20, - "=" => 10, - - "(" => 0, ")" =>0 - ); - var $unary_ops = array( - "-" => ".-", "+" => ".+" - ); - function __construct ($rule, $countries, $tax_id) { parent::__construct ($rule, $countries, $tax_id); } @@ -188,6 +170,21 @@ class ShippingRule_Advanced extends ShippingRule { // Split at all operators: $atoms = $this->tokenize_expression ($rulepart); + $operators = array( + ".-" => 100, ".+" => 100, + "in" => 80, + "^" => 70, + "*" => 60, "/" => 60, "%" => 60, + "+" => 50, "-" => 50, + "<" => 40, "<=" => 40, ">" => 40, ">=" => 40, "=>" => 40, "=<" => 40, + "==" => 40, "!=" => 40, "<>" => 40, "~" => 40, + "&&" => 21, "AND" => 21, + "OR" => 20, + "=" => 10, + "(" => 0, ")" =>0 + ); + $unary_ops = array("-" => ".-", "+" => ".+"); + // Any of these indicate a comparison and thus a condition: $condition_ops = array('<', '<=', '=<', '<>', '!=', '==', '>', '>=', '=>', '~', ' OR ', 'OR', 'AND', ' AND ', '&&', 'in', ' in '); $comparison_ops = array('<', '<=', '=<', '<>', '!=', '==', '>', '>=', '=>', '~'); @@ -266,16 +263,16 @@ class ShippingRule_Advanced extends ShippingRule { } while (true); $prev_token_operator = false; - } elseif (isset($this->unary_ops[$a]) && $prev_token_operator) { // 4) UNARY operators + } elseif (isset($unary_ops[$a]) && $prev_token_operator) { // 4) UNARY operators // Unary and binary operators need to be handled differently: // Unary operators must only pop other unary operators, never any binary operator - $unary_op = $this->unary_ops[$a]; + $unary_op = $unary_ops[$a]; // For unary operators, pop other unary operators from the stack until you reach an opening parenthesis, // an operator of lower precedence, or a right associative symbol of equal precedence. while (count($stack)>0) { // 4a) $op = array_pop ($stack); // Remove all other unary operators: - if (in_array ($op, $this->unary_ops)) { + if (in_array ($op, $unary_ops)) { array_push ($out_stack, $op); } else { // No unary operator -> add it back to stack, exit loop @@ -286,8 +283,8 @@ class ShippingRule_Advanced extends ShippingRule { array_push ($stack, $unary_op); // 4b) $prev_token_operator = true; - } elseif (isset($this->operators[$a])) { // 4) BINARY operators - $prec = $this->operators[$a]; + } elseif (isset($operators[$a])) { // 4) BINARY operators + $prec = $operators[$a]; $is_condition |= in_array($a, $condition_ops); $is_assignment |= ($a == "="); @@ -300,7 +297,7 @@ class ShippingRule_Advanced extends ShippingRule { // add it back to the stack! array_push ($stack, $op); break; - } elseif ($this->operators[$op]<$prec) { + } elseif ($operators[$op]<$prec) { // We found an operator with lower precedence, add it back to the stack! array_push ($stack, $op); // 4b) break; @@ -356,7 +353,7 @@ class ShippingRule_Advanced extends ShippingRule { array_unshift($args, $function); array_unshift($args, "FUNCTION"); $stack[] = $args; - } elseif (in_array($e, $this->unary_ops)) { // 4) unary operators + } elseif (in_array($e, $unary_ops)) { // 4) unary operators // Operator => apply to the last value on the stack if (count($stack)<1) { // 4d) JFactory::getApplication()->enqueueMessage(JText::sprintf('VMSHIPMENT_RULES_EVALUATE_SYNTAXERROR', $rulepart), 'error'); @@ -368,7 +365,7 @@ class ShippingRule_Advanced extends ShippingRule { // insert the arguments to the existing comparison instead of creating a new one $op = array ($e, $o1); // 4b) array_push ($stack, $op); // 4c) - } elseif (isset($this->operators[$e])) { // 4) binary operators + } elseif (isset($operators[$e])) { // 4) binary operators // Operator => apply to the last two values on the stack if (count($stack)<2) { // 4d) JFactory::getApplication()->enqueueMessage(JText::sprintf('VMSHIPMENT_RULES_EVALUATE_SYNTAXERROR', $rulepart), 'error'); -- GitLab