2013-08-17 05:16:48 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
2016-07-21 11:07:57 -04:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
|
*
|
2015-03-26 06:44:34 -04:00
|
|
|
* @author Bernhard Posselt <dev@bernhard-posselt.com>
|
2020-03-31 04:49:10 -04:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2016-05-26 13:56:05 -04:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 06:44:34 -04:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2019-12-03 13:57:53 -05:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-03-26 06:44:34 -04:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
|
* @author Thomas Tanghus <thomas@tanghus.net>
|
2023-06-14 02:56:42 -04:00
|
|
|
* @author Kate Döen <kate.doeen@nextcloud.com>
|
2013-08-17 05:16:48 -04:00
|
|
|
*
|
2015-03-26 06:44:34 -04:00
|
|
|
* @license AGPL-3.0
|
2013-08-17 05:16:48 -04:00
|
|
|
*
|
2015-03-26 06:44:34 -04:00
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
|
* as published by the Free Software Foundation.
|
2013-08-17 05:16:48 -04:00
|
|
|
*
|
2015-03-26 06:44:34 -04:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2013-08-17 05:16:48 -04:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 06:44:34 -04:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU Affero General Public License for more details.
|
2013-08-17 05:16:48 -04:00
|
|
|
*
|
2015-03-26 06:44:34 -04:00
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 13:57:53 -05:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2013-08-17 05:16:48 -04:00
|
|
|
*
|
|
|
|
|
*/
|
2013-08-20 19:00:26 -04:00
|
|
|
namespace OCP\AppFramework\Http;
|
2013-08-17 05:16:48 -04:00
|
|
|
|
2013-10-22 23:57:34 -04:00
|
|
|
use OCP\AppFramework\Http;
|
2013-08-17 05:16:48 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A renderer for JSON calls
|
2015-04-16 11:00:08 -04:00
|
|
|
* @since 6.0.0
|
2023-06-14 02:56:42 -04:00
|
|
|
* @template S of int
|
|
|
|
|
* @template-covariant T of array|object|\stdClass|\JsonSerializable
|
|
|
|
|
* @template H of array<string, mixed>
|
|
|
|
|
* @template-extends Response<int, array<string, mixed>>
|
2013-08-17 05:16:48 -04:00
|
|
|
*/
|
|
|
|
|
class JSONResponse extends Response {
|
2013-11-25 10:28:24 -05:00
|
|
|
/**
|
|
|
|
|
* response data
|
2023-06-14 02:56:42 -04:00
|
|
|
* @var T
|
2013-11-25 10:28:24 -05:00
|
|
|
*/
|
2013-08-17 05:16:48 -04:00
|
|
|
protected $data;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-11-25 10:28:24 -05:00
|
|
|
* constructor of JSONResponse
|
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
|
2015-04-16 11:00:08 -04:00
|
|
|
* @since 6.0.0
|
2013-08-17 05:16:48 -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
|
|
|
|
2013-08-17 05:16:48 -04:00
|
|
|
$this->data = $data;
|
2014-11-05 06:04:56 -05:00
|
|
|
$this->addHeader('Content-Type', 'application/json; charset=utf-8');
|
2013-08-17 05:16:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the rendered json
|
|
|
|
|
* @return string the rendered json
|
2015-04-16 11:00:08 -04:00
|
|
|
* @since 6.0.0
|
2015-07-01 14:47:04 -04:00
|
|
|
* @throws \Exception If data could not get encoded
|
2013-08-17 05:16:48 -04:00
|
|
|
*/
|
2015-07-01 14:47:04 -04:00
|
|
|
public function render() {
|
2022-05-27 08:57:22 -04:00
|
|
|
return json_encode($this->data, JSON_HEX_TAG | JSON_THROW_ON_ERROR);
|
2013-08-17 05:16:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
2013-08-17 05:16:48 -04:00
|
|
|
* to JSON
|
2014-03-10 04:31:30 -04:00
|
|
|
* @return JSONResponse Reference to this object
|
2015-04-16 11:00:08 -04:00
|
|
|
* @since 6.0.0 - return value was added in 7.0.0
|
2013-08-17 05:16:48 -04:00
|
|
|
*/
|
2020-04-09 07:53:40 -04:00
|
|
|
public function setData($data) {
|
2013-08-17 05:16:48 -04:00
|
|
|
$this->data = $data;
|
2014-03-09 18:01:16 -04:00
|
|
|
|
|
|
|
|
return $this;
|
2013-08-17 05:16:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-06-14 02:56:42 -04:00
|
|
|
* @return T the data
|
2015-04-16 11:00:08 -04:00
|
|
|
* @since 6.0.0
|
2013-08-17 05:16:48 -04:00
|
|
|
*/
|
2020-04-09 07:53:40 -04:00
|
|
|
public function getData() {
|
2013-08-17 05:16:48 -04:00
|
|
|
return $this->data;
|
|
|
|
|
}
|
|
|
|
|
}
|