DataResponse.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCP\AppFramework\Http;
  26. use OCP\AppFramework\Http;
  27. /**
  28. * A generic DataResponse class that is used to return generic data responses
  29. * for responders to transform
  30. * @since 8.0.0
  31. */
  32. class DataResponse extends Response {
  33. /**
  34. * response data
  35. * @var array|int|float|string|bool|object
  36. */
  37. protected $data;
  38. /**
  39. * @param array|int|float|string|bool|object $data the object or array that should be transformed
  40. * @param int $statusCode the Http status code, defaults to 200
  41. * @param array $headers additional key value based headers
  42. * @since 8.0.0
  43. */
  44. public function __construct($data = [], $statusCode = Http::STATUS_OK,
  45. array $headers = []) {
  46. parent::__construct();
  47. $this->data = $data;
  48. $this->setStatus($statusCode);
  49. $this->setHeaders(array_merge($this->getHeaders(), $headers));
  50. }
  51. /**
  52. * Sets values in the data json array
  53. * @param array|int|float|string|object $data an array or object which will be transformed
  54. * @return DataResponse Reference to this object
  55. * @since 8.0.0
  56. */
  57. public function setData($data) {
  58. $this->data = $data;
  59. return $this;
  60. }
  61. /**
  62. * Used to get the set parameters
  63. * @return array|int|float|string|bool|object the data
  64. * @since 8.0.0
  65. */
  66. public function getData() {
  67. return $this->data;
  68. }
  69. }