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