Js.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. class Js implements Htmlable
  9. {
  10. /**
  11. * The JavaScript string.
  12. *
  13. * @var string
  14. */
  15. protected $js;
  16. /**
  17. * Flags that should be used when encoding to JSON.
  18. *
  19. * @var int
  20. */
  21. protected const REQUIRED_FLAGS = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_THROW_ON_ERROR;
  22. /**
  23. * Create a new class instance.
  24. *
  25. * @param mixed $data
  26. * @param int|null $flags
  27. * @param int $depth
  28. * @return void
  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 BackedEnum) {
  66. $data = $data->value;
  67. }
  68. $json = static::encode($data, $flags, $depth);
  69. if (is_string($data)) {
  70. return "'".substr($json, 1, -1)."'";
  71. }
  72. return $this->convertJsonToJavaScriptExpression($json, $flags);
  73. }
  74. /**
  75. * Encode the given data as JSON.
  76. *
  77. * @param mixed $data
  78. * @param int $flags
  79. * @param int $depth
  80. * @return string
  81. *
  82. * @throws \JsonException
  83. */
  84. public static function encode($data, $flags = 0, $depth = 512)
  85. {
  86. if ($data instanceof Jsonable) {
  87. return $data->toJson($flags | static::REQUIRED_FLAGS);
  88. }
  89. if ($data instanceof Arrayable && ! ($data instanceof JsonSerializable)) {
  90. $data = $data->toArray();
  91. }
  92. return json_encode($data, $flags | static::REQUIRED_FLAGS, $depth);
  93. }
  94. /**
  95. * Convert the given JSON to a JavaScript expression.
  96. *
  97. * @param string $json
  98. * @param int $flags
  99. * @return string
  100. *
  101. * @throws \JsonException
  102. */
  103. protected function convertJsonToJavaScriptExpression($json, $flags = 0)
  104. {
  105. if ($json === '[]' || $json === '{}') {
  106. return $json;
  107. }
  108. if (Str::startsWith($json, ['"', '{', '['])) {
  109. return "JSON.parse('".substr(json_encode($json, $flags | static::REQUIRED_FLAGS), 1, -1)."')";
  110. }
  111. return $json;
  112. }
  113. /**
  114. * Get the string representation of the data for use in HTML.
  115. *
  116. * @return string
  117. */
  118. public function toHtml()
  119. {
  120. return $this->js;
  121. }
  122. /**
  123. * Get the string representation of the data for use in HTML.
  124. *
  125. * @return string
  126. */
  127. public function __toString()
  128. {
  129. return $this->toHtml();
  130. }
  131. }