2012-12-12 11:50:25 -05:00
|
|
|
<?php
|
2024-05-23 03:26:56 -04:00
|
|
|
|
2012-12-31 10:47:15 -05:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2015-03-26 06:44:34 -04:00
|
|
|
*/
|
2016-05-12 03:43:53 -04:00
|
|
|
namespace OC\OCS;
|
|
|
|
|
|
|
|
|
|
class Result {
|
2023-07-05 13:43:36 -04:00
|
|
|
protected array $data;
|
2015-08-07 07:12:43 -04:00
|
|
|
|
|
|
|
|
/** @var null|string */
|
2023-07-05 13:43:36 -04:00
|
|
|
protected ?string $message;
|
2015-08-07 07:12:43 -04:00
|
|
|
|
|
|
|
|
/** @var int */
|
2023-07-05 13:43:36 -04:00
|
|
|
protected int $statusCode;
|
2015-08-07 07:12:43 -04:00
|
|
|
|
|
|
|
|
/** @var integer */
|
|
|
|
|
protected $items;
|
|
|
|
|
|
|
|
|
|
/** @var integer */
|
|
|
|
|
protected $perPage;
|
|
|
|
|
|
|
|
|
|
/** @var array */
|
2023-07-05 13:43:36 -04:00
|
|
|
private array $headers = [];
|
2013-01-14 14:30:39 -05:00
|
|
|
|
2012-12-12 11:50:25 -05:00
|
|
|
/**
|
|
|
|
|
* create the OCS_Result object
|
2023-07-05 13:43:36 -04:00
|
|
|
*
|
|
|
|
|
* @param mixed|null $data the data to return
|
2014-09-15 11:08:56 -04:00
|
|
|
* @param int $code
|
2023-07-05 13:43:36 -04:00
|
|
|
* @param string|null $message
|
2015-08-13 04:39:52 -04:00
|
|
|
* @param array $headers
|
2012-12-12 11:50:25 -05:00
|
|
|
*/
|
2024-03-28 11:13:19 -04:00
|
|
|
public function __construct(mixed $data = null, int $code = 100, ?string $message = null, array $headers = []) {
|
2014-01-28 11:42:26 -05:00
|
|
|
if ($data === null) {
|
2020-03-26 04:30:18 -04:00
|
|
|
$this->data = [];
|
2014-01-28 11:42:26 -05:00
|
|
|
} elseif (!is_array($data)) {
|
2020-03-26 04:30:18 -04:00
|
|
|
$this->data = [$this->data];
|
2014-01-28 11:42:26 -05:00
|
|
|
} else {
|
|
|
|
|
$this->data = $data;
|
|
|
|
|
}
|
2012-12-31 10:47:15 -05:00
|
|
|
$this->statusCode = $code;
|
2012-12-12 11:50:25 -05:00
|
|
|
$this->message = $message;
|
2015-08-10 15:33:50 -04:00
|
|
|
$this->headers = $headers;
|
2012-12-12 11:50:25 -05:00
|
|
|
}
|
2013-01-14 14:30:39 -05:00
|
|
|
|
2012-12-12 11:50:25 -05:00
|
|
|
/**
|
|
|
|
|
* optionally set the total number of items available
|
2023-07-05 13:43:36 -04:00
|
|
|
*
|
2014-05-11 16:51:30 -04:00
|
|
|
* @param int $items
|
2012-12-12 11:50:25 -05:00
|
|
|
*/
|
2023-07-05 13:43:36 -04:00
|
|
|
public function setTotalItems(int $items): void {
|
2012-12-12 11:50:25 -05:00
|
|
|
$this->items = $items;
|
|
|
|
|
}
|
2013-01-14 14:30:39 -05:00
|
|
|
|
2012-12-12 11:50:25 -05:00
|
|
|
/**
|
2023-07-05 13:43:36 -04:00
|
|
|
* optionally set the number of items per page
|
|
|
|
|
*
|
2014-05-11 16:51:30 -04:00
|
|
|
* @param int $items
|
2012-12-12 11:50:25 -05:00
|
|
|
*/
|
2023-07-05 13:43:36 -04:00
|
|
|
public function setItemsPerPage(int $items): void {
|
2012-12-31 10:47:15 -05:00
|
|
|
$this->perPage = $items;
|
2012-12-12 11:50:25 -05:00
|
|
|
}
|
2014-01-28 11:42:26 -05:00
|
|
|
|
2013-01-25 07:48:59 -05:00
|
|
|
/**
|
|
|
|
|
* get the status code
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2023-07-05 13:43:36 -04:00
|
|
|
public function getStatusCode(): int {
|
2013-01-25 07:48:59 -05:00
|
|
|
return $this->statusCode;
|
|
|
|
|
}
|
2014-01-28 11:42:26 -05:00
|
|
|
|
2012-12-12 11:50:25 -05:00
|
|
|
/**
|
2013-01-25 07:48:59 -05:00
|
|
|
* get the meta data for the result
|
2012-12-12 11:50:25 -05:00
|
|
|
* @return array
|
|
|
|
|
*/
|
2023-07-05 13:43:36 -04:00
|
|
|
public function getMeta(): array {
|
2020-03-26 04:30:18 -04:00
|
|
|
$meta = [];
|
2015-08-05 18:01:56 -04:00
|
|
|
$meta['status'] = $this->succeeded() ? 'ok' : 'failure';
|
2013-01-25 07:48:59 -05:00
|
|
|
$meta['statuscode'] = $this->statusCode;
|
|
|
|
|
$meta['message'] = $this->message;
|
2021-02-15 04:03:48 -05:00
|
|
|
if ($this->items !== null) {
|
2013-01-25 07:48:59 -05:00
|
|
|
$meta['totalitems'] = $this->items;
|
2012-12-12 15:58:40 -05:00
|
|
|
}
|
2021-02-15 04:03:48 -05:00
|
|
|
if ($this->perPage !== null) {
|
2013-01-25 07:48:59 -05:00
|
|
|
$meta['itemsperpage'] = $this->perPage;
|
2012-12-12 15:58:40 -05:00
|
|
|
}
|
2013-01-25 07:48:59 -05:00
|
|
|
return $meta;
|
|
|
|
|
}
|
2014-01-28 11:42:26 -05:00
|
|
|
|
2013-01-25 07:48:59 -05:00
|
|
|
/**
|
|
|
|
|
* get the result data
|
2014-01-28 11:42:26 -05:00
|
|
|
* @return array
|
2013-01-25 07:48:59 -05:00
|
|
|
*/
|
2023-07-05 13:43:36 -04:00
|
|
|
public function getData(): array {
|
2013-01-25 07:48:59 -05:00
|
|
|
return $this->data;
|
|
|
|
|
}
|
2014-01-28 11:42:26 -05:00
|
|
|
|
2013-01-25 07:48:59 -05:00
|
|
|
/**
|
2014-09-15 11:08:56 -04:00
|
|
|
* return bool Whether the method succeeded
|
2013-01-25 07:48:59 -05:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2023-07-05 13:43:36 -04:00
|
|
|
public function succeeded(): bool {
|
2014-04-02 11:50:51 -04:00
|
|
|
return ($this->statusCode == 100);
|
2012-12-12 11:50:25 -05:00
|
|
|
}
|
2013-01-14 14:30:39 -05:00
|
|
|
|
2015-08-07 07:12:43 -04:00
|
|
|
/**
|
|
|
|
|
* Adds a new header to the response
|
2023-07-05 13:43:36 -04:00
|
|
|
*
|
2015-08-07 07:12:43 -04:00
|
|
|
* @param string $name The name of the HTTP header
|
|
|
|
|
* @param string $value The value, null will delete it
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
2023-07-05 13:43:36 -04:00
|
|
|
public function addHeader(string $name, ?string $value): static {
|
2015-08-07 07:12:43 -04:00
|
|
|
$name = trim($name); // always remove leading and trailing whitespace
|
|
|
|
|
// to be able to reliably check for security
|
|
|
|
|
// headers
|
|
|
|
|
|
2020-04-10 08:19:56 -04:00
|
|
|
if (is_null($value)) {
|
2015-08-07 07:12:43 -04:00
|
|
|
unset($this->headers[$name]);
|
|
|
|
|
} else {
|
|
|
|
|
$this->headers[$name] = $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the set headers
|
|
|
|
|
* @return array the headers
|
|
|
|
|
*/
|
2023-07-05 13:43:36 -04:00
|
|
|
public function getHeaders(): array {
|
2015-08-07 07:12:43 -04:00
|
|
|
return $this->headers;
|
|
|
|
|
}
|
2013-08-18 05:02:08 -04:00
|
|
|
}
|