Skip to content

Commit d32e138

Browse files
authored
Featrue/normalizers (#9)
* add Normalizer * add Normalizer * add Normalizer * add Normalizer * fixed docs * fixed docs * fixed toArray Bug * added JsonSerializable * add Normalizer fixed toArray * add cast tests * add doc
1 parent a590ff7 commit d32e138

File tree

9 files changed

+227
-8
lines changed

9 files changed

+227
-8
lines changed

.openapi.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@
5757
*
5858
*/
5959
'response' => [
60-
'code' => ['description' =>'状态码', 'example'=> 200],
61-
'message' => ['description' =>'返回信息', 'example'=> '操作成功'],
60+
'code' => ['description' =>'code', 'example'=> 200],
61+
'message' => ['description' =>'message', 'example'=> 'success'],
6262
'data' => 'T',
6363
]
6464
];

docs/en/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* [Enum Mapping](mapper/enum-mapper.md)
1111
* [Array Object Mapping](mapper/array-mapper.md)
1212
* [Union Type Mapping](mapper/union-mapper.md)
13+
* [Output Format](mapper/out.md)
1314

1415
## Annotation Usage
1516

docs/en/mapper/out.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
## Output Format
2+
3+
### Creating the Serialize Class
4+
5+
```php
6+
use Astral\Serialize\Serialize;
7+
use DateTime;
8+
9+
class UserLoginLog extends Serialize {
10+
public string $remark,
11+
public DateTime $create_time;
12+
}
13+
14+
class User extends Serialize {
15+
public string $name,
16+
public int $age,
17+
public UserLoginLog $login_log
18+
}
19+
20+
// Create an object
21+
$user = User::from([
22+
'name' => 'Jon',
23+
'age' => 30
24+
], login_log: new UserLoginLog(remark:'Test Data',create_time: DateTime::createFromFormat('Y-m-d','2008-09-01')));
25+
````
26+
27+
### Outputting the Object
28+
29+
```php
30+
// $user is an object by default
31+
echo $user->name; // Output: Jon
32+
echo $user->age; // Output: 30
33+
echo $user->login_log->remark; // Output 'Test Data'
34+
echo $user->login_log->create_time; // Output DateTime Object
35+
36+
```
37+
38+
### Outputting an Array
39+
40+
```php
41+
// Convert to an array
42+
$vols = $user->toArray();
43+
echo $vols['name']; // Output: Jon
44+
echo $vols['age']; // Output: 30
45+
echo $vols['login_log']['remark']; // Output 'Test Data'
46+
echo $vols['login_log']['create_time']; // Output 2008-09-01 00:00:00
47+
// Content of $vols:
48+
// [
49+
// 'name' => 'Jon',
50+
// 'age' => 30,
51+
// 'login_log' => [
52+
// [
53+
// 'remark' => 'Test Data',
54+
// 'create_time' => '2008-09-01 00:00:00'
55+
// ]
56+
// ]
57+
// ]
58+
```
59+
60+
### Outputting a JSON String
61+
62+
1. The `Serialize` class implements `JsonSerializable` by default. Similar to a `Laravel` `Controller` you can directly return the object, and the framework will output the `JSON` information correctly
63+
2. By default, the JSON output from `Serialize` includes `data` `code` and `message` f you need to [replace/modify/add] these, please refer to the configuration information [Response Data Structure Definition](../openapi/config.md)
64+
65+
#### Outputting JSON Information
66+
67+
- You can use the API `toJsonString`
68+
- Alternatively, you can directly use `json_decode`
69+
70+
```php
71+
echo $user->toJsonString();
72+
echo json_decode($user);
73+
// Both outputs are the same
74+
// {"code":200,"message":"success","data":{"name":"Jon","age":30,"login_log":{"remark":"Test Data","create_time":"2008-09-01 00:00:00"}}
75+
```
76+
77+
#### Setting Output Code/Message
78+
79+
```php
80+
$user->setCode(500);
81+
$user->setMessage('Operation failed');
82+
echo json_decode($user);
83+
// Output
84+
// {"code":500,"message":"Operation failed","data":{"name":"Jon","age":30,"login_log":{"remark":"Test Data","create_time":"2008-09-01 00:00:00"}}
85+
```
86+
87+
#### Setting Custom JSON Outer Layer
88+
89+
`withResponses` can temporarily add or modify custom return information. To add global return information, you can configure it in the [Response Data Structure Definition](../openapi/config.md)
90+
91+
```php
92+
$user->withResponses([
93+
"code"=> ["description"=>"Return Code", "value"=>401],
94+
"message"=> ["description"=>"Return Message", "value"=>"Operation successful"],
95+
"error"=> ["description"=>"Return Error", "value"=>0],
96+
]);
97+
// Output
98+
// {"code":401,"message":"Operation successful","error":0,"data":{"name":"Jon","age":30,"login_log":{"remark":"Test Data","create_time":"2008-09-01 00:00:00"}}
99+
```
100+
101+
#### Outputting JSON Without Outer Layer Information
102+
103+
Use`withoutResponseToJsonString` to return JSON data containing only the object’s properties.
104+
105+
```php
106+
$user->withoutResponseToJsonString();
107+
// Output
108+
// {"name":"Jon","age":30,"login_log":{"remark":"Test Data","create_time":"2008-09-01 00:00:00"}
109+
```

docs/en/openapi/config.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ return [
6868
*
6969
*/
7070
'response' => [
71-
'code' => ['description' =>'状态码', 'example'=> 200],
72-
'message' => ['description' =>'返回信息', 'example'=> '操作成功'],
71+
'code' => ['description' =>'code', 'example'=> 200],
72+
'message' => ['description' =>'message', 'example'=> 'success'],
7373
'data' => 'T',
7474
]
7575
];

docs/zh/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* [枚举转换](mapper/enum-mapper.md)
1111
* [数组对象转换](mapper/array-mapper.md)
1212
* [联合类型转换](mapper/union-mapper.md)
13+
* [输出格式](mapper/out.md)
1314

1415
## 注解类使用
1516

docs/zh/mapper/out.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
## 输出格式
2+
3+
### 创建Serialize类
4+
5+
```php
6+
use Astral\Serialize\Serialize;
7+
use DateTime;
8+
9+
class UserLoginLog extends Serialize {
10+
public string $remark,
11+
public DateTime $create_time;
12+
}
13+
14+
class User extends Serialize {
15+
public string $name,
16+
public int $age,
17+
public UserLoginLog $login_log
18+
}
19+
20+
// 创建对象
21+
$user = User::from([
22+
'name' => '张三',
23+
'age' => 30
24+
], login_log: new UserLoginLog(remark:'测试数据',create_time: DateTime::createFromFormat('Y-m-d','2008-09-01')));
25+
````
26+
27+
### 输出对象
28+
29+
```php
30+
// $user默认就是一个对象
31+
echo $user->name; // 输出: 张三
32+
echo $user->age; // 输出: 30
33+
echo $user->login_log->remark; // 输出 '测试数据'
34+
echo $user->login_log->create_time; // 输出 DateTime对象
35+
36+
```
37+
38+
### 输出数组
39+
40+
```php
41+
// 转换成数组
42+
$vols = $user->toArray();
43+
echo $vols['name']; // 输出: 张三
44+
echo $vols['age']; // 输出: 30
45+
echo $vols['login_log']['remark']; // 输出 '测试数据'
46+
echo $vols['login_log']['create_time']; // 输出 2008-09-01 00:00:00
47+
// $vols 的内容:
48+
// [
49+
// 'name' => '张三',
50+
// 'age' => 30,
51+
// 'login_log' => [
52+
// [
53+
// 'remark' => '测试数据',
54+
// 'create_time' => '2008-09-01 00:00:00'
55+
// ]
56+
// ]
57+
// ]
58+
```
59+
60+
### 输出数组json字符串
61+
62+
1. `Serialize` 默认实现了 `JsonSerializable` 类似`Laravel``Controller` 可以直接返回对象,框架会正常输出`json信息`
63+
2. `Serialize` 默认json 增加 `data` `code` `message` 如果需要`[替换/修改/增加]`
64+
请查看配置信息 [响应数据结构定义](../openapi/config.md)
65+
66+
#### 输出json信息
67+
68+
- 可以使用 api `toJsonString`
69+
- 也可以直接使用 `json_decode`
70+
71+
```php
72+
echo $user->toJsonString();
73+
echo json_decode($user);
74+
// 输出内容相同
75+
// {"code":200,"message":"success","data":{"name":"张三","age":30,"login_log":{"remark":"测试数据","create_time":"2008-09-01 00:00:00"}}
76+
```
77+
78+
#### 设置输出code/message
79+
80+
```php
81+
$user->setCode(500);
82+
$user->setMessage('操作失败');
83+
echo json_decode($user);
84+
// 输出内容
85+
// {"code":500,"message":"操作失败","data":{"name":"张三","age":30,"login_log":{"remark":"测试数据","create_time":"2008-09-01 00:00:00"}}
86+
```
87+
88+
#### 设置自定义json外层
89+
90+
`withResponses` 可以临时增加修改自定义返回信息,全局增加返回信息可以在 [响应数据结构定义](../openapi/config.md)中配置
91+
92+
```php
93+
$user->withResponses([
94+
"code"=> ["description"=>"返回code", "value"=>401],
95+
"message"=> ["description"=>"返回信息", "value"=>"操作成功"],
96+
"error"=> ["description"=>"返回error", "value"=>0],
97+
]);
98+
// 输出内容
99+
// {"code":401,"message":"操作成功","error":0,"data":{"name":"张三","age":30,"login_log":{"remark":"测试数据","create_time":"2008-09-01 00:00:00"}}
100+
```
101+
102+
#### 输出不包含外层信息的Json
103+
104+
使用`withoutResponseToJsonString` 可以返回只有对象属性的json数据
105+
106+
```php
107+
$user->withoutResponseToJsonString();
108+
// 输出内容
109+
// {"name":"张三","age":30,"login_log":{"remark":"测试数据","create_time":"2008-09-01 00:00:00"}
110+
```

