summaryrefslogtreecommitdiff
path: root/src/STEStandardLibrary.php
blob: 915b699109c8b3498bd5648de09818ec57c80dab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php

namespace kch42\ste;

class STEStandardLibrary {
	static public function _register_lib($ste) {
		foreach(get_class_methods(__CLASS__) as $method) {
			if($method[0] != "_") {
				$ste->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 <ste:arraylen>.");
		}
		$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 <ste:inc>.");
		}
		$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 <ste:dec>.");
		}
		$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 <ste:in_array>.");
		}
		$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 <ste:join>.");
		}
		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 <ste:split>.");
		}
		if(empty($params["delim"])) {
			throw new RuntimeError("Missing delim parameter in <ste:split>.");
		}
		$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 <ste:array_add>.");
		}
		
		$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 <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);
	}
}