@@ -34,22 +34,20 @@ class JsonResponse extends Response
34
34
protected $ encodingOptions = self ::DEFAULT_ENCODING_OPTIONS ;
35
35
36
36
/**
37
- * Constructor.
38
- *
39
- * @param mixed $data The response data
40
- * @param int $status The response status code
41
- * @param array $headers An array of response headers
42
- * @param bool $preEncoded If the data is already a JSON string
37
+ * @param mixed $data The response data
38
+ * @param int $status The response status code
39
+ * @param array $headers An array of response headers
40
+ * @param bool $json If the data is already a JSON string
43
41
*/
44
- public function __construct ($ data = null , $ status = 200 , $ headers = array (), $ preEncoded = false )
42
+ public function __construct ($ data = null , $ status = 200 , $ headers = array (), $ json = false )
45
43
{
46
44
parent ::__construct ('' , $ status , $ headers );
47
45
48
46
if (null === $ data ) {
49
47
$ data = new \ArrayObject ();
50
48
}
51
49
52
- $ this ->setData ($ data, $ preEncoded );
50
+ $ json ? $ this ->setJson ($ data) : $ this -> setData ( $ data );
53
51
}
54
52
55
53
/**
@@ -87,46 +85,57 @@ public function setCallback($callback = null)
87
85
return $ this ->update ();
88
86
}
89
87
88
+ /**
89
+ * Sets a raw string containing a JSON document to be sent.
90
+ *
91
+ * @param string $data
92
+ *
93
+ * @return JsonResponse
94
+ *
95
+ * @throws \InvalidArgumentException
96
+ */
97
+ public function setJson ($ json )
98
+ {
99
+ $ this ->data = $ json ;
100
+
101
+ return $ this ->update ();
102
+ }
103
+
90
104
/**
91
105
* Sets the data to be sent as JSON.
92
106
*
93
107
* @param mixed $data
94
- * @param bool $preEncoded If the data is already a JSON string
95
108
*
96
109
* @return JsonResponse
97
110
*
98
111
* @throws \InvalidArgumentException
99
112
*/
100
- public function setData ($ data = array (), $ preEncoded = false )
113
+ public function setData ($ data = array ())
101
114
{
102
- if (!$ preEncoded ) {
103
- if (defined ('HHVM_VERSION ' )) {
104
- // HHVM does not trigger any warnings and let exceptions
105
- // thrown from a JsonSerializable object pass through.
106
- // If only PHP did the same...
115
+ if (defined ('HHVM_VERSION ' )) {
116
+ // HHVM does not trigger any warnings and let exceptions
117
+ // thrown from a JsonSerializable object pass through.
118
+ // If only PHP did the same...
119
+ $ data = json_encode ($ data , $ this ->encodingOptions );
120
+ } else {
121
+ try {
122
+ // PHP 5.4 and up wrap exceptions thrown by JsonSerializable
123
+ // objects in a new exception that needs to be removed.
124
+ // Fortunately, PHP 5.5 and up do not trigger any warning anymore.
107
125
$ data = json_encode ($ data , $ this ->encodingOptions );
108
- } else {
109
- try {
110
- // PHP 5.4 and up wrap exceptions thrown by JsonSerializable
111
- // objects in a new exception that needs to be removed.
112
- // Fortunately, PHP 5.5 and up do not trigger any warning anymore.
113
- $ data = json_encode ($ data , $ this ->encodingOptions );
114
- } catch (\Exception $ e ) {
115
- if ('Exception ' === get_class ($ e ) && 0 === strpos ($ e ->getMessage (), 'Failed calling ' )) {
116
- throw $ e ->getPrevious () ?: $ e ;
117
- }
118
- throw $ e ;
126
+ } catch (\Exception $ e ) {
127
+ if ('Exception ' === get_class ($ e ) && 0 === strpos ($ e ->getMessage (), 'Failed calling ' )) {
128
+ throw $ e ->getPrevious () ?: $ e ;
119
129
}
120
- }
121
-
122
- if (JSON_ERROR_NONE !== json_last_error ()) {
123
- throw new \InvalidArgumentException (json_last_error_msg ());
130
+ throw $ e ;
124
131
}
125
132
}
126
133
127
- $ this ->data = $ data ;
134
+ if (JSON_ERROR_NONE !== json_last_error ()) {
135
+ throw new \InvalidArgumentException (json_last_error_msg ());
136
+ }
128
137
129
- return $ this ->update ( );
138
+ return $ this ->setJson ( $ data );
130
139
}
131
140
132
141
/**
0 commit comments