2013-08-17 05:16:48 -04:00
|
|
|
<?php
|
2024-05-23 03:26:56 -04:00
|
|
|
|
2013-08-17 05:16:48 -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
|
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;
|
2024-07-30 21:19:55 -04:00
|
|
|
/**
|
|
|
|
|
* Additional `json_encode` flags
|
|
|
|
|
* @var int
|
|
|
|
|
*/
|
|
|
|
|
protected $encodeFlags;
|
2013-08-17 05:16:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
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
|
2024-07-30 21:19:55 -04:00
|
|
|
* @param int $encodeFlags Additional `json_encode` flags
|
2015-04-16 11:00:08 -04:00
|
|
|
* @since 6.0.0
|
2024-07-30 21:19:55 -04:00
|
|
|
* @since 30.0.0 Added `$encodeFlags` param
|
2013-08-17 05:16:48 -04:00
|
|
|
*/
|
2024-07-30 21:19:55 -04:00
|
|
|
public function __construct(
|
|
|
|
|
mixed $data = [],
|
|
|
|
|
int $statusCode = Http::STATUS_OK,
|
|
|
|
|
array $headers = [],
|
|
|
|
|
int $encodeFlags = 0,
|
|
|
|
|
) {
|
2023-06-14 02:56:42 -04:00
|
|
|
parent::__construct($statusCode, $headers);
|
2019-04-03 12:42:34 -04:00
|
|
|
|
2013-08-17 05:16:48 -04:00
|
|
|
$this->data = $data;
|
2024-07-30 21:19:55 -04:00
|
|
|
$this->encodeFlags = $encodeFlags;
|
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() {
|
2024-07-30 21:19:55 -04:00
|
|
|
return json_encode($this->data, JSON_HEX_TAG | JSON_THROW_ON_ERROR | $this->encodeFlags, 2048);
|
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
|
2024-08-23 09:10:27 -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;
|
|
|
|
|
}
|
|
|
|
|
}
|