result.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Tom Needham
  6. * @copyright 2012 Tom Needham tom@owncloud.com
  7. *
  8. * This file is licensed under the Affero General Public License version 3 or
  9. * later.
  10. * See the COPYING-README file.
  11. */
  12. class OC_OCS_Result{
  13. protected $data, $message, $statusCode, $items, $perPage;
  14. /**
  15. * create the OCS_Result object
  16. * @param mixed $data the data to return
  17. * @param int $code
  18. * @param null|string $message
  19. */
  20. public function __construct($data=null, $code=100, $message=null) {
  21. if ($data === null) {
  22. $this->data = array();
  23. } elseif (!is_array($data)) {
  24. $this->data = array($this->data);
  25. } else {
  26. $this->data = $data;
  27. }
  28. $this->statusCode = $code;
  29. $this->message = $message;
  30. }
  31. /**
  32. * optionally set the total number of items available
  33. * @param int $items
  34. */
  35. public function setTotalItems($items) {
  36. $this->items = $items;
  37. }
  38. /**
  39. * optionally set the the number of items per page
  40. * @param int $items
  41. */
  42. public function setItemsPerPage($items) {
  43. $this->perPage = $items;
  44. }
  45. /**
  46. * get the status code
  47. * @return int
  48. */
  49. public function getStatusCode() {
  50. return $this->statusCode;
  51. }
  52. /**
  53. * get the meta data for the result
  54. * @return array
  55. */
  56. public function getMeta() {
  57. $meta = array();
  58. $meta['status'] = ($this->statusCode === 100) ? 'ok' : 'failure';
  59. $meta['statuscode'] = $this->statusCode;
  60. $meta['message'] = $this->message;
  61. if(isset($this->items)) {
  62. $meta['totalitems'] = $this->items;
  63. }
  64. if(isset($this->perPage)) {
  65. $meta['itemsperpage'] = $this->perPage;
  66. }
  67. return $meta;
  68. }
  69. /**
  70. * get the result data
  71. * @return array
  72. */
  73. public function getData() {
  74. return $this->data;
  75. }
  76. /**
  77. * return bool Whether the method succeeded
  78. * @return bool
  79. */
  80. public function succeeded() {
  81. return ($this->statusCode == 100);
  82. }
  83. }