JsonResponse.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <[email protected]>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation;
  11. /**
  12. * Response represents an HTTP response in JSON format.
  13. *
  14. * @author Igor Wiedler <[email protected]>
  15. */
  16. class JsonResponse extends Response
  17. {
  18. protected $data;
  19. protected $callback;
  20. /**
  21. * Constructor.
  22. *
  23. * @param mixed $data The response data
  24. * @param integer $status The response status code
  25. * @param array $headers An array of response headers
  26. */
  27. public function __construct($data = array(), $status = 200, $headers = array())
  28. {
  29. parent::__construct('', $status, $headers);
  30. $this->setData($data);
  31. }
  32. /**
  33. * {@inheritDoc}
  34. */
  35. public static function create($data = array(), $status = 200, $headers = array())
  36. {
  37. return new static($data, $status, $headers);
  38. }
  39. /**
  40. * Sets the JSONP callback.
  41. *
  42. * @param string $callback
  43. *
  44. * @return JsonResponse
  45. */
  46. public function setCallback($callback = null)
  47. {
  48. if (null !== $callback) {
  49. // taken from http://www.geekality.net/2011/08/03/valid-javascript-identifier/
  50. $pattern = '/^[$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\x{200C}\x{200D}]*+$/u';
  51. $parts = explode('.', $callback);
  52. foreach ($parts as $part) {
  53. if (!preg_match($pattern, $part)) {
  54. throw new \InvalidArgumentException('The callback name is not valid.');
  55. }
  56. }
  57. }
  58. $this->callback = $callback;
  59. return $this->update();
  60. }
  61. /**
  62. * Sets the data to be sent as json.
  63. *
  64. * @param mixed $data
  65. *
  66. * @return JsonResponse
  67. */
  68. public function setData($data = array())
  69. {
  70. // root should be JSON object, not array
  71. if (is_array($data) && 0 === count($data)) {
  72. $data = new \ArrayObject();
  73. }
  74. // Encode <, >, ', &, and " for RFC4627-compliant JSON, which may also be embedded into HTML.
  75. $this->data = json_encode($data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
  76. return $this->update();
  77. }
  78. /**
  79. * Updates the content and headers according to the json data and callback.
  80. *
  81. * @return JsonResponse
  82. */
  83. protected function update()
  84. {
  85. if (null !== $this->callback) {
  86. // Not using application/javascript for compatibility reasons with older browsers.
  87. $this->headers->set('Content-Type', 'text/javascript');
  88. return $this->setContent(sprintf('%s(%s);', $this->callback, $this->data));
  89. }
  90. // Only set the header when there is none or when it equals 'text/javascript' (from a previous update with callback)
  91. // in order to not overwrite a custom definition.
  92. if (!$this->headers->has('Content-Type') || 'text/javascript' === $this->headers->get('Content-Type')) {
  93. $this->headers->set('Content-Type', 'application/json');
  94. }
  95. return $this->setContent($this->data);
  96. }
  97. }