1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPMV\js; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Javascript utilities. |
7
|
|
|
* PHPMV\js$JavascriptUtils |
8
|
|
|
* This class is part of php-ui-common |
9
|
|
|
* |
10
|
|
|
* @author jc |
11
|
|
|
* @version 1.0.0 |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
class JavascriptUtils { |
15
|
|
|
|
16
|
|
|
static private array $removeQuote = ["start" => "!!%", "end" => "%!!"]; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Returns a JSON string from an object. |
20
|
|
|
* |
21
|
|
|
* @param mixed $object |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
2 |
|
public static function toJSON($object): string { |
25
|
2 |
|
if (\is_object($object)) { |
26
|
2 |
|
if (\method_exists($object, 'toArray')) { |
27
|
1 |
|
$object = $object->toArray(); |
28
|
|
|
} else { |
29
|
2 |
|
$object = (array) $object; |
30
|
|
|
} |
31
|
|
|
} |
32
|
2 |
|
return \json_encode($object, \JSON_PRETTY_PRINT | \JSON_NUMERIC_CHECK); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Return a javascript object from a php associative array. |
37
|
|
|
* |
38
|
|
|
* @param array $array |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
1 |
|
public static function arrayToJsObject(array $array): string { |
42
|
1 |
|
$res = []; |
43
|
1 |
|
foreach ($array as $k => $v) { |
44
|
1 |
|
if (\is_object($v) || \is_array($v)) { |
45
|
1 |
|
$v = self::toJSON($v); |
46
|
1 |
|
} elseif (\is_bool($v)) { |
47
|
1 |
|
$v = ($v) ? 'true' : 'false'; |
48
|
1 |
|
} elseif (\is_string($v)) { |
49
|
1 |
|
$v = '"' . $v . '"'; |
50
|
|
|
} |
51
|
1 |
|
$res[] = "$k: $v"; |
52
|
|
|
} |
53
|
1 |
|
return '{' . \implode(',', $res) . '}'; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Add script tags to a javascript code. |
58
|
|
|
* |
59
|
|
|
* @param string $script |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
1 |
|
public static function wrapScript(string $script): string { |
63
|
1 |
|
if ($script == null) { |
64
|
1 |
|
return ''; |
65
|
|
|
} |
66
|
1 |
|
if (\substr($script, 0, \strlen('<script>')) !== '<script>') { |
67
|
1 |
|
$script = "<script>$script</script>"; |
68
|
|
|
} |
69
|
1 |
|
return $script; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public static function cleanJSONFunctions(string $json): string { |
73
|
|
|
$pattern = '/(("|\')' . self::$removeQuote['start'] . ')|(' . self::$removeQuote['end'] . '("|\'))/'; |
74
|
|
|
return \preg_replace($pattern, '', $json); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public static function removeQuotes(string $body): string { |
78
|
|
|
return self::$removeQuote["start"] . $body . self::$removeQuote["end"]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public static function generateFunction(string $body, array $params = [], bool $needRemoveQuote = true): string { |
82
|
|
|
if ($needRemoveQuote) { |
83
|
|
|
return self::removeQuotes("function(" . implode(",", $params) . "){" . $body . "}"); |
84
|
|
|
} |
85
|
|
|
return "function(" . implode(",", $params) . "){" . $body . "}"; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public static function declareVariable(string $type, string $name, $value, bool $lineBreak = true): string { |
89
|
|
|
$declaration = $type . " " . $name . " = " . $value . ";"; |
90
|
|
|
if ($lineBreak) $declaration .= PHP_EOL; |
91
|
|
|
return $declaration; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public static function kebabToPascal(string $string): string { |
95
|
|
|
$string[0] = \strtoupper($string[0]); |
96
|
|
|
$pattern = '/(-\w{1})/'; |
97
|
|
|
return \preg_replace_callback($pattern, |
98
|
|
|
function ($matches) { |
99
|
|
|
return \strtoupper($matches[1][1]); |
100
|
|
|
}, $string); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|