7
7
use Illuminate \Contracts \Support \Jsonable ;
8
8
use JsonSerializable ;
9
9
10
- class JsString implements Htmlable
10
+ class Js implements Htmlable
11
11
{
12
12
/**
13
- * Flags that must always be used when encoding to JSON for JsString.
14
- *
15
- * @var int
16
- */
17
- protected const REQUIRED_FLAGS = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_THROW_ON_ERROR ;
18
-
19
- /**
20
- * The javascript string.
13
+ * The JavaScript string.
21
14
*
22
15
* @var string
23
16
*/
24
17
protected $ js ;
25
18
26
19
/**
27
- * Create a new JsString from data.
28
- *
29
- * @param mixed $data
30
- * @param int $flags
31
- * @param int $depth
32
- * @return static
20
+ * Flags that should be used when encoding to JSON.
33
21
*
34
- * @throws \JsonException
22
+ * @var int
35
23
*/
36
- public static function from ($ data , $ flags = 0 , $ depth = 512 )
37
- {
38
- return new static ($ data , $ flags , $ depth );
39
- }
24
+ protected const REQUIRED_FLAGS = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_THROW_ON_ERROR ;
40
25
41
26
/**
42
- * Create a new JsString .
27
+ * Create a new class instance .
43
28
*
44
29
* @param mixed $data
45
30
* @param int|null $flags
@@ -54,27 +39,22 @@ public function __construct($data, $flags = 0, $depth = 512)
54
39
}
55
40
56
41
/**
57
- * Get string representation of data for use in HTML .
42
+ * Create a new JavaScript string from the given data .
58
43
*
59
- * @return string
60
- */
61
- public function toHtml ()
62
- {
63
- return $ this ->js ;
64
- }
65
-
66
- /**
67
- * Get string representation of data for use in HTML.
44
+ * @param mixed $data
45
+ * @param int $flags
46
+ * @param int $depth
47
+ * @return static
68
48
*
69
- * @return string
49
+ * @throws \JsonException
70
50
*/
71
- public function __toString ( )
51
+ public static function from ( $ data , $ flags = 0 , $ depth = 512 )
72
52
{
73
- return $ this -> toHtml ( );
53
+ return new static ( $ data , $ flags , $ depth );
74
54
}
75
55
76
56
/**
77
- * Convert data to a JavaScript expression.
57
+ * Convert the given data to a JavaScript expression.
78
58
*
79
59
* @param mixed $data
80
60
* @param int $flags
@@ -99,7 +79,7 @@ protected function convertDataToJavaScriptExpression($data, $flags = 0, $depth =
99
79
}
100
80
101
81
/**
102
- * Encode data as JSON.
82
+ * Encode the given data as JSON.
103
83
*
104
84
* @param mixed $data
105
85
* @param int $flags
@@ -122,7 +102,7 @@ protected function jsonEncode($data, $flags = 0, $depth = 512)
122
102
}
123
103
124
104
/**
125
- * Convert JSON to a JavaScript expression.
105
+ * Convert the given JSON to a JavaScript expression.
126
106
*
127
107
* @param string $json
128
108
* @param int $flags
@@ -137,11 +117,29 @@ protected function convertJsonToJavaScriptExpression($json, $flags = 0)
137
117
}
138
118
139
119
if (Str::startsWith ($ json , ['" ' , '{ ' , '[ ' ])) {
140
- $ json = json_encode ($ json , $ flags | static ::REQUIRED_FLAGS );
141
-
142
- return "JSON.parse(' " .substr ($ json , 1 , -1 )."') " ;
120
+ return "JSON.parse(' " .substr (json_encode ($ json , $ flags | static ::REQUIRED_FLAGS ), 1 , -1 )."') " ;
143
121
}
144
122
145
123
return $ json ;
146
124
}
125
+
126
+ /**
127
+ * Get the string representation of the data for use in HTML.
128
+ *
129
+ * @return string
130
+ */
131
+ public function toHtml ()
132
+ {
133
+ return $ this ->js ;
134
+ }
135
+
136
+ /**
137
+ * Get the string representation of the data for use in HTML.
138
+ *
139
+ * @return string
140
+ */
141
+ public function __toString ()
142
+ {
143
+ return $ this ->toHtml ();
144
+ }
147
145
}
0 commit comments