From 286bbc561bb755fde55248828f76e7af538d0053 Mon Sep 17 00:00:00 2001 From: Digit Date: Thu, 11 Jul 2024 10:44:52 +0300 Subject: [PATCH] Add `isSuccessful` property to `Response` annotation to override check by the status. --- composer.json | 2 +- oa/Response.php | 15 +++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index 33d9f93..5ba742a 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "Swagger yaml generator", "keywords": ["api", "swagger", "open auth"], "license": "MIT", - "version": "1.4.10", + "version": "1.4.11", "authors": [ { "name": "Digit", diff --git a/oa/Response.php b/oa/Response.php index 721a0ef..38abe42 100644 --- a/oa/Response.php +++ b/oa/Response.php @@ -18,6 +18,7 @@ * @Target("METHOD") * @Attributes({ * @Attribute("status",type="integer"), + * @Attribute("isSuccessful",type="boolean"), * @Attribute("contentType",type="string"), * @Attribute("description",type="string"), * @Attribute("asList",type="boolean"), @@ -30,6 +31,7 @@ class Response extends BaseAnnotation public mixed $content = null; public string $contentType = 'application/json'; public int $status = 200; + public ?bool $isSuccessful = null; public ?string $description = null; public bool $asList = false; public bool $asPagedList = false; @@ -179,12 +181,12 @@ public function __toString() protected function wrapInDefaultResponse(mixed $content = null): mixed { $content = $content ?? $this->content; - $responseData = static::getDefaultResponse($this->contentType, $this->status); + $responseData = static::getDefaultResponse($this->contentType, $this->status, $this->isSuccessful); if ($responseData === null) { return $content; } [$responseRaw, $resultKey] = array_values($responseData); - if (($this->asPagedList || $this->asCursorPagedList) && static::isSuccessStatus($this->status)) { + if (($this->asPagedList || $this->asCursorPagedList) && ($this->isSuccessful ?? static::isSuccessStatus($this->status))) { if ($this->asPagedList) { $responseRaw['pagination'] = static::getPagerExample(); } elseif ($this->asCursorPagedList) { @@ -200,13 +202,14 @@ protected function wrapInDefaultResponse(mixed $content = null): mixed /** * Get default response by content type [response, result_array_key]. * - * @param string $contentType - * @param int $status + * @param string $contentType + * @param int $status + * @param bool|null $isSuccessful Override check by status for success/error response * @return mixed|null */ - protected static function getDefaultResponse(string $contentType, int $status = 200): mixed + protected static function getDefaultResponse(string $contentType, int $status = 200, ?bool $isSuccessful = null): mixed { - $key = static::isSuccessStatus($status) ? 'ok' : 'error'; + $key = ($isSuccessful ?? static::isSuccessStatus($status)) ? 'ok' : 'error'; $responses = [ 'application/json' => [ 'ok' => [