1 | <?php |
||||
2 | |||||
3 | namespace HTML; |
||||
4 | |||||
5 | class js |
||||
6 | { |
||||
7 | public static function js_str($s) |
||||
8 | { |
||||
9 | return '"' . addcslashes($s, "\0..\37\"\\") . '"'; |
||||
10 | } |
||||
11 | |||||
12 | public static function js_array(array $array) |
||||
13 | { |
||||
14 | $temp = array_map(function ($index) { |
||||
15 | return self::js_str($index); |
||||
16 | }, $array); |
||||
17 | |||||
18 | return '[' . implode(',', $temp) . ']'; |
||||
19 | } |
||||
20 | |||||
21 | /** |
||||
22 | * Create javascript variable. |
||||
23 | * |
||||
24 | * @param string $variable variable name |
||||
25 | * @param string|array|object $content array object will turn into JSON javascript object |
||||
26 | * |
||||
27 | * @return string var name = content; |
||||
28 | */ |
||||
29 | public static function var(string $variable, $content) |
||||
30 | { |
||||
31 | if (\ArrayHelper\helper::is_iterable($content)) { |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
32 | $content = \JSON\json::json($content, false, false); |
||||
33 | } |
||||
34 | if (false !== strpos($content, ';')) { |
||||
35 | $content = rtrim($content, ';'); |
||||
36 | } |
||||
37 | |||||
38 | return 'var ' . $variable . ' = `' . $content . '`;'; |
||||
0 ignored issues
–
show
Are you sure
$content of type array|object|string can be used in concatenation ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
39 | } |
||||
40 | } |
||||
41 |