diff options
-rw-r--r-- | auto_prepend.php | 4 | ||||
-rw-r--r-- | it_text.class | 28 | ||||
-rwxr-xr-x | tests/autoprepend.t | 4 | ||||
-rwxr-xr-x | tests/it_text.t | 3 |
4 files changed, 25 insertions, 14 deletions
diff --git a/auto_prepend.php b/auto_prepend.php index f4f0b1c..ba42293 100644 --- a/auto_prepend.php +++ b/auto_prepend.php @@ -82,7 +82,9 @@ function T($label, $language = null, $values = null) if (is_array($language)) # Need to swap params? list($language, $values) = array($values, $language); - return is_array($values) ? $GLOBALS['it_text']->etext($label, array_map(array("it_html", "Q"), $values), $language) : $GLOBALS['it_text']->text($label, $language); + $result = $GLOBALS['it_text']->text($label, $language); + + return strpos($result, "{") === false ? $result : it_text::transmogrify($result, array_map(array("it_html", "Q"), (array)$values), $label, $GLOBALS['it_text']->allowedfuncs); } /** diff --git a/it_text.class b/it_text.class index 29bc2d1..0045370 100644 --- a/it_text.class +++ b/it_text.class @@ -36,6 +36,7 @@ class it_text * @param $p['global'] store text object in global it_text for global functions (default: true) * @param $p['phpfile'] optional texts file(s), default: all texts.php in include path * @param $p['phpfiles'] text files to load in addition to $p['phpfile'] (Note: $p['phpfile'] defaults to all texts.php files in include path) + * @param $p['transmogrifiers'] comma separated functions that may be called by using {foo:bar} (foo will be called with bar as argument) in T() */ function it_text($p = array()) { @@ -47,10 +48,11 @@ function it_text($p = array()) $p['phpfiles'][] = $phpfile; } - $this->p = ($p += array( + $this->p = ($p += (array)$GLOBALS['it_text_defaultconfig'] + array( 'global' => true, 'phpfiles' => array_unique(array_merge((array)$p['phpfiles'], (array)$p['phpfile'])), )); + $this->allowedfuncs = array_flip(explode(",", $p['transmogrifiers'])); # Read and merge texts from php files if none defined yet foreach ($p['phpfiles'] as $phpfile) @@ -170,7 +172,7 @@ function text($label, $language = null) */ function etext($label, $values = null, $language = null) { - return $this->transmogrify($this->text($label, $language), $values, $label); + return $this->transmogrify($this->text($label, $language), $values, $label, $this->allowedfuncs); } @@ -226,22 +228,26 @@ function set($label, $text = null, $language = null) /** - * Replaces variables of the form {obj.var} with value, e.g. {user.name} + * Replaces variables of the form {obj.var} with value from $values, e.g. {user.name}, or result of a func, e.g. {LU:foo.html} * NOTE: Invalid object names or non-existing variables are simply deleted. */ -function transmogrify($text, $values = null, $label = null) +function transmogrify($text, $values = null, $label = null, $allowedfuncs = null) { - foreach (preg_split('/{([\w.]+)}/', $text, -1, PREG_SPLIT_DELIM_CAPTURE) as $i => $part) + foreach (preg_split('#{([^}]*)}#', $text, -1, PREG_SPLIT_DELIM_CAPTURE) as $i => $part) { if ($i % 2) # odd offsets are delimiters, i.e. braces to be replaced { - $value = $values ? $values : $GLOBALS; - foreach (explode(".", $part) as $key) + if (it::match('^[\w.]+$', $part)) { - $value = is_object($value) ? $value->$key : $value[$key]; - if ($value === null && $values && $label) # do not test in $GLOBALS mode - it::error(array('title' => "No value given for text variable {" . $key ."} in label $label", 'backtraceskip' => 3)); - } + $value = $values ? $values : $GLOBALS; + foreach (explode(".", $part) as $key) + { + $value = is_object($value) ? $value->$key : $value[$key]; + if ($value === null && $values && $label) # do not test in $GLOBALS mode + it::error(array('title' => "No value given for text variable {" . $key ."} in label $label", 'backtraceskip' => 3)); + } + } else + $value = (list($func, $arg) = it::match('^([\w:]+)\((.*)\)$', $part)) && isset($allowedfuncs[$func]) ? call_user_func($func, $arg) : "{" . $part . "}"; $result .= $GLOBALS['debug_texts'] ? "</span>$value<span style='background:#8F8'>" : $value; } diff --git a/tests/autoprepend.t b/tests/autoprepend.t index bf40605..01da49e 100755 --- a/tests/autoprepend.t +++ b/tests/autoprepend.t @@ -9,13 +9,13 @@ $GLOBALS['it_text']->statictext = array( is( T('foo'), - "bar {v1}", + "bar ", "simple T()" ); is( T('foo', 'en'), - "qux {v1}", + "qux ", "simple T() with language" ); diff --git a/tests/it_text.t b/tests/it_text.t index bc4d5c0..5629d51 100755 --- a/tests/it_text.t +++ b/tests/it_text.t @@ -16,3 +16,6 @@ is(it_text::transmogrify("X{foo}{bar}", array('foo' => 1, 'bar' => 2)), "X12"); is(it_text::transmogrify("X{foo}", array('foo' => "&")), "X&"); is(it_text::transmogrify("X{x}", $obj), "Xattr"); is(it_text::transmogrify("X{y.z}", $obj), "Xattr"); +is(it_text::transmogrify("{mb_strlen(xxx)}", null, null, ['mb_strlen' => 1]), "3"); +is(it_text::transmogrify("{it::ucfirst(xxx)}", null, null, ['it::ucfirst' => 1]), "Xxx"); +is(it_text::transmogrify("{format violation}"), '{format violation}'); |