1
0

DataResponse.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. * @author Kate Döen <kate.doeen@nextcloud.com>
  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. namespace OCP\AppFramework\Http;
  27. use OCP\AppFramework\Http;
  28. /**
  29. * A generic DataResponse class that is used to return generic data responses
  30. * for responders to transform
  31. * @since 8.0.0
  32. * @psalm-type DataResponseType = array|int|float|string|bool|object|null|\stdClass|\JsonSerializable
  33. * @template S of int
  34. * @template-covariant T of DataResponseType
  35. * @template H of array<string, mixed>
  36. * @template-extends Response<int, array<string, mixed>>
  37. */
  38. class DataResponse extends Response {
  39. /**
  40. * response data
  41. * @var T
  42. */
  43. protected $data;
  44. /**
  45. * @param T $data the object or array that should be transformed
  46. * @param S $statusCode the Http status code, defaults to 200
  47. * @param H $headers additional key value based headers
  48. * @since 8.0.0
  49. */
  50. public function __construct(mixed $data = [], int $statusCode = Http::STATUS_OK, array $headers = []) {
  51. parent::__construct($statusCode, $headers);
  52. $this->data = $data;
  53. }
  54. /**
  55. * Sets values in the data json array
  56. * @psalm-suppress InvalidTemplateParam
  57. * @param T $data an array or object which will be transformed
  58. * @return DataResponse Reference to this object
  59. * @since 8.0.0
  60. */
  61. public function setData($data) {
  62. $this->data = $data;
  63. return $this;
  64. }
  65. /**
  66. * Used to get the set parameters
  67. * @return T the data
  68. * @since 8.0.0
  69. */
  70. public function getData() {
  71. return $this->data;
  72. }
  73. }