Result.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 OC\OCS;
  8. class Result {
  9. protected array $data;
  10. /** @var null|string */
  11. protected ?string $message;
  12. /** @var int */
  13. protected int $statusCode;
  14. /** @var integer */
  15. protected $items;
  16. /** @var integer */
  17. protected $perPage;
  18. /** @var array */
  19. private array $headers = [];
  20. /**
  21. * create the OCS_Result object
  22. *
  23. * @param mixed|null $data the data to return
  24. * @param int $code
  25. * @param string|null $message
  26. * @param array $headers
  27. */
  28. public function __construct(mixed $data = null, int $code = 100, ?string $message = null, array $headers = []) {
  29. if ($data === null) {
  30. $this->data = [];
  31. } elseif (!is_array($data)) {
  32. $this->data = [$this->data];
  33. } else {
  34. $this->data = $data;
  35. }
  36. $this->statusCode = $code;
  37. $this->message = $message;
  38. $this->headers = $headers;
  39. }
  40. /**
  41. * optionally set the total number of items available
  42. *
  43. * @param int $items
  44. */
  45. public function setTotalItems(int $items): void {
  46. $this->items = $items;
  47. }
  48. /**
  49. * optionally set the number of items per page
  50. *
  51. * @param int $items
  52. */
  53. public function setItemsPerPage(int $items): void {
  54. $this->perPage = $items;
  55. }
  56. /**
  57. * get the status code
  58. * @return int
  59. */
  60. public function getStatusCode(): int {
  61. return $this->statusCode;
  62. }
  63. /**
  64. * get the meta data for the result
  65. * @return array
  66. */
  67. public function getMeta(): array {
  68. $meta = [];
  69. $meta['status'] = $this->succeeded() ? 'ok' : 'failure';
  70. $meta['statuscode'] = $this->statusCode;
  71. $meta['message'] = $this->message;
  72. if ($this->items !== null) {
  73. $meta['totalitems'] = $this->items;
  74. }
  75. if ($this->perPage !== null) {
  76. $meta['itemsperpage'] = $this->perPage;
  77. }
  78. return $meta;
  79. }
  80. /**
  81. * get the result data
  82. * @return array
  83. */
  84. public function getData(): array {
  85. return $this->data;
  86. }
  87. /**
  88. * return bool Whether the method succeeded
  89. * @return bool
  90. */
  91. public function succeeded(): bool {
  92. return ($this->statusCode == 100);
  93. }
  94. /**
  95. * Adds a new header to the response
  96. *
  97. * @param string $name The name of the HTTP header
  98. * @param string $value The value, null will delete it
  99. * @return $this
  100. */
  101. public function addHeader(string $name, ?string $value): static {
  102. $name = trim($name); // always remove leading and trailing whitespace
  103. // to be able to reliably check for security
  104. // headers
  105. if (is_null($value)) {
  106. unset($this->headers[$name]);
  107. } else {
  108. $this->headers[$name] = $value;
  109. }
  110. return $this;
  111. }
  112. /**
  113. * Returns the set headers
  114. * @return array the headers
  115. */
  116. public function getHeaders(): array {
  117. return $this->headers;
  118. }
  119. }