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