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