Js.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. * @param mixed $data
  84. * @param int $flags
  85. * @param int $depth
  86. * @return string
  87. *
  88. * @throws \JsonException
  89. */
  90. public static function encode($data, $flags = 0, $depth = 512)
  91. {
  92. if ($data instanceof Jsonable) {
  93. return $data->toJson($flags | static::REQUIRED_FLAGS);
  94. }
  95. if ($data instanceof Arrayable && ! ($data instanceof JsonSerializable)) {
  96. $data = $data->toArray();
  97. }
  98. return json_encode($data, $flags | static::REQUIRED_FLAGS, $depth);
  99. }
  100. /**
  101. * Convert the given JSON to a JavaScript expression.
  102. *
  103. * @param string $json
  104. * @param int $flags
  105. * @return string
  106. *
  107. * @throws \JsonException
  108. */
  109. protected function convertJsonToJavaScriptExpression($json, $flags = 0)
  110. {
  111. if ($json === '[]' || $json === '{}') {
  112. return $json;
  113. }
  114. if (Str::startsWith($json, ['"', '{', '['])) {
  115. return "JSON.parse('".substr(json_encode($json, $flags | static::REQUIRED_FLAGS), 1, -1)."')";
  116. }
  117. return $json;
  118. }
  119. /**
  120. * Get the string representation of the data for use in HTML.
  121. *
  122. * @return string
  123. */
  124. public function toHtml()
  125. {
  126. return $this->js;
  127. }
  128. /**
  129. * Get the string representation of the data for use in HTML.
  130. *
  131. * @return string
  132. */
  133. public function __toString()
  134. {
  135. return $this->toHtml();
  136. }
  137. }