Skip to content
Snippets Groups Projects
Commit 069db14b authored by Reinhold Kainhofer's avatar Reinhold Kainhofer
Browse files

Implement list handling functions and digit and substring functions

parent 1b13640e
Branches
Tags
No related merge requests found
......@@ -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')"
......@@ -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;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment