|
2 | 2 |
|
3 | 3 | namespace Illuminate\Support;
|
4 | 4 |
|
5 |
| -class Js |
| 5 | +use Illuminate\Contracts\Support\Arrayable; |
| 6 | +use Illuminate\Contracts\Support\Htmlable; |
| 7 | +use Illuminate\Contracts\Support\Jsonable; |
| 8 | +use JsonSerializable; |
| 9 | + |
| 10 | +class Js implements Htmlable |
6 | 11 | {
|
7 | 12 | /**
|
8 |
| - * Convert an expression into a valid JavaScript object, JSON string, or string. |
| 13 | + * The JavaScript string. |
| 14 | + * |
| 15 | + * @var string |
| 16 | + */ |
| 17 | + protected $js; |
| 18 | + |
| 19 | + /** |
| 20 | + * Flags that should be used when encoding to JSON. |
| 21 | + * |
| 22 | + * @var int |
| 23 | + */ |
| 24 | + protected const REQUIRED_FLAGS = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_THROW_ON_ERROR; |
| 25 | + |
| 26 | + /** |
| 27 | + * Create a new class instance. |
| 28 | + * |
| 29 | + * @param mixed $data |
| 30 | + * @param int|null $flags |
| 31 | + * @param int $depth |
| 32 | + * @return void |
| 33 | + * |
| 34 | + * @throws \JsonException |
| 35 | + */ |
| 36 | + public function __construct($data, $flags = 0, $depth = 512) |
| 37 | + { |
| 38 | + $this->js = $this->convertDataToJavaScriptExpression($data, $flags, $depth); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Create a new JavaScript string from the given data. |
9 | 43 | *
|
10 |
| - * @param mixed $expression |
11 |
| - * @param int|null $options |
| 44 | + * @param mixed $data |
| 45 | + * @param int $flags |
| 46 | + * @param int $depth |
| 47 | + * @return static |
| 48 | + * |
| 49 | + * @throws \JsonException |
| 50 | + */ |
| 51 | + public static function from($data, $flags = 0, $depth = 512) |
| 52 | + { |
| 53 | + return new static($data, $flags, $depth); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Convert the given data to a JavaScript expression. |
| 58 | + * |
| 59 | + * @param mixed $data |
| 60 | + * @param int $flags |
12 | 61 | * @param int $depth
|
13 | 62 | * @return string
|
| 63 | + * |
| 64 | + * @throws \JsonException |
14 | 65 | */
|
15 |
| - public static function from($expression, $options = null, $depth = 512) |
| 66 | + protected function convertDataToJavaScriptExpression($data, $flags = 0, $depth = 512) |
16 | 67 | {
|
17 |
| - if (is_object($expression) || is_array($expression)) { |
18 |
| - $base64 = base64_encode(json_encode($expression, $options, $depth)); |
| 68 | + if ($data instanceof self) { |
| 69 | + return $data->toHtml(); |
| 70 | + } |
19 | 71 |
|
20 |
| - return "JSON.parse(atob('{$base64}'))"; |
| 72 | + $json = $this->jsonEncode($data, $flags, $depth); |
| 73 | + |
| 74 | + if (is_string($data)) { |
| 75 | + return "'".substr($json, 1, -1)."'"; |
21 | 76 | }
|
22 | 77 |
|
23 |
| - if (is_string($expression)) { |
24 |
| - return "'".str_replace("'", "\'", str_replace('\\', '\\\\', $expression))."'"; |
| 78 | + return $this->convertJsonToJavaScriptExpression($json, $flags); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Encode the given data as JSON. |
| 83 | + * |
| 84 | + * @param mixed $data |
| 85 | + * @param int $flags |
| 86 | + * @param int $depth |
| 87 | + * @return string |
| 88 | + * |
| 89 | + * @throws \JsonException |
| 90 | + */ |
| 91 | + protected function jsonEncode($data, $flags = 0, $depth = 512) |
| 92 | + { |
| 93 | + if ($data instanceof Jsonable) { |
| 94 | + return $data->toJson($flags | static::REQUIRED_FLAGS); |
| 95 | + } |
| 96 | + |
| 97 | + if ($data instanceof Arrayable && ! ($data instanceof JsonSerializable)) { |
| 98 | + $data = $data->toArray(); |
| 99 | + } |
| 100 | + |
| 101 | + return json_encode($data, $flags | static::REQUIRED_FLAGS, $depth); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Convert the given JSON to a JavaScript expression. |
| 106 | + * |
| 107 | + * @param string $json |
| 108 | + * @param int $flags |
| 109 | + * @return string |
| 110 | + * |
| 111 | + * @throws \JsonException |
| 112 | + */ |
| 113 | + protected function convertJsonToJavaScriptExpression($json, $flags = 0) |
| 114 | + { |
| 115 | + if ('[]' === $json || '{}' === $json) { |
| 116 | + return $json; |
25 | 117 | }
|
26 | 118 |
|
27 |
| - return json_encode($expression, $options, $depth); |
| 119 | + if (Str::startsWith($json, ['"', '{', '['])) { |
| 120 | + return "JSON.parse('".substr(json_encode($json, $flags | static::REQUIRED_FLAGS), 1, -1)."')"; |
| 121 | + } |
| 122 | + |
| 123 | + return $json; |
| 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(); |
28 | 144 | }
|
29 | 145 | }
|
0 commit comments