summaryrefslogtreecommitdiff
path: root/src/ste/STEStandardLibrary.php
blob: cd23fdfd1ded5b33c43381067e11e10ea148fc35 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php

namespace kch42\ste;

class STEStandardLibrary
{
    public static function _register_lib(STECore $ste): void
    {
        foreach (get_class_methods(__CLASS__) as $method) {
            if ($method[0] != "_") {
                $ste->register_tag($method, [__CLASS__, $method]);
            }
        }
    }

    /**
     * @param STECore $ste
     * @param array $params
     * @param callable $sub
     * @return string
     */
    public static function escape(STECore $ste, array $params, callable $sub)
    {
        if ($ste->evalbool(@$params["lines"])) {
            return nl2br(htmlspecialchars(str_replace("\r\n", "\n", $sub($ste))));
        } else {
            return htmlspecialchars($sub($ste));
        }
    }

    /**
     * @param STECore $ste
     * @param array $params
     * @param callable $sub
     * @return string
     */
    public static function strlen(STECore $ste, array $params, callable $sub)
    {
        return strlen($sub($ste));
    }

    /**
     * @param STECore $ste
     * @param array $params
     * @param callable $sub
     * @return string
     * @throws RuntimeError
     */
    public static function arraylen(STECore $ste, array $params, callable $sub)
    {
        if (empty($params["array"])) {
            throw new RuntimeError("Missing array parameter in <ste:arraylen>.");
        }
        $a = $ste->get_var_by_name($params["array"]);
        return (is_array($a)) ? count($a) : "";
    }

    /**
     * @param STECore $ste
     * @param array $params
     * @param callable $sub
     * @throws RuntimeError
     */
    public static function inc(STECore $ste, array $params, callable $sub)
    {
        if (empty($params["var"])) {
            throw new RuntimeError("Missing var parameter in <ste:inc>.");
        }
        $ref = &$ste->get_var_reference($params["var"], true);
        $ref++;
    }

    /**
     * @param STECore $ste
     * @param array $params
     * @param callable $sub
     * @throws RuntimeError
     */
    public static function dec(STECore $ste, array $params, callable $sub)
    {
        if (empty($params["var"])) {
            throw new RuntimeError("Missing var parameter in <ste:dec>.");
        }
        $ref = &$ste->get_var_reference($params["var"], true);
        $ref--;
    }

    /**
     * @param STECore $ste
     * @param array $params
     * @param callable $sub
     * @return string
     */
    public static function date(STECore $ste, array $params, callable $sub)
    {
        return @strftime($sub($ste), empty($params["timestamp"]) ? @time() : (int) $params["timestamp"]);
    }

    /**
     * @param STECore $ste
     * @param array $params
     * @param callable $sub
     * @return string
     * @throws RuntimeError
     */
    public static function in_array(STECore $ste, array $params, callable $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" : "";
    }

    /**
     * @param STECore $ste
     * @param array $params
     * @param callable $sub
     * @return string
     * @throws RuntimeError
     */
    public static function join(STECore $ste, array $params, callable $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"]));
    }

    /**
     * @param STECore $ste
     * @param array $params
     * @param callable $sub
     * @throws RuntimeError
     */
    public static function split(STECore $ste, array $params, callable $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)));
    }

    /**
     * @param STECore $ste
     * @param array $params
     * @param callable $sub
     * @throws RuntimeError
     */
    public static function array_add(STECore $ste, array $params, callable $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);
        }
    }

    /**
     * @param STECore $ste
     * @param array $params
     * @param callable $sub
     * @throws RuntimeError
     */
    public static function array_filter(STECore $ste, array $params, callable $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);
    }
}