From 07bac0bf75aa598e63f68949eabbd621c552466a Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Mon, 2 Jul 2012 22:19:28 +0200 Subject: set:array_filter added --- docu/language_definition.html | 8 +++++++ stupid_template_engine.php | 51 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/docu/language_definition.html b/docu/language_definition.html index 4cf4aaf..e311907 100644 --- a/docu/language_definition.html +++ b/docu/language_definition.html @@ -517,5 +517,13 @@

ste:array_add

Adding an element to an array. The array parameter takes the variable name of the array. The key parameter takes the array key to map the value to, if omitted, the value will be appended to the end of the array. The value is the tag's content.

+

ste:array_filter

+

Filter out array elements by multiple criterias.

+

All the parameters are names of array variables.

+

array – The array to filter.

+

keep_by_keys – Keep elements with the keys in this array.

+

keep_by_values – Keep elements with the values in this array.

+

delete_by_keys – Delete elements with the keys in this array.

+

delete_by_values – Delete elements with the values in this array.

diff --git a/stupid_template_engine.php b/stupid_template_engine.php index e8e78ec..b231e12 100644 --- a/stupid_template_engine.php +++ b/stupid_template_engine.php @@ -1434,6 +1434,57 @@ class STEStandardLibrary else $ar[$params["key"]] = $sub($ste); } + + static public function array_filter($ste, $params, $sub) + { + if(empty($params["array"])) + throw new RuntimeError("Missing array parameter in ."); + + $ar = $ste->get_var_by_name($params["array"]); + if(!is_array($ar)) + throw new RuntimeError("Variable at 'array' is not an array."); + + $keys = array_keys($ar); + + if(!empty($params["keep_by_keys"])) + { + $keep_by_keys = &$ste->get_var_reference($params["keep_by_keys"], False); + if(!is_array($keep_by_keys)) + throw new RuntimeError("Variable at 'keep_by_keys' is not an array."); + $delkeys = array_filter($keys, function($k) use ($keep_by_keys) { return !in_array($k, $keep_by_keys); }); + foreach($delkeys as $dk) + unset($ar[$dk]); + $keys = array_keys($ar); + } + if(!empty($params["keep_by_values"])) + { + $keep_by_values = &$ste->get_var_reference($params["keep_by_values"], False); + if(!is_array($keep_by_values)) + throw new RuntimeError("Variable at 'keep_by_values' is not an array."); + $ar = array_filter($ar, function($v) use ($keep_by_values) { return in_array($v, $keep_by_values); }); + $keys = array_keys($ar); + } + if(!empty($params["delete_by_keys"])) + { + $delete_by_keys = &$ste->get_var_reference($params["delete_by_keys"], False); + if(!is_array($delete_by_keys)) + throw new RuntimeError("Variable at 'delete_by_keys' is not an array."); + $delkeys = array_filter($keys, function($k) use ($delete_by_keys) { return in_array($k, $delete_by_keys); }); + foreach($delkeys as $dk) + unset($ar[$dk]); + $keys = array_keys($ar); + } + if(!empty($params["delete_by_values"])) + { + $delete_by_values = &$ste->get_var_reference($params["delete_by_values"], False); + if(!is_array($delete_by_values)) + throw new RuntimeError("Variable at 'delete_by_values' is not an array."); + $ar = array_filter($ar, function($v) use ($delete_by_values) { return !in_array($v, $delete_by_values); }); + $keys = array_keys($ar); + } + + $ste->set_var_by_name($params["array"], $ar); + } } ?> \ No newline at end of file -- cgit v1.2.3-54-g00ecf