1
0

JSONResponse.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. parent::__construct();
  50. $this->data = $data;
  51. $this->setStatus($statusCode);
  52. $this->addHeader('Content-Type', 'application/json; charset=utf-8');
  53. }
  54. /**
  55. * Returns the rendered json
  56. * @return string the rendered json
  57. * @since 6.0.0
  58. * @throws \Exception If data could not get encoded
  59. */
  60. public function render() {
  61. $response = json_encode($this->data, JSON_HEX_TAG);
  62. if($response === false) {
  63. throw new \Exception(sprintf('Could not json_encode due to invalid ' .
  64. 'non UTF-8 characters in the array: %s', var_export($this->data, true)));
  65. }
  66. return $response;
  67. }
  68. /**
  69. * Sets values in the data json array
  70. * @param array|object $data an array or object which will be transformed
  71. * to JSON
  72. * @return JSONResponse Reference to this object
  73. * @since 6.0.0 - return value was added in 7.0.0
  74. */
  75. public function setData($data){
  76. $this->data = $data;
  77. return $this;
  78. }
  79. /**
  80. * Used to get the set parameters
  81. * @return array the data
  82. * @since 6.0.0
  83. */
  84. public function getData(){
  85. return $this->data;
  86. }
  87. }