Result.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. /** @var array */
  34. protected $data;
  35. /** @var null|string */
  36. protected $message;
  37. /** @var int */
  38. protected $statusCode;
  39. /** @var integer */
  40. protected $items;
  41. /** @var integer */
  42. protected $perPage;
  43. /** @var array */
  44. private $headers = [];
  45. /**
  46. * create the OCS_Result object
  47. * @param mixed $data the data to return
  48. * @param int $code
  49. * @param null|string $message
  50. * @param array $headers
  51. */
  52. public function __construct($data = null, $code = 100, $message = null, $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. * @param int $items
  67. */
  68. public function setTotalItems($items) {
  69. $this->items = $items;
  70. }
  71. /**
  72. * optionally set the the number of items per page
  73. * @param int $items
  74. */
  75. public function setItemsPerPage($items) {
  76. $this->perPage = $items;
  77. }
  78. /**
  79. * get the status code
  80. * @return int
  81. */
  82. public function getStatusCode() {
  83. return $this->statusCode;
  84. }
  85. /**
  86. * get the meta data for the result
  87. * @return array
  88. */
  89. public function getMeta() {
  90. $meta = [];
  91. $meta['status'] = $this->succeeded() ? 'ok' : 'failure';
  92. $meta['statuscode'] = $this->statusCode;
  93. $meta['message'] = $this->message;
  94. if ($this->items !== null) {
  95. $meta['totalitems'] = $this->items;
  96. }
  97. if ($this->perPage !== null) {
  98. $meta['itemsperpage'] = $this->perPage;
  99. }
  100. return $meta;
  101. }
  102. /**
  103. * get the result data
  104. * @return array
  105. */
  106. public function getData() {
  107. return $this->data;
  108. }
  109. /**
  110. * return bool Whether the method succeeded
  111. * @return bool
  112. */
  113. public function succeeded() {
  114. return ($this->statusCode == 100);
  115. }
  116. /**
  117. * Adds a new header to the response
  118. * @param string $name The name of the HTTP header
  119. * @param string $value The value, null will delete it
  120. * @return $this
  121. */
  122. public function addHeader($name, $value) {
  123. $name = trim($name); // always remove leading and trailing whitespace
  124. // to be able to reliably check for security
  125. // headers
  126. if (is_null($value)) {
  127. unset($this->headers[$name]);
  128. } else {
  129. $this->headers[$name] = $value;
  130. }
  131. return $this;
  132. }
  133. /**
  134. * Returns the set headers
  135. * @return array the headers
  136. */
  137. public function getHeaders() {
  138. return $this->headers;
  139. }
  140. }