docs/zh/openapi/config.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ return [
6969
*/
7070
'response' => [
7171
'code' => ['description' =>'状态码', 'example'=> 200],
72-
'message' => ['description' =>'返回信息', 'example'=> '操作成功'],
72+
'message' => ['description' =>'返回信息', 'example'=> 'success'],
7373
'data' => 'T',
7474
]
7575
];

tests/Serialize/Config/AddCastTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
use Astral\Serialize\Support\Config\ConfigManager;
99

1010
beforeAll(function () {
11-
12-
1311
class AddCastTestClass extends Serialize
1412
{
1513
#[InputName('name_three_other')]

tests/Serialize/From/NormalizerFromSerializeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class NormalizerClass extends Serialize
6969
$res = NormalizerClass::from(one: $normalizerOne, two: $normalizerTwo, three: $normalizerOne);
7070

7171
$resJson = json_encode($res);
72-
expect($resJson)->toBe('{"code":200,"message":"\u64cd\u4f5c\u6210\u529f","data":{"one":{"name_one":"one name","id_one":1},"two":{"name_two":"two name","id_2":2},"three":{"name_one":"one name","id_one":1}}}');
72+
expect($resJson)->toBe('{"code":200,"message":"success","data":{"one":{"name_one":"one name","id_one":1},"two":{"name_two":"two name","id_2":2},"three":{"name_one":"one name","id_one":1}}}');
7373

7474
$res->setMessage('233');
7575
$resJson = json_encode($res);

0 commit comments

Comments
 (0)