Skip to content
This repository was archived by the owner on Feb 14, 2023. It is now read-only.

Commit a562196

Browse files
author
Andrey Helldar
committed
Fixed status type overriding
1 parent 4a03d72 commit a562196

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

src/Services/Response.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ final class Response
2626
/** @var int */
2727
protected $status_code = 200;
2828

29-
/** @var string */
29+
/** @var string|null */
3030
protected $status_type;
3131

3232
/**
@@ -37,28 +37,33 @@ public static function init(): self
3737
return new self();
3838
}
3939

40+
public function exception(string $status_type = null): self
41+
{
42+
$this->status_type = $status_type;
43+
44+
return $this;
45+
}
46+
4047
/**
4148
* @param mixed $data
4249
* @param int $status_code
4350
* @param bool $use_data
44-
* @param string|null $exception
4551
*
4652
* @throws \ReflectionException
4753
*
4854
* @return $this
4955
*/
50-
public function data($data = null, int $status_code = 200, bool $use_data = true, string $exception = BaseException::class): self
56+
public function data($data = null, int $status_code = 200, bool $use_data = true): self
5157
{
5258
$this->use_data = $use_data;
5359
$this->status_code = $status_code;
5460

5561
if (Exception::isError($data)) {
5662
$this->status_code = Exception::getCode($data, $status_code);
57-
$this->status_type = Exception::getType($data, $exception);
63+
$this->status_type = Exception::getType($data, $this->status_type);
5864
$this->data = Exception::getData($data);
5965
} else {
60-
$this->status_type = Instance::basename($exception);
61-
$this->data = ResponseSupport::get($data);
66+
$this->data = ResponseSupport::get($data);
6267
}
6368

6469
return $this;
@@ -112,6 +117,13 @@ protected function e($value = null, $doubleEncode = true)
112117
: null;
113118
}
114119

120+
protected function getStatusType(): string
121+
{
122+
return Instance::basename(
123+
$this->status_type ?: BaseException::class
124+
);
125+
}
126+
115127
protected function getData()
116128
{
117129
$this->splitData();
@@ -127,7 +139,7 @@ protected function getErrorData(): array
127139
{
128140
return [
129141
'error' => [
130-
'type' => $this->status_type,
142+
'type' => $this->getStatusType(),
131143
'data' => $this->e($this->data),
132144
],
133145
];

src/Support/Exception.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public static function getCode($value, int $status_code = 400): int
5656

5757
public static function getType(Throwable $class, string $exception = null): string
5858
{
59-
return Instance::basename($exception ?: $class);
59+
return $exception ?: Instance::classname($class);
6060
}
6161

6262
public static function getData($exception)

src/Support/Instance.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,16 @@ public static function of($haystack, $needles): bool
4848
*/
4949
public static function basename($class): string
5050
{
51-
$class = static::isObject($class) ? get_class($class) : $class;
51+
$class = static::classname($class);
5252

5353
return basename(str_replace('\\', '/', $class));
5454
}
5555

56+
public static function classname($class): string
57+
{
58+
return static::isObject($class) ? get_class($class) : $class;
59+
}
60+
5661
/**
5762
* @param Throwable $object
5863
* @param string $method

src/helpers.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ function api_response(
2525
string $exception = null
2626
) {
2727
return Response::init()
28-
->data($data, $status_code, $use_data, $exception)
28+
->exception($exception)
29+
->data($data, $status_code, $use_data)
2930
->with($with)
3031
->headers($headers)
3132
->response();

0 commit comments

Comments
 (0)