From: Maxime Bourget Date: Sat, 25 May 2013 19:37:03 +0000 (+0100) Subject: test expand and add (!...) X-Git-Url: https://delta.frontaccounting.com/gitweb/?a=commitdiff_plain;h=8c747efd28322098192ec5e2bc56633f6c6fae0c;p=textcart.git test expand and add (!...) --- diff --git a/includes/utilities.inc b/includes/utilities.inc index 54cbcc5..d0319d6 100644 --- a/includes/utilities.inc +++ b/includes/utilities.inc @@ -51,13 +51,19 @@ function expand_template($template, $value, $default_value=null) { $value = str_replace('@', $default_value, $value); #echo " default template=$template value=$value default_value=$default_value
"; - #evaluate expression + #evaluate arithmtic expression if(preg_match('/^\((.*)\)$/', $value, $matches)) { - // keep only arithmetical expression - $to_eval = preg_replace('/[a-zA-Z]*/', "", $matches[1]); - #echo "eval trimmed '$matches[1]' => '$to_eval'
"; - eval("\$value=$to_eval;"); - #echo "replaced template=$template value=$value default_value=$default_value
"; + $expression = $matches[1]; + if(preg_match('/^!(.*)/', $expression, $matches)) { + $value = $matches[1]; + } + else { + // keep only arithmetical expression + $to_eval = preg_replace('/[a-zA-Z]*/', "", $expression); + #echo "eval trimmed '$matches[1]' => '$to_eval'
"; + eval("\$value=$to_eval;"); + #echo "replaced template=$template value=$value default_value=$default_value
"; + } } return $value; } diff --git a/test/expandTest.php b/test/expandTest.php new file mode 100644 index 0000000..9dc3fa3 --- /dev/null +++ b/test/expandTest.php @@ -0,0 +1,39 @@ +assertEquals(expand_template($template, $value, $default), $result); + } + + public function expandables() { + return array( + array(null, '5', null, '5') /** normal **/ + ,array('2', null, null, '2') /** use template **/ + + ,array('10', '5', null, '5') /** value override template one **/ + ,array('-#-', '5', 'default', '-5-') /** but not if # in it */ + ,array('-@-', '5', 'default', '5') /** value override if formulat use default */ + ,array('#-@', '5', 'default', '5-default') /** def can be use in template formula **/ + ,array('-@-', null, 'default', '-default-') + + ,array('-#-', '<<@>>', 'default', '-<>-') /** ???? */ + + ,array('10', ':#:', null, ':10:') /** use template in value formula **/ + ,array('10', ':#:@', 'default', ':10:default') /** use template and default in value formula **/ + + ,array('(10)', '5', null, '10') /** template formula win **/ + ,array('(@)', '5', '2+7', '9') /** template formula win **/ + ,array('(!@)', '5', '2+7', '2+7') /** don't evaluate the expression **/ + + + ); + } + +} +?>