1
0

Result.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Christopher Schäpers <kondou@ts.unde.re>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin McCorkell <robin@mccorkell.me.uk>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. * @author Tom Needham <tom@owncloud.com>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OC\OCS;
  32. class Result {
  33. protected array $data;
  34. /** @var null|string */
  35. protected ?string $message;
  36. /** @var int */
  37. protected int $statusCode;
  38. /** @var integer */
  39. protected $items;
  40. /** @var integer */
  41. protected $perPage;
  42. /** @var array */
  43. private array $headers = [];
  44. /**
  45. * create the OCS_Result object
  46. *
  47. * @param mixed|null $data the data to return
  48. * @param int $code
  49. * @param string|null $message
  50. * @param array $headers
  51. */
  52. public function __construct(mixed $data = null, int $code = 100, string $message = null, array $headers = []) {
  53. if ($data === null) {
  54. $this->data = [];
  55. } elseif (!is_array($data)) {
  56. $this->data = [$this->data];
  57. } else {
  58. $this->data = $data;
  59. }
  60. $this->statusCode = $code;
  61. $this->message = $message;
  62. $this->headers = $headers;
  63. }
  64. /**
  65. * optionally set the total number of items available
  66. *
  67. * @param int $items
  68. */
  69. public function setTotalItems(int $items): void {
  70. $this->items = $items;
  71. }
  72. /**
  73. * optionally set the number of items per page
  74. *
  75. * @param int $items
  76. */
  77. public function setItemsPerPage(int $items): void {
  78. $this->perPage = $items;
  79. }
  80. /**
  81. * get the status code
  82. * @return int
  83. */
  84. public function getStatusCode(): int {
  85. return $this->statusCode;
  86. }
  87. /**
  88. * get the meta data for the result
  89. * @return array
  90. */
  91. public function getMeta(): array {
  92. $meta = [];
  93. $meta['status'] = $this->succeeded() ? 'ok' : 'failure';
  94. $meta['statuscode'] = $this->statusCode;
  95. $meta['message'] = $this->message;
  96. if ($this->items !== null) {
  97. $meta['totalitems'] = $this->items;
  98. }
  99. if ($this->perPage !== null) {
  100. $meta['itemsperpage'] = $this->perPage;
  101. }
  102. return $meta;
  103. }
  104. /**
  105. * get the result data
  106. * @return array
  107. */
  108. public function getData(): array {
  109. return $this->data;
  110. }
  111. /**
  112. * return bool Whether the method succeeded
  113. * @return bool
  114. */
  115. public function succeeded(): bool {
  116. return ($this->statusCode == 100);
  117. }
  118. /**
  119. * Adds a new header to the response
  120. *
  121. * @param string $name The name of the HTTP header
  122. * @param string $value The value, null will delete it
  123. * @return $this
  124. */
  125. public function addHeader(string $name, ?string $value): static {
  126. $name = trim($name); // always remove leading and trailing whitespace
  127. // to be able to reliably check for security
  128. // headers
  129. if (is_null($value)) {
  130. unset($this->headers[$name]);
  131. } else {
  132. $this->headers[$name] = $value;
  133. }
  134. return $this;
  135. }
  136. /**
  137. * Returns the set headers
  138. * @return array the headers
  139. */
  140. public function getHeaders(): array {
  141. return $this->headers;
  142. }
  143. }