Skip to content

Commit

Permalink
Add template types to responses
Browse files Browse the repository at this point in the history
Signed-off-by: jld3103 <jld3103yt@gmail.com>
  • Loading branch information
provokateurin committed Jun 27, 2023
1 parent 266436b commit e524051
Show file tree
Hide file tree
Showing 20 changed files with 215 additions and 87 deletions.
10 changes: 9 additions & 1 deletion lib/private/AppFramework/OCS/BaseResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @author Joas Schilling <coding@schilljs.com>
* @author Lukas Reschke <lukas@statuscode.ch>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Kate Döen <kate.doeen@nextcloud.com>
*
* @license GNU AGPL version 3 or any later version
*
Expand All @@ -30,6 +31,13 @@
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\Response;

/**
* @psalm-import-type DataResponseType from DataResponse
* @template S of int
* @template-covariant T of DataResponseType
* @template H of array<string, mixed>
* @template-extends Response<int, array<string, mixed>>
*/
abstract class BaseResponse extends Response {
/** @var array */
protected $data;
Expand All @@ -49,7 +57,7 @@ abstract class BaseResponse extends Response {
/**
* BaseResponse constructor.
*
* @param DataResponse $dataResponse
* @param DataResponse<S, T, H> $dataResponse
* @param string $format
* @param string|null $statusMessage
* @param int|null $itemsCount
Expand Down
9 changes: 9 additions & 0 deletions lib/private/AppFramework/OCS/V1Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Joas Schilling <coding@schilljs.com>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Kate Döen <kate.doeen@nextcloud.com>
*
* @license GNU AGPL version 3 or any later version
*
Expand All @@ -25,8 +26,16 @@
namespace OC\AppFramework\OCS;

use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCSController;

/**
* @psalm-import-type DataResponseType from DataResponse
* @template S of int
* @template-covariant T of DataResponseType
* @template H of array<string, mixed>
* @template-extends BaseResponse<int, DataResponseType, array<string, mixed>>
*/
class V1Response extends BaseResponse {
/**
* The V1 endpoint has very limited http status codes basically everything
Expand Down
9 changes: 9 additions & 0 deletions lib/private/AppFramework/OCS/V2Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Kate Döen <kate.doeen@nextcloud.com>
*
* @license GNU AGPL version 3 or any later version
*
Expand All @@ -24,8 +25,16 @@
namespace OC\AppFramework\OCS;

use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCSController;

/**
* @psalm-import-type DataResponseType from DataResponse
* @template S of int
* @template-covariant T of DataResponseType
* @template H of array<string, mixed>
* @template-extends BaseResponse<int, DataResponseType, array<string, mixed>>
*/
class V2Response extends BaseResponse {
/**
* The V2 endpoint just passes on status codes.
Expand Down
15 changes: 8 additions & 7 deletions lib/public/AppFramework/Http/DataDisplayResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* @author Julius Härtl <jus@bitgrid.net>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Kate Döen <kate.doeen@nextcloud.com>
*
* @license AGPL-3.0
*
Expand All @@ -30,6 +31,9 @@
* Class DataDisplayResponse
*
* @since 8.1.0
* @template S of int
* @template H of array<string, mixed>
* @template-extends Response<int, array<string, mixed>>
*/
class DataDisplayResponse extends Response {
/**
Expand All @@ -41,17 +45,14 @@ class DataDisplayResponse extends Response {

/**
* @param string $data the data to display
* @param int $statusCode the Http status code, defaults to 200
* @param array $headers additional key value based headers
* @param S $statusCode the Http status code, defaults to 200
* @param H $headers additional key value based headers
* @since 8.1.0
*/
public function __construct($data = '', $statusCode = Http::STATUS_OK,
$headers = []) {
parent::__construct();
public function __construct($data = '', int $statusCode = Http::STATUS_OK, array $headers = []) {
parent::__construct($statusCode, $headers);

$this->data = $data;
$this->setStatus($statusCode);
$this->setHeaders(array_merge($this->getHeaders(), $headers));
$this->addHeader('Content-Disposition', 'inline; filename=""');
}

Expand Down
15 changes: 12 additions & 3 deletions lib/public/AppFramework/Http/DataDownloadResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @author Georg Ehrke <oc.list@georgehrke.com>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Kate Döen <kate.doeen@nextcloud.com>
*
* @license AGPL-3.0
*
Expand All @@ -23,10 +24,16 @@
*/
namespace OCP\AppFramework\Http;

use OCP\AppFramework\Http;

/**
* Class DataDownloadResponse
*
* @since 8.0.0
* @template S of int
* @template C of string
* @template H of array<string, mixed>
* @template-extends DownloadResponse<int, string, array<string, mixed>>
*/
class DataDownloadResponse extends DownloadResponse {
/**
Expand All @@ -38,12 +45,14 @@ class DataDownloadResponse extends DownloadResponse {
* Creates a response that prompts the user to download the text
* @param string $data text to be downloaded
* @param string $filename the name that the downloaded file should have
* @param string $contentType the mimetype that the downloaded file should have
* @param C $contentType the mimetype that the downloaded file should have
* @param S $status
* @param H $headers
* @since 8.0.0
*/
public function __construct($data, $filename, $contentType) {
public function __construct($data, $filename, $contentType, int $status = Http::STATUS_OK, array $headers = []) {
$this->data = $data;
parent::__construct($filename, $contentType);
parent::__construct($filename, $contentType, $status, $headers);
}

/**
Expand Down
26 changes: 15 additions & 11 deletions lib/public/AppFramework/Http/DataResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Kate Döen <kate.doeen@nextcloud.com>
*
* @license AGPL-3.0
*
Expand All @@ -30,34 +31,37 @@
* A generic DataResponse class that is used to return generic data responses
* for responders to transform
* @since 8.0.0
* @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>>
*/
class DataResponse extends Response {
/**
* response data
* @var array|int|float|string|bool|object
* @var T
*/
protected $data;


/**
* @param array|int|float|string|bool|object $data the object or array that should be transformed
* @param int $statusCode the Http status code, defaults to 200
* @param array $headers additional key value based headers
* @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
* @since 8.0.0
*/
public function __construct($data = [], $statusCode = Http::STATUS_OK,
array $headers = []) {
parent::__construct();
public function __construct($data = [], int $statusCode = Http::STATUS_OK, array $headers = []) {
parent::__construct($statusCode, $headers);

$this->data = $data;
$this->setStatus($statusCode);
$this->setHeaders(array_merge($this->getHeaders(), $headers));
}


/**
* Sets values in the data json array
* @param array|int|float|string|object $data an array or object which will be transformed
* @psalm-suppress InvalidTemplateParam
* @param T $data an array or object which will be transformed
* @return DataResponse Reference to this object
* @since 8.0.0
*/
Expand All @@ -70,7 +74,7 @@ public function setData($data) {

/**
* Used to get the set parameters
* @return array|int|float|string|bool|object the data
* @return T the data
* @since 8.0.0
*/
public function getData() {
Expand Down
15 changes: 12 additions & 3 deletions lib/public/AppFramework/Http/DownloadResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @author Morris Jobke <hey@morrisjobke.de>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Thomas Müller <thomas.mueller@tmit.eu>
* @author Kate Döen <kate.doeen@nextcloud.com>
*
* @license AGPL-3.0
*
Expand All @@ -25,19 +26,27 @@
*/
namespace OCP\AppFramework\Http;

use OCP\AppFramework\Http;

/**
* Prompts the user to download the a file
* @since 7.0.0
* @template S of int
* @template C of string
* @template H of array<string, mixed>
* @template-extends Response<int, array<string, mixed>>
*/
class DownloadResponse extends Response {
/**
* Creates a response that prompts the user to download the file
* @param string $filename the name that the downloaded file should have
* @param string $contentType the mimetype that the downloaded file should have
* @param C $contentType the mimetype that the downloaded file should have
* @param S $status
* @param H $headers
* @since 7.0.0
*/
public function __construct(string $filename, string $contentType) {
parent::__construct();
public function __construct(string $filename, string $contentType, int $status = Http::STATUS_OK, array $headers = []) {
parent::__construct($status, $headers);

$filename = strtr($filename, ['"' => '\\"', '\\' => '\\\\']);

Expand Down
15 changes: 8 additions & 7 deletions lib/public/AppFramework/Http/FileDisplayResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Kate Döen <kate.doeen@nextcloud.com>
*
* @license GNU AGPL version 3 or any later version
*
Expand All @@ -29,6 +30,9 @@
* Class FileDisplayResponse
*
* @since 11.0.0
* @template S of int
* @template H of array<string, mixed>
* @template-extends Response<int, array<string, mixed>>
*/
class FileDisplayResponse extends Response implements ICallbackResponse {
/** @var \OCP\Files\File|\OCP\Files\SimpleFS\ISimpleFile */
Expand All @@ -38,17 +42,14 @@ class FileDisplayResponse extends Response implements ICallbackResponse {
* FileDisplayResponse constructor.
*
* @param \OCP\Files\File|\OCP\Files\SimpleFS\ISimpleFile $file
* @param int $statusCode
* @param array $headers
* @param S $statusCode
* @param H $headers
* @since 11.0.0
*/
public function __construct($file, $statusCode = Http::STATUS_OK,
$headers = []) {
parent::__construct();
public function __construct($file, int $statusCode = Http::STATUS_OK, array $headers = []) {
parent::__construct($statusCode, $headers);

$this->file = $file;
$this->setStatus($statusCode);
$this->setHeaders(array_merge($this->getHeaders(), $headers));
$this->addHeader('Content-Disposition', 'inline; filename="' . rawurldecode($file->getName()) . '"');

$this->setETag($file->getEtag());
Expand Down
23 changes: 14 additions & 9 deletions lib/public/AppFramework/Http/JSONResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Thomas Müller <thomas.mueller@tmit.eu>
* @author Thomas Tanghus <thomas@tanghus.net>
* @author Kate Döen <kate.doeen@nextcloud.com>
*
* @license AGPL-3.0
*
Expand All @@ -32,26 +33,30 @@
/**
* A renderer for JSON calls
* @since 6.0.0
* @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>>
*/
class JSONResponse extends Response {
/**
* response data
* @var array|object
* @var T
*/
protected $data;


/**
* constructor of JSONResponse
* @param array|object $data the object or array that should be transformed
* @param int $statusCode the Http status code, defaults to 200
* @param T $data the object or array that should be transformed
* @param S $statusCode the Http status code, defaults to 200
* @param H $headers
* @since 6.0.0
*/
public function __construct($data = [], $statusCode = Http::STATUS_OK) {
parent::__construct();
public function __construct($data = [], int $statusCode = Http::STATUS_OK, array $headers = []) {
parent::__construct($statusCode, $headers);

$this->data = $data;
$this->setStatus($statusCode);
$this->addHeader('Content-Type', 'application/json; charset=utf-8');
}

Expand All @@ -68,7 +73,8 @@ public function render() {

/**
* Sets values in the data json array
* @param array|object $data an array or object which will be transformed
* @psalm-suppress InvalidTemplateParam
* @param T $data an array or object which will be transformed
* to JSON
* @return JSONResponse Reference to this object
* @since 6.0.0 - return value was added in 7.0.0
Expand All @@ -81,8 +87,7 @@ public function setData($data) {


/**
* Used to get the set parameters
* @return array the data
* @return T the data
* @since 6.0.0
*/
public function getData() {
Expand Down

0 comments on commit e524051

Please sign in to comment.