Skip to content

Commit ca7e79f

Browse files
committed
gitbook zh success
1 parent be44e20 commit ca7e79f

18 files changed

+201
-72
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Languages
66

7-
- [Complete documen-English](./docs/en/README.md)
7+
- [Complete documen-English](https://astrals-organization.gitbook.io/php-serialize/php-serialize-en)
88
- [完整文档-中文](https://astrals-organization.gitbook.io/php-serialize)
99

1010
# php-serialize

docs/zh/SUMMARY.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,19 @@
1414
## 注解类使用
1515

1616
* [属性分组](annotation/group-annotation.md)
17-
* [输入/输入出名称映射](annotation/alisa-annotation.md)
17+
* [输入/输出映射](annotation/alisa-annotation.md)
1818
* [Mapper映射](annotation/mapper-annotation.md)
1919
* [输入/输出忽略](annotation/ignore-annotation.md)
2020
* [时间格式映射](annotation/date-annotation.md)
21+
* [自定义注解](annotation/customer-annotation.md)
2122

2223
## 参数快速Faker
2324

2425
* [简单属性Faker](faker/value-faker.md)
2526
* [集合Faker](faker/collection-faker.md)
2627
* [嵌套Faker](faker/nested-faker.md)
27-
* [方法Faker](faker/method-faker.md)
28+
* [方法Faker](faker/method-faker.md)
29+
30+
## 自动创建OpenApi文档
31+
32+
* [基础演示](openapi/base-openapi.md)

docs/zh/annotation/alisa-annotation.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#### 名称映射
1+
## 名称映射
22

3-
##### 基础使用
3+
### 基础使用
44

55
```php
66
use Astral\Serialize\Attributes\InputName;
@@ -39,7 +39,7 @@ $userArray = $user->toArray();
3939
// ]
4040
```
4141

42-
##### 多输入/输出名称处理
42+
### 多输入/输出名称处理
4343

4444
```php
4545
use Astral\Serialize\Attributes\InputName;
@@ -104,7 +104,7 @@ $userArray = $user->toArray();
104104
// ]
105105
```
106106

107-
##### 复杂映射场景
107+
### 复杂映射场景
108108

109109
```php
110110
use Astral\Serialize\Serialize;
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
### 自定义注解类实现
2+
3+
你可以通过自定义注解类,灵活地扩展序列化库的输入输出处理逻辑。
4+
5+
---
6+
7+
#### 入参处理注解类
8+
9+
实现 `InputValueCastInterface` 接口,重写其中的 `match``resolve` 方法,来自定义输入数据的转换和处理。
10+
11+
- **`match`**:用于判断是否对当前值进行处理,返回 `true` 表示进入 `resolve`
12+
- **`resolve`**:对匹配的输入值进行转换,并返回转换后的结果。
13+
14+
示例:给输入值添加自定义前缀的注解类
15+
16+
```php
17+
use Astral\Serialize\Contracts\Attribute\InputValueCastInterface;
18+
use Attribute;
19+
20+
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_CLASS)]
21+
class CustomerInput implements InputValueCastInterface
22+
{
23+
public function __construct(
24+
public string $prefix = '',
25+
) {
26+
}
27+
28+
public function match(mixed $value, DataCollection $collection, InputValueContext $context): bool
29+
{
30+
// 对所有输入值都生效
31+
return true;
32+
}
33+
34+
public function resolve(mixed $value, DataCollection $collection, InputValueContext $context): mixed
35+
{
36+
// 给输入值添加前缀
37+
return $this->prefix . $value;
38+
}
39+
}
40+
````
41+
42+
### 输出处理注解类
43+
44+
输出处理注解与输入处理注解类似,只是实现的接口不同——需要实现 `OutputValueCastInterface`,用以对序列化输出的值进行自定义转换。
45+
46+
示例:给序列化输出的值添加自定义后缀的注解类
47+
48+
```php
49+
use Astral\Serialize\Contracts\Attribute\OutputValueCastInterface;
50+
use Attribute;
51+
52+
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_CLASS)]
53+
class CustomerOutput implements OutputValueCastInterface
54+
{
55+
public function __construct(
56+
public string $suffix = '',
57+
) {
58+
}
59+
60+
public function match(mixed $value, DataCollection $collection, OutputValueContext $context): bool
61+
{
62+
// 对所有输出值都生效
63+
return true;
64+
}
65+
66+
public function resolve(mixed $value, DataCollection $collection, OutputValueContext $context): mixed
67+
{
68+
// 给输出值添加后缀
69+
return $value . $this->suffix;
70+
}
71+
}
72+
```
73+

docs/zh/annotation/date-annotation.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#### 时间转换
1+
## 时间转换
22

33
1. 格式灵活性
44
支持多种输入和输出时间格式
@@ -10,7 +10,7 @@
1010
自动将字符串转换为 DateTime 对象
1111
保证类型的一致性和正确性
1212

13-
##### 基础使用
13+
### 基础使用
1414

1515
```php
1616
use Astral\Serialize\Attributes\Input\InputDateFormat;
@@ -57,7 +57,7 @@ $orderArray = $order->toArray();
5757
// ]
5858
```
5959

