Js.php 3.8 KB

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