Debian Codegraph @

lib/3rdParty/composer/illuminate/support/Js.php

raw symbols SCIP
1 <?php
2
3 namespace Illuminate\Support;
4
5 use Illuminate\Contracts\Support\Arrayable;
6 use Illuminate\Contracts\Support\Htmlable;
7 use Illuminate\Contracts\Support\Jsonable;
8 use JsonSerializable;
9 use Stringable;
10 use UnitEnum;
11
12 class Js implements Htmlable, Stringable
13 {
14 /**
15 * The JavaScript string.
16 *
17 * @var string
18 */
19 protected $js;
20
21 /**
22 * Flags that should be used when encoding to JSON.
23 *
24 * @var int
25 */
26 protected const REQUIRED_FLAGS = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_THROW_ON_ERROR;
27
28 /**
29 * Create a new class instance.
30 *
31 * @param mixed $data
32 * @param int|null $flags
33 * @param int $depth
34 *
35 * @throws \JsonException
36 */
37 public function __construct($data, $flags = 0, $depth = 512)
38 {
39 $this->js = $this->convertDataToJavaScriptExpression($data, $flags, $depth);
40 }
41
42 /**
43 * Create a new JavaScript string from the given data.
44 *
45 * @param mixed $data
46 * @param int $flags
47 * @param int $depth
48 * @return static
49 *
50 * @throws \JsonException
51 */
52 public static function from($data, $flags = 0, $depth = 512)
53 {
54 return new static($data, $flags, $depth);
55 }
56
57 /**
58 * Convert the given data to a JavaScript expression.
59 *
60 * @param mixed $data
61 * @param int $flags
62 * @param int $depth
63 * @return string
64 *
65 * @throws \JsonException
66 */
67 protected function convertDataToJavaScriptExpression($data, $flags = 0, $depth = 512)
68 {
69 if ($data instanceof self) {
70 return $data->toHtml();
71 }
72
73 if ($data instanceof Htmlable &&
74 ! $data instanceof Arrayable &&
75 ! $data instanceof Jsonable &&
76 ! $data instanceof JsonSerializable) {
77 $data = $data->toHtml();
78 }
79
80 if ($data instanceof UnitEnum) {
81 $data = enum_value($data);
82 }
83
84 $json = static::encode($data, $flags, $depth);
85
86 if (is_string($data)) {
87 return "'".substr($json, 1, -1)."'";
88 }
89
90 return $this->convertJsonToJavaScriptExpression($json, $flags);
91 }
92
93 /**
94 * Encode the given data as JSON.
95 *
96 * Invalid UTF-8 sequences are replaced with � instead of throwing.
97 *
98 * @param mixed $data
99 * @param int $flags
100 * @param int $depth
101 * @return string
102 *
103 * @throws \JsonException
104 */
105 public static function encode($data, $flags = 0, $depth = 512)
106 {
107 if ($data instanceof Jsonable) {
108 return $data->toJson($flags | static::REQUIRED_FLAGS);
109 }
110
111 if ($data instanceof Arrayable && ! ($data instanceof JsonSerializable)) {
112 $data = $data->toArray();
113 }
114
115 return json_encode($data, $flags | static::REQUIRED_FLAGS | JSON_INVALID_UTF8_SUBSTITUTE, $depth);
116 }
117
118 /**
119 * Convert the given JSON to a JavaScript expression.
120 *
121 * @param string $json
122 * @param int $flags
123 * @return string
124 *
125 * @throws \JsonException
126 */
127 protected function convertJsonToJavaScriptExpression($json, $flags = 0)
128 {
129 if ($json === '[]' || $json === '{}') {
130 return $json;
131 }
132
133 if (Str::startsWith($json, ['"', '{', '['])) {
134 return "JSON.parse('".substr(json_encode($json, $flags | static::REQUIRED_FLAGS), 1, -1)."')";
135 }
136
137 return $json;
138 }
139
140 /**
141 * Get the string representation of the data for use in HTML.
142 *
143 * @return string
144 */
145 public function toHtml()
146 {
147 return $this->js;
148 }
149
150 /**
151 * Get the string representation of the data for use in HTML.
152 *
153 * @return string
154 */
155 public function __toString()
156 {
157 return $this->toHtml();
158 }
159 }