result.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. protected $data, $message, $statusCode, $items, $perPage;
  29. /**
  30. * create the OCS_Result object
  31. * @param mixed $data the data to return
  32. * @param int $code
  33. * @param null|string $message
  34. */
  35. public function __construct($data=null, $code=100, $message=null) {
  36. if ($data === null) {
  37. $this->data = array();
  38. } elseif (!is_array($data)) {
  39. $this->data = array($this->data);
  40. } else {
  41. $this->data = $data;
  42. }
  43. $this->statusCode = $code;
  44. $this->message = $message;
  45. }
  46. /**
  47. * optionally set the total number of items available
  48. * @param int $items
  49. */
  50. public function setTotalItems($items) {
  51. $this->items = $items;
  52. }
  53. /**
  54. * optionally set the the number of items per page
  55. * @param int $items
  56. */
  57. public function setItemsPerPage($items) {
  58. $this->perPage = $items;
  59. }
  60. /**
  61. * get the status code
  62. * @return int
  63. */
  64. public function getStatusCode() {
  65. return $this->statusCode;
  66. }
  67. /**
  68. * get the meta data for the result
  69. * @return array
  70. */
  71. public function getMeta() {
  72. $meta = array();
  73. $meta['status'] = ($this->statusCode === 100) ? 'ok' : 'failure';
  74. $meta['statuscode'] = $this->statusCode;
  75. $meta['message'] = $this->message;
  76. if(isset($this->items)) {
  77. $meta['totalitems'] = $this->items;
  78. }
  79. if(isset($this->perPage)) {
  80. $meta['itemsperpage'] = $this->perPage;
  81. }
  82. return $meta;
  83. }
  84. /**
  85. * get the result data
  86. * @return array
  87. */
  88. public function getData() {
  89. return $this->data;
  90. }
  91. /**
  92. * return bool Whether the method succeeded
  93. * @return bool
  94. */
  95. public function succeeded() {
  96. return ($this->statusCode == 100);
  97. }
  98. }