1
0

DataResponse.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCP\AppFramework\Http;
  8. use OCP\AppFramework\Http;
  9. /**
  10. * A generic DataResponse class that is used to return generic data responses
  11. * for responders to transform
  12. * @since 8.0.0
  13. * @psalm-type DataResponseType = array|int|float|string|bool|object|null|\stdClass|\JsonSerializable
  14. * @template S of int
  15. * @template-covariant T of DataResponseType
  16. * @template H of array<string, mixed>
  17. * @template-extends Response<int, array<string, mixed>>
  18. */
  19. class DataResponse extends Response {
  20. /**
  21. * response data
  22. * @var T
  23. */
  24. protected $data;
  25. /**
  26. * @param T $data the object or array that should be transformed
  27. * @param S $statusCode the Http status code, defaults to 200
  28. * @param H $headers additional key value based headers
  29. * @since 8.0.0
  30. */
  31. public function __construct(mixed $data = [], int $statusCode = Http::STATUS_OK, array $headers = []) {
  32. parent::__construct($statusCode, $headers);
  33. $this->data = $data;
  34. }
  35. /**
  36. * Sets values in the data json array
  37. * @psalm-suppress InvalidTemplateParam
  38. * @param T $data an array or object which will be transformed
  39. * @return DataResponse Reference to this object
  40. * @since 8.0.0
  41. */
  42. public function setData($data) {
  43. $this->data = $data;
  44. return $this;
  45. }
  46. /**
  47. * Used to get the set parameters
  48. * @return T the data
  49. * @since 8.0.0
  50. */
  51. public function getData() {
  52. return $this->data;
  53. }
  54. }