@@ -189,6 +189,117 @@ public function sendPhpResponse($exception)
189189echo $ this ->decorate ($ this ->getContent ($ exception ),$ this ->getStylesheet ($ exception ));
190190 }
191191
192+ /**
193+ * Gets the content associated with the given exception.
194+ *
195+ * @param \Exception|FlattenException $exception An \Exception or FlattenException instance
196+ * @param string $format The request format (html, json, xml, txt)
197+ *
198+ * @return string The formatted content as a string
199+ */
200+ public function getFormattedContent ($ exception ,string $ format ):string
201+ {
202+ switch ($ format ) {
203+ case 'json ' :
204+ return $ this ->getJson ($ exception );
205+ case 'xml ' :
206+ return $ this ->getXml ($ exception );
207+ case 'txt ' :
208+ return $ this ->getTxt ($ exception );
209+ default :
210+ return $ this ->getHtml ($ exception );
211+ }
212+ }
213+
214+ /**
215+ * Gets the JSON content associated with the given exception.
216+ *
217+ * @param \Exception|FlattenException $exception An \Exception or FlattenException instance
218+ *
219+ * @return string The JSON content as a string
220+ */
221+ public function getJson ($ exception ):string
222+ {
223+ if (!$ exceptioninstanceof FlattenException) {
224+ $ exception = FlattenException::create ($ exception );
225+ }
226+
227+ return (string )json_encode ([
228+ 'error ' => [
229+ 'code ' =>$ exception ->getStatusCode (),
230+ 'exception ' =>$ exception ->toArray (),
231+ ],
232+ ]);
233+ }
234+
235+ /**
236+ * Gets the XML content associated with the given exception.
237+ *
238+ * @param \Exception|FlattenException $exception An \Exception or FlattenException instance
239+ *
240+ * @return string The XML content as a string
241+ */
242+ public function getXml ($ exception ):string
243+ {
244+ if (!$ exceptioninstanceof FlattenException) {
245+ $ exception = FlattenException::create ($ exception );
246+ }
247+
248+ $ content ='' ;
249+ foreach ($ exception ->toArray ()as $ e ) {
250+ $ content .=sprintf ('<exception class="%s" message="%s"><traces> ' ,$ e ['class ' ],$ e ['message ' ]);
251+ foreach ($ e ['trace ' ]as $ trace ) {
252+ $ content .='<trace> ' ;
253+ if ($ trace ['function ' ]) {
254+ $ content .=sprintf ('at %s%s%s(%s) ' ,$ trace ['class ' ],$ trace ['type ' ],$ trace ['function ' ],strip_tags ($ this ->formatArgs ($ trace ['args ' ])));
255+ }
256+ if (isset ($ trace ['file ' ],$ trace ['line ' ])) {
257+ $ content .=strip_tags ($ this ->formatPath ($ trace ['file ' ],$ trace ['line ' ]));
258+ }
259+ $ content .='</trace> ' ;
260+ }
261+ $ content .='</traces></exception> ' ;
262+ }
263+
264+ return <<<EOF
265+ <?xml version="1.0" encoding=" {$ this ->charset }" ?>
266+ <error code=" {$ exception ->getStatusCode ()}">
267+ {$ content }
268+ </error>
269+ EOF ;
270+ }
271+
272+ /**
273+ * Gets the TXT content associated with the given exception.
274+ *
275+ * @param \Exception|FlattenException $exception An \Exception or FlattenException instance
276+ *
277+ * @return string The TXT content as a string
278+ */
279+ public function getTxt ($ exception ):string
280+ {
281+ if (!$ exceptioninstanceof FlattenException) {
282+ $ exception = FlattenException::create ($ exception );
283+ }
284+
285+ $ content =sprintf ("[exception] %s | %s \n" ,$ exception ->getStatusCode (),$ exception ->getClass ());
286+ foreach ($ exception ->toArray ()as $ i =>$ e ) {
287+ $ content .=sprintf ("[%d] %s: %s \n" ,$ i +1 ,$ e ['class ' ],$ e ['message ' ]);
288+
289+ foreach ($ e ['trace ' ]as $ trace ) {
290+ if ($ trace ['function ' ]) {
291+ $ content .=sprintf ('at %s%s%s(%s) ' ,$ trace ['class ' ],$ trace ['type ' ],$ trace ['function ' ],strip_tags ($ this ->formatArgs ($ trace ['args ' ])));
292+ }
293+ if (isset ($ trace ['file ' ],$ trace ['line ' ])) {
294+ $ content .=strip_tags ($ this ->formatPath ($ trace ['file ' ],$ trace ['line ' ]));
295+ }
296+ $ content .="\n" ;
297+ }
298+ }
299+
300+ return $ content ;
301+ }
302+
192303/**
193304 * Gets the full HTML content associated with the given exception.
194305 *