| 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 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
protected $js; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 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 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public function __construct($data, $flags = 0, $depth = 512) |
| 38 |
{ |
| 39 |
$this->js = $this->convertDataToJavaScriptExpression($data, $flags, $depth); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public static function from($data, $flags = 0, $depth = 512) |
| 53 |
{ |
| 54 |
return new static($data, $flags, $depth); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 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 |
|
| 95 |
|
| 96 |
g. |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 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 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 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 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public function toHtml() |
| 146 |
{ |
| 147 |
return $this->js; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public function __toString() |
| 156 |
{ |
| 157 |
return $this->toHtml(); |
| 158 |
} |
| 159 |
} |