60-
##### 带时区的时间转换
60+
### 带时区的时间转换
6161

6262
```php
6363

docs/zh/annotation/group-annotation.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#### 属性分组
1+
## 属性分组
22

33
属性分组提供了一种灵活的方式来控制属性的输入和输出行为,允许在不同场景下精细地管理数据转换。
44

5-
##### 基本用法
5+
### 基本用法
66

77
在属性上使用 `#[Groups]` 注解来指定属性所属的分组。
88

@@ -148,7 +148,7 @@ $multiGroupArray = $user4->toArray();
148148
// ]
149149
```
150150

151-
##### 嵌套类指定Group类展示
151+
### 嵌套类指定Group类展示
152152

153153
```php
154154
class ComplexUser extends Serialize {

docs/zh/annotation/ignore-annotation.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#### 字段忽略
1+
## 字段忽略
22

33
1. **安全性控制**
44
- 防止敏感信息的意外泄露
@@ -12,7 +12,7 @@
1212
- 减少不必要字段的序列化开销
1313
- 精简数据传输
1414

15-
##### 基础使用
15+
### 基础使用
1616

1717
```php
1818
use Astral\Serialize\Attributes\InputIgnore;
@@ -51,7 +51,7 @@ $userArray = $user->toArray();
5151
// ]
5252
```
5353

54-
##### 分组忽略
54+
### 分组忽略
5555

5656
忽略分组需要搭配Groups注解一起使用
5757

docs/zh/annotation/mapper-annotation.md

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
##### Mapper映射
1+
### Mapper映射
2+
3+
### 属性映射
24

35
```php
46
use Astral\Serialize\Attributes\InputName;
@@ -46,7 +48,7 @@ $userArray = $user->toArray();
4648
// ]
4749
```
4850

49-
##### 全局类映射
51+
### 全局类映射
5052

5153
```php
5254
use Astral\Serialize\Attributes\InputName;
@@ -88,33 +90,7 @@ $userArray = $user->toArray();
8890
// ]
8991
```
9092

91-
###### 属性映射大于类级映射
92-
93-
```php
94-
95-
#[InputName(SnakeCaseMapper::class)]
96-
class PartialOverrideUser extends Serialize {
97-
#[InputName(PascalCaseMapper::class)]
98-
public string $userName; // 优先使用帕斯卡命名映射
99-
100-
public string $userEmail; // 继续使用类级别的全局映射
101-
}
102-
103-
$partialUser = PartialOverrideUser::from([
104-
'User_name' => '张三', // 使用蛇形映射
105-
'UserName' => '李四', // 使用帕斯卡映射
106-
'user_email' => 'user@example.com' // 使用蛇形映射
107-
]);
108-
109-
$partialUser->toArray();
110-
// $partialUser 的内容:
111-
// [
112-
// 'userName' => '李四',
113-
// 'userEmail' => 'user@example.com',
114-
// ]
115-
```
116-
117-
###### 全局类映射的分组使用
93+
### 全局类映射的分组使用
11894

11995
需要搭配`Groups`注解一起使用
12096

@@ -180,7 +156,7 @@ $complexUser = $complexUser->toArray();
180156
// 'userEmail' => 张三,
181157
// ]
182158
```
183-
#### 自定义映射器
159+
### 自定义映射器
184160

185161
```php
186162
// 自定义映射器 需要继承NameMapper 并实现 resolve
@@ -195,4 +171,30 @@ class AdvancedUser extends Serialize {
195171
#[InputName(CustomMapper::class)]
196172
public string $name;
197173
}
174+
```
175+
176+
### Tips:属性映射优先于类级映射
177+
178+
```php
179+
180+
#[InputName(SnakeCaseMapper::class)]
181+
class PartialOverrideUser extends Serialize {
182+
#[InputName(PascalCaseMapper::class)]
183+
public string $userName; // 优先使用帕斯卡命名映射
184+
185+
public string $userEmail; // 继续使用类级别的全局映射
186+
}
187+
188+
$partialUser = PartialOverrideUser::from([
189+
'User_name' => '张三', // 使用蛇形映射
190+
'UserName' => '李四', // 使用帕斯卡映射
191+
'user_email' => 'user@example.com' // 使用蛇形映射
192+
]);
193+
194+
$partialUser->toArray();
195+
// $partialUser 的内容:
196+
// [
197+
// 'userName' => '李四',
198+
// 'userEmail' => 'user@example.com',
199+
// ]
198200
```

docs/zh/faker/collection-faker.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
### 集合模拟
1+
## 集合模拟
22

33
```php
44

docs/zh/faker/method-faker.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
### Faker类方法模拟
1+
## Faker类方法模拟
2+
3+
### 基本用法
24

35
```php
46
class UserService {
@@ -13,7 +15,7 @@ class UserFaker extends Serialize {
1315
}
1416
```
1517

16-
#### 完整的示例
18+
### 完整的示例
1719

1820
```php
1921
use Astral\Serialize\Serialize;

0 commit comments

Comments
 (0)