1
0

JSONResponse.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Thomas Tanghus <thomas@tanghus.net>
  12. * @author Kate Döen <kate.doeen@nextcloud.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCP\AppFramework\Http;
  30. use OCP\AppFramework\Http;
  31. /**
  32. * A renderer for JSON calls
  33. * @since 6.0.0
  34. * @template S of int
  35. * @template-covariant T of array|object|\stdClass|\JsonSerializable
  36. * @template H of array<string, mixed>
  37. * @template-extends Response<int, array<string, mixed>>
  38. */
  39. class JSONResponse extends Response {
  40. /**
  41. * response data
  42. * @var T
  43. */
  44. protected $data;
  45. /**
  46. * constructor of JSONResponse
  47. * @param T $data the object or array that should be transformed
  48. * @param S $statusCode the Http status code, defaults to 200
  49. * @param H $headers
  50. * @since 6.0.0
  51. */
  52. public function __construct(mixed $data = [], int $statusCode = Http::STATUS_OK, array $headers = []) {
  53. parent::__construct($statusCode, $headers);
  54. $this->data = $data;
  55. $this->addHeader('Content-Type', 'application/json; charset=utf-8');
  56. }
  57. /**
  58. * Returns the rendered json
  59. * @return string the rendered json
  60. * @since 6.0.0
  61. * @throws \Exception If data could not get encoded
  62. */
  63. public function render() {
  64. return json_encode($this->data, JSON_HEX_TAG | JSON_THROW_ON_ERROR);
  65. }
  66. /**
  67. * Sets values in the data json array
  68. * @psalm-suppress InvalidTemplateParam
  69. * @param T $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. * @return T the data
  80. * @since 6.0.0
  81. */
  82. public function getData() {
  83. return $this->data;
  84. }
  85. }