2014-10-28 11:34:04 -04:00
|
|
|
<?php
|
2024-05-23 03:26:56 -04:00
|
|
|
|
2014-10-28 11:34:04 -04: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
|
2014-10-28 11:34:04 -04:00
|
|
|
*/
|
|
|
|
|
namespace OCP\AppFramework\Http;
|
|
|
|
|
|
|
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A generic DataResponse class that is used to return generic data responses
|
|
|
|
|
* for responders to transform
|
2015-04-16 11:00:08 -04:00
|
|
|
* @since 8.0.0
|
2023-06-14 02:56:42 -04:00
|
|
|
* @psalm-type DataResponseType = array|int|float|string|bool|object|null|\stdClass|\JsonSerializable
|
|
|
|
|
* @template S of int
|
|
|
|
|
* @template-covariant T of DataResponseType
|
|
|
|
|
* @template H of array<string, mixed>
|
|
|
|
|
* @template-extends Response<int, array<string, mixed>>
|
2014-10-28 11:34:04 -04:00
|
|
|
*/
|
|
|
|
|
class DataResponse extends Response {
|
|
|
|
|
/**
|
|
|
|
|
* response data
|
2023-06-14 02:56:42 -04:00
|
|
|
* @var T
|
2014-10-28 11:34:04 -04:00
|
|
|
*/
|
|
|
|
|
protected $data;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-06-14 02:56:42 -04:00
|
|
|
* @param T $data the object or array that should be transformed
|
|
|
|
|
* @param S $statusCode the Http status code, defaults to 200
|
|
|
|
|
* @param H $headers additional key value based headers
|
2015-04-16 11:00:08 -04:00
|
|
|
* @since 8.0.0
|
2014-10-28 11:34:04 -04:00
|
|
|
*/
|
2023-06-14 02:56:42 -04:00
|
|
|
public function __construct(mixed $data = [], int $statusCode = Http::STATUS_OK, array $headers = []) {
|
|
|
|
|
parent::__construct($statusCode, $headers);
|
2019-04-03 12:42:34 -04:00
|
|
|
|
2014-10-28 11:34:04 -04:00
|
|
|
$this->data = $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets values in the data json array
|
2023-06-14 02:56:42 -04:00
|
|
|
* @psalm-suppress InvalidTemplateParam
|
|
|
|
|
* @param T $data an array or object which will be transformed
|
2014-10-28 11:34:04 -04:00
|
|
|
* @return DataResponse Reference to this object
|
2015-04-16 11:00:08 -04:00
|
|
|
* @since 8.0.0
|
2014-10-28 11:34:04 -04:00
|
|
|
*/
|
2020-04-09 07:53:40 -04:00
|
|
|
public function setData($data) {
|
2014-10-28 11:34:04 -04:00
|
|
|
$this->data = $data;
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Used to get the set parameters
|
2023-06-14 02:56:42 -04:00
|
|
|
* @return T the data
|
2015-04-16 11:00:08 -04:00
|
|
|
* @since 8.0.0
|
2014-10-28 11:34:04 -04:00
|
|
|
*/
|
2020-04-09 07:53:40 -04:00
|
|
|
public function getData() {
|
2014-10-28 11:34:04 -04:00
|
|
|
return $this->data;
|
|
|
|
|
}
|
|
|
|
|
}
|