Js.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace Illuminate\Support;
  3. use BackedEnum;
  4. use Illuminate\Contracts\Support\Arrayable;
  5. use Illuminate\Contracts\Support\Htmlable;
  6. use Illuminate\Contracts\Support\Jsonable;
  7. use JsonSerializable;
  8. use Stringable;
  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. * @return void
  30. *
  31. * @throws \JsonException
  32. */
  33. public function __construct($data, $flags = 0, $depth = 512)
  34. {
  35. $this->js = $this->convertDataToJavaScriptExpression($data, $flags, $depth);
  36. }
  37. /**
  38. * Create a new JavaScript string from the given data.
  39. *
  40. * @param mixed $data
  41. * @param int $flags
  42. * @param int $depth
  43. * @return static
  44. *
  45. * @throws \JsonException
  46. */
  47. public static function from($data, $flags = 0, $depth = 512)
  48. {
  49. return new static($data, $flags, $depth);
  50. }
  51. /**
  52. * Convert the given data to a JavaScript expression.
  53. *
  54. * @param mixed $data
  55. * @param int $flags
  56. * @param int $depth
  57. * @return string
  58. *
  59. * @throws \JsonException
  60. */
  61. protected function convertDataToJavaScriptExpression($data, $flags = 0, $depth = 512)
  62. {
  63. if ($data instanceof self) {
  64. return $data->toHtml();
  65. }
  66. if ($data instanceof BackedEnum) {
  67. $data = $data->value;
  68. }
  69. $json = static::encode($data, $flags, $depth);
  70. if (is_string($data)) {
  71. return "'".substr($json, 1, -1)."'";
  72. }
  73. return $this->convertJsonToJavaScriptExpression($json, $flags);
  74. }
  75. /**
  76. * Encode the given data as JSON.
  77. *
  78. * @param mixed $data
  79. * @param int $flags
  80. * @param int $depth
  81. * @return string
  82. *
  83. * @throws \JsonException
  84. */
  85. public static function encode($data, $flags = 0, $depth = 512)
  86. {
  87. if ($data instanceof Jsonable) {
  88. return $data->toJson($flags | static::REQUIRED_FLAGS);
  89. }
  90. if ($data instanceof Arrayable && ! ($data instanceof JsonSerializable)) {
  91. $data = $data->toArray();
  92. }
  93. return json_encode($data, $flags | static::REQUIRED_FLAGS, $depth);
  94. }
  95. /**
  96. * Convert the given JSON to a JavaScript expression.
  97. *
  98. * @param string $json
  99. * @param int $flags
  100. * @return string
  101. *
  102. * @throws \JsonException
  103. */
  104. protected function convertJsonToJavaScriptExpression($json, $flags = 0)
  105. {
  106. if ($json === '[]' || $json === '{}') {
  107. return $json;
  108. }
  109. if (Str::startsWith($json, ['"', '{', '['])) {
  110. return "JSON.parse('".substr(json_encode($json, $flags | static::REQUIRED_FLAGS), 1, -1)."')";
  111. }
  112. return $json;
  113. }
  114. /**
  115. * Get the string representation of the data for use in HTML.
  116. *
  117. * @return string
  118. */
  119. public function toHtml()
  120. {
  121. return $this->js;
  122. }
  123. /**
  124. * Get the string representation of the data for use in HTML.
  125. *
  126. * @return string
  127. */
  128. public function __toString()
  129. {
  130. return $this->toHtml();
  131. }
  132. }