Skip to content

Commit 855e65a

Browse files
committed
[fix] result handle on response
1 parent 25e9547 commit 855e65a

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
lines changed

src/Base/Concerns/HasApi.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Contracts\Support\Arrayable;
99
use Illuminate\Http\Resources\Json\JsonResource;
1010
use Illuminate\Http\Resources\Json\ResourceCollection;
11+
use Kfn\Base\Contracts\IResponseResult;
1112
use Kfn\Base\Enums\ResponseCode;
1213
use Kfn\Base\Response;
1314

@@ -20,7 +21,7 @@ trait HasApi
2021
protected array $responseMessages;
2122

2223
/**
23-
* Use to get response message.
24+
* Use to get a response message.
2425
*
2526
* @param string $context
2627
*
@@ -37,6 +38,7 @@ public function getResponseMessage(string $context): string
3738
* @param ResponseCode $rc
3839
* @param array $headers
3940
* @param array $extra
41+
* @param IResponseResult|null $as
4042
*
4143
* @return Response
4244
*/
@@ -45,13 +47,14 @@ public function response(
4547
string|null $message = null,
4648
ResponseCode $rc = ResponseCode::SUCCESS,
4749
array $headers = [],
48-
array $extra = []
50+
array $extra = [],
51+
IResponseResult|null $as = null
4952
): Response {
5053
if (is_null($this->accept)) {
5154
$this->acceptJson();
5255
}
5356

54-
return new Response($data, $message, $rc, $headers, $extra);
57+
return new Response($data, $message, $rc, $headers, $extra, $as);
5558
}
5659

5760
public function contentType(string $contentType): static

src/Base/Response.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,26 @@ class Response implements IResponse, Responsable
3535

3636
/** @var int */
3737
public static int $jsonOption = JSON_THROW_ON_ERROR;
38+
39+
/** @var ResponseHeaderBag */
3840
public ResponseHeaderBag $headers;
41+
42+
/** @var string */
3943
protected string $content;
4044
protected string $version;
45+
46+
/** @var int */
4147
protected int $statusCode;
48+
49+
/** @var string */
4250
protected string $statusText;
51+
52+
/** @var string|null */
4353
protected string|null $charset = null;
4454

55+
/** @var array */
56+
public array $extra = [];
57+
4558
/**
4659
* Response constructor.
4760
*
@@ -50,16 +63,19 @@ class Response implements IResponse, Responsable
5063
* @param IResponseCode $code
5164
* @param array $headers
5265
* @param array $extra
66+
* @param IResponseResult|null $as
5367
*/
5468
public function __construct(
5569
public array|Arrayable|CursorPaginator|JsonResource|LengthAwarePaginator|ResourceCollection|string|null $data = null,
5670
public string|null $message = null,
5771
public IResponseCode $code = ResponseCode::SUCCESS,
5872
array $headers = [],
59-
public array $extra = [],
73+
array $extra = [],
74+
private readonly IResponseResult|null $as = null
6075
) {
6176
//
6277
$this->headers = new ResponseHeaderBag($headers);
78+
$this->extra = array_merge($this->extra, $extra);
6379
}
6480

6581
/**
@@ -102,7 +118,11 @@ public function getResponseData(): array
102118
default => $this->data,
103119
};
104120

105-
if (static::$resultAs == ResponseResult::CUSTOM) {
121+
if ($this->as instanceof IResponseResult && static::$resultAs !== ResponseResult::CUSTOM) {
122+
static::$resultAs = $this->as;
123+
}
124+
125+
if (static::$resultAs === ResponseResult::CUSTOM) {
106126
if (static::$customResult instanceof Closure) {
107127
return call_user_func(static::$customResult, $this, $payload);
108128
}

src/UI/Response.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,24 @@
1717
use Illuminate\Support\Fluent;
1818
use Kfn\Base\Contracts\IResponseCode;
1919
use Kfn\Base\Enums\ResponseCode;
20+
use Kfn\Base\Enums\ResponseResult;
2021
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
2122

2223
class Response extends \Kfn\Base\Response implements Responsable
2324
{
25+
/** @var Response|string|View|null */
2426
private Response|string|View|null $view = null;
27+
28+
/** @var Fluent */
2529
private Fluent $viewOption;
30+
31+
/** @var string|null */
2632
private string|null $redirect = null;
33+
34+
/** @var Fluent */
2735
private Fluent $redirectOption;
36+
37+
/** @var array */
2838
private array $with = [];
2939

3040
/** {@inheritdoc} */
@@ -35,23 +45,25 @@ public function __construct(
3545
array $headers = [],
3646
array $extra = []
3747
) {
38-
parent::__construct($data, $message, $code, $headers, $extra);
48+
parent::__construct($data, $message, $code, $headers, $extra, ResponseResult::DEFAULT);
3949
$this->redirectOption = new Fluent;
4050
$this->viewOption = new Fluent;
4151
}
4252

4353
/** {@inheritdoc} */
4454
public function toResponse($request): HttpResponse|JsonResponse|SymfonyResponse
4555
{
46-
if (! $request->expectsJson()) {
47-
if (in_array($this->redirect, ['back', 'to', 'intended', 'action', 'route'])) {
48-
$response = null;
49-
$this->handleRedirect($request, $response);
56+
$response = null;
5057

51-
if ($response instanceof RedirectResponse) {
52-
return $response->send();
53-
}
58+
if (in_array($this->redirect, ['back', 'to', 'intended', 'action', 'route'])) {
59+
$this->handleRedirect($request, $response);
60+
}
61+
62+
if (! $request->expectsJson()) {
63+
if ($response instanceof RedirectResponse) {
64+
return $response->send();
5465
}
66+
5567
if (! is_null($this->view)) {
5668
return new HttpResponse($this->handleView());
5769
}

0 commit comments

Comments
 (0)