From 3c51baa4167d5a68ff010dc81525b9340c5f1329 Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Sat, 24 May 2014 14:45:13 +0200 Subject: Moved code into src subfolder and fixed docu --- src/STEStandardLibrary.php | 149 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 src/STEStandardLibrary.php (limited to 'src/STEStandardLibrary.php') diff --git a/src/STEStandardLibrary.php b/src/STEStandardLibrary.php new file mode 100644 index 0000000..915b699 --- /dev/null +++ b/src/STEStandardLibrary.php @@ -0,0 +1,149 @@ +register_tag($method, array(__CLASS__, $method)); + } + } + } + + static public function escape($ste, $params, $sub) { + if($ste->evalbool(@$params["lines"])) { + return nl2br(htmlspecialchars(str_replace("\r\n", "\n", $sub($ste)))); + } else { + return htmlspecialchars($sub($ste)); + } + } + + static public function strlen($ste, $params, $sub) { + return strlen($sub($ste)); + } + + static public function arraylen($ste, $params, $sub) { + if(empty($params["array"])) { + throw new RuntimeError("Missing array parameter in ."); + } + $a = $ste->get_var_by_name($params["array"], false); + return (is_array($a)) ? count($a) : ""; + } + + static public function inc($ste, $params, $sub) { + if(empty($params["var"])) { + throw new RuntimeError("Missing var parameter in ."); + } + $ref = &$ste->get_var_reference($params["var"], true); + $ref++; + } + + static public function dec($ste, $params, $sub) { + if(empty($params["var"])) { + throw new RuntimeError("Missing var parameter in ."); + } + $ref = &$ste->get_var_reference($params["var"], true); + $ref--; + } + + static public function date($ste, $params, $sub) { + return @strftime($sub($ste), empty($params["timestamp"]) ? @time() : (int) $params["timestamp"]); + } + + static public function in_array($ste, $params, $sub) { + if(empty($params["array"])) { + throw new RuntimeError("Missing array parameter in ."); + } + $ar = &$ste->get_var_reference($params["array"], false); + if(!is_array($ar)) { + return ""; + } + return in_array($sub($ste), $ar) ? "y" : ""; + } + + static public function join($ste, $params, $sub) { + if(empty($params["array"])) { + throw new RuntimeError("Missing array parameter in ."); + } + return implode($sub($ste), $ste->get_var_by_name($params["array"])); + } + + static public function split($ste, $params, $sub) { + if(empty($params["array"])) { + throw new RuntimeError("Missing array parameter in ."); + } + if(empty($params["delim"])) { + throw new RuntimeError("Missing delim parameter in ."); + } + $ste->set_var_by_name($params["array"], explode($params["delim"], $sub($ste))); + } + + static public function array_add($ste, $params, $sub) { + if(empty($params["array"])) { + throw new RuntimeError("Missing array parameter in ."); + } + + $ar = &$ste->get_var_reference($params["array"], true); + if(empty($params["key"])) { + $ar[] = $sub($ste); + } 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); + } +} -- cgit v1.2.3-70-g09d2