JSONResponse.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. * @author Thomas Tanghus <thomas@tanghus.net>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. /**
  27. * Public interface of ownCloud for apps to use.
  28. * AppFramework\HTTP\JSONResponse class
  29. */
  30. namespace OCP\AppFramework\Http;
  31. use OCP\AppFramework\Http;
  32. /**
  33. * A renderer for JSON calls
  34. * @since 6.0.0
  35. */
  36. class JSONResponse extends Response {
  37. /**
  38. * response data
  39. * @var array|object
  40. */
  41. protected $data;
  42. /**
  43. * constructor of JSONResponse
  44. * @param array|object $data the object or array that should be transformed
  45. * @param int $statusCode the Http status code, defaults to 200
  46. * @since 6.0.0
  47. */
  48. public function __construct($data=array(), $statusCode=Http::STATUS_OK) {
  49. $this->data = $data;
  50. $this->setStatus($statusCode);
  51. $this->addHeader('Content-Type', 'application/json; charset=utf-8');
  52. }
  53. /**
  54. * Returns the rendered json
  55. * @return string the rendered json
  56. * @since 6.0.0
  57. * @throws \Exception If data could not get encoded
  58. */
  59. public function render() {
  60. $response = json_encode($this->data, JSON_HEX_TAG);
  61. if($response === false) {
  62. throw new \Exception(sprintf('Could not json_encode due to invalid ' .
  63. 'non UTF-8 characters in the array: %s', var_export($this->data, true)));
  64. }
  65. return $response;
  66. }
  67. /**
  68. * Sets values in the data json array
  69. * @param array|object $data an array or object which will be transformed
  70. * to JSON
  71. * @return JSONResponse Reference to this object
  72. * @since 6.0.0 - return value was added in 7.0.0
  73. */
  74. public function setData($data){
  75. $this->data = $data;
  76. return $this;
  77. }
  78. /**
  79. * Used to get the set parameters
  80. * @return array the data
  81. * @since 6.0.0
  82. */
  83. public function getData(){
  84. return $this->data;
  85. }
  86. }