JsonResponse.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace Illuminate\Http;
  3. use JsonSerializable;
  4. use InvalidArgumentException;
  5. use Illuminate\Support\Traits\Macroable;
  6. use Illuminate\Contracts\Support\Jsonable;
  7. use Illuminate\Contracts\Support\Arrayable;
  8. use Symfony\Component\HttpFoundation\JsonResponse as BaseJsonResponse;
  9. class JsonResponse extends BaseJsonResponse
  10. {
  11. use ResponseTrait, Macroable {
  12. Macroable::__call as macroCall;
  13. }
  14. /**
  15. * Constructor.
  16. *
  17. * @param mixed $data
  18. * @param int $status
  19. * @param array $headers
  20. * @param int $options
  21. * @return void
  22. */
  23. public function __construct($data = null, $status = 200, $headers = [], $options = 0)
  24. {
  25. $this->encodingOptions = $options;
  26. parent::__construct($data, $status, $headers);
  27. }
  28. /**
  29. * Sets the JSONP callback.
  30. *
  31. * @param string|null $callback
  32. * @return $this
  33. */
  34. public function withCallback($callback = null)
  35. {
  36. return $this->setCallback($callback);
  37. }
  38. /**
  39. * Get the json_decoded data from the response.
  40. *
  41. * @param bool $assoc
  42. * @param int $depth
  43. * @return mixed
  44. */
  45. public function getData($assoc = false, $depth = 512)
  46. {
  47. return json_decode($this->data, $assoc, $depth);
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function setData($data = [])
  53. {
  54. $this->original = $data;
  55. if ($data instanceof Jsonable) {
  56. $this->data = $data->toJson($this->encodingOptions);
  57. } elseif ($data instanceof JsonSerializable) {
  58. $this->data = json_encode($data->jsonSerialize(), $this->encodingOptions);
  59. } elseif ($data instanceof Arrayable) {
  60. $this->data = json_encode($data->toArray(), $this->encodingOptions);
  61. } else {
  62. $this->data = json_encode($data, $this->encodingOptions);
  63. }
  64. if (! $this->hasValidJson(json_last_error())) {
  65. throw new InvalidArgumentException(json_last_error_msg());
  66. }
  67. return $this->update();
  68. }
  69. /**
  70. * Determine if an error occurred during JSON encoding.
  71. *
  72. * @param int $jsonError
  73. * @return bool
  74. */
  75. protected function hasValidJson($jsonError)
  76. {
  77. if ($jsonError === JSON_ERROR_NONE) {
  78. return true;
  79. }
  80. return $this->hasEncodingOption(JSON_PARTIAL_OUTPUT_ON_ERROR) &&
  81. in_array($jsonError, [
  82. JSON_ERROR_RECURSION,
  83. JSON_ERROR_INF_OR_NAN,
  84. JSON_ERROR_UNSUPPORTED_TYPE,
  85. ]);
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function setEncodingOptions($options)
  91. {
  92. $this->encodingOptions = (int) $options;
  93. return $this->setData($this->getData());
  94. }
  95. /**
  96. * Determine if a JSON encoding option is set.
  97. *
  98. * @param int $option
  99. * @return bool
  100. */
  101. public function hasEncodingOption($option)
  102. {
  103. return (bool) ($this->encodingOptions & $option);
  104. }
  105. }