summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docu/language_definition.html8
-rw-r--r--stupid_template_engine.php51
2 files changed, 59 insertions, 0 deletions
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 @@
<h3 id="stdlib_array_add">ste:array_add</h3>
<p>Adding an element to an array. The <code>array</code> parameter takes the variable name of the array. The <code>key</code> 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.</p>
+ <h3 id="stdlib_array_filter">ste:array_filter</h3>
+ <p>Filter out array elements by multiple criterias.</p>
+ <p>All the parameters are names of array variables.</p>
+ <p><code>array</code> – The array to filter.</p>
+ <p><code>keep_by_keys</code> – Keep elements with the keys in this array.</p>
+ <p><code>keep_by_values</code> – Keep elements with the values in this array.</p>
+ <p><code>delete_by_keys</code> – Delete elements with the keys in this array.</p>
+ <p><code>delete_by_values</code> – Delete elements with the values in this array.</p>
</body>
</html>
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 <ste:array_filter>.");
+
+ $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