result.php 3.4 KB

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