Skip to content

Commit ae0dd8f

Browse files
committed
add base response success
1 parent 6680e1f commit ae0dd8f

3 files changed

Lines changed: 39 additions & 12 deletions

File tree

.openapi.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,16 @@
4949
* You can override or extend this list in your config file.
5050
*/
5151
'exclude_dirs' => [],
52+
53+
/**
54+
* Response Data Structure Definition
55+
*
56+
* Defines the basic structure of API responses, including status code, return message, and data body
57+
*
58+
*/
5259
'response' => [
53-
'code' => '状态码',
54-
'message' => '返回信息',
55-
'data' => 'object',
60+
'code' => ['description' =>'状态码', 'example'=> 200],
61+
'message' => ['description' =>'返回信息', 'example'=> '操作成功'],
62+
'data' => 'T',
5663
]
5764
];

src/OpenApi/Storage/OpenAPI/ResponseStorage.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ public function addGlobParameters(array $vols): void
3030

3131
$this->parameter['properties'] = [];
3232

33-
foreach ($vols as $field => $description){
34-
if($description !== 'object'){
35-
$this->parameter['properties'][$field] = [
36-
'type' => 'string',
37-
'description' => $description,
38-
];
39-
}else{
33+
foreach ($vols as $field => $item){
34+
if($item === 'T'){
4035
$this->parameter['properties'][$field] = [
4136
'type' => 'object',
42-
'description' => $description,
4337
'properties' => $dates,
4438
'required' => $required,
4539
];
40+
}else{
41+
$this->parameter['properties'][$field] = [
42+
'type' => 'string',
43+
'description' => $item['description'] ?? '',
44+
'example' => $item['example'],
45+
];
4646
}
4747

4848
}

src/Serialize.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace Astral\Serialize;
44

5+
use Astral\Serialize\OpenApi\Handler\Config;
56
use Astral\Serialize\Support\Context\SerializeContext;
67
use Astral\Serialize\Support\Factories\ContextFactory;
8+
use JsonSerializable;
79

8-
abstract class Serialize
10+
abstract class Serialize implements JsonSerializable
911
{
1012
private ?SerializeContext $_context = null;
1113

@@ -71,4 +73,22 @@ public function __debugInfo()
7173

7274
return $res;
7375
}
76+
77+
public function jsonSerialize(): array
78+
{
79+
$baseResponse = Config::get('response',[]);
80+
if($baseResponse){
81+
$resultData = [];
82+
foreach ($baseResponse as $field => $item){
83+
if($item === 'T'){
84+
$resultData[$field] = $this->toArray();
85+
}else{
86+
$resultData[$field] = $item['example'] ?? '';
87+
}
88+
}
89+
return $resultData;
90+
}
91+
92+
return $this->toArray();
93+
}
7494
}

0 commit comments

Comments
 (